Skip to content

Commit e5cbc4a

Browse files
authored
🌱 Add input validations for controllers (#11327)
* Add input validations for controllers * Fix test cases after input validation * Update DockerMachinePoolReconciler to remove unused Scheme
1 parent 0000f09 commit e5cbc4a

File tree

29 files changed

+109
-26
lines changed

29 files changed

+109
-26
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ type Scope struct {
103103

104104
// SetupWithManager sets up the reconciler with the Manager.
105105
func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
106+
if r.Client == nil || r.SecretCachingClient == nil || r.ClusterCache == nil || r.TokenTTL == time.Duration(0) {
107+
return errors.New("Client, SecretCachingClient and ClusterCache must not be nil and TokenTTL must not be 0")
108+
}
109+
106110
if r.KubeadmInitLock == nil {
107111
r.KubeadmInitLock = locking.NewControlPlaneInitMutex(mgr.GetClient())
108112
}

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ type ClusterResourceSetReconciler struct {
7070
}
7171

7272
func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options, partialSecretCache cache.Cache) error {
73+
if r.Client == nil || r.ClusterCache == nil {
74+
return errors.New("Client and ClusterCache must not be nil")
75+
}
76+
7377
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "clusterresourceset")
7478
err := ctrl.NewControllerManagedBy(mgr).
7579
For(&addonsv1.ClusterResourceSet{}).

exp/addons/internal/controllers/clusterresourcesetbinding_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type ClusterResourceSetBindingReconciler struct {
4848
}
4949

5050
func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
51+
if r.Client == nil {
52+
return errors.New("Client must not be nil")
53+
}
54+
5155
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "clusterresourcesetbinding")
5256
err := ctrl.NewControllerManagedBy(mgr).
5357
For(&addonsv1.ClusterResourceSetBinding{}).

exp/internal/controllers/machinepool_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ type scope struct {
100100
}
101101

102102
func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
103+
if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil {
104+
return errors.New("Client, APIReader and ClusterCache must not be nil")
105+
}
106+
103107
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machinepool")
104108
clusterToMachinePools, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &expv1.MachinePoolList{}, mgr.GetScheme())
105109
if err != nil {

exp/internal/controllers/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestMain(m *testing.M) {
6868

6969
if err := (&MachinePoolReconciler{
7070
Client: mgr.GetClient(),
71+
APIReader: mgr.GetAPIReader(),
7172
ClusterCache: clusterCache,
7273
recorder: mgr.GetEventRecorderFor("machinepool-controller"),
7374
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {

exp/runtime/internal/controllers/extensionconfig_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ type Reconciler struct {
6363
}
6464

6565
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options, partialSecretCache cache.Cache) error {
66+
if r.Client == nil || r.APIReader == nil || r.RuntimeClient == nil {
67+
return errors.New("Client, APIReader and RuntimeClient must not be nil")
68+
}
69+
6670
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "extensionconfig")
6771
err := ctrl.NewControllerManagedBy(mgr).
6872
For(&runtimev1.ExtensionConfig{}).

internal/controllers/cluster/cluster_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ type Reconciler struct {
8787
}
8888

8989
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
90+
if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil || r.RemoteConnectionGracePeriod == time.Duration(0) {
91+
return errors.New("Client, APIReader and ClusterCache must not be nil and RemoteConnectionGracePeriod must not be 0")
92+
}
93+
9094
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "cluster")
9195
c, err := ctrl.NewControllerManagedBy(mgr).
9296
For(&clusterv1.Cluster{}).

internal/controllers/cluster/suite_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ func TestMain(m *testing.M) {
9898
panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err))
9999
}
100100
if err := (&machinecontroller.Reconciler{
101-
Client: mgr.GetClient(),
102-
APIReader: mgr.GetAPIReader(),
103-
ClusterCache: clusterCache,
101+
Client: mgr.GetClient(),
102+
APIReader: mgr.GetAPIReader(),
103+
ClusterCache: clusterCache,
104+
RemoteConditionsGracePeriod: 5 * time.Minute,
104105
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
105106
panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err))
106107
}

internal/controllers/clusterclass/clusterclass_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ type Reconciler struct {
7171
}
7272

7373
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
74+
if r.Client == nil || r.RuntimeClient == nil {
75+
return errors.New("Client and RuntimeClient must not be nil")
76+
}
77+
7478
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "clusterclass")
7579
err := ctrl.NewControllerManagedBy(mgr).
7680
For(&clusterv1.ClusterClass{}).

internal/controllers/clusterclass/suite_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3939
"sigs.k8s.io/cluster-api/api/v1beta1/index"
4040
"sigs.k8s.io/cluster-api/feature"
41+
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
4142
"sigs.k8s.io/cluster-api/internal/test/envtest"
4243
)
4344

@@ -63,7 +64,8 @@ func TestMain(m *testing.M) {
6364
}
6465
setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) {
6566
if err := (&Reconciler{
66-
Client: mgr.GetClient(),
67+
Client: mgr.GetClient(),
68+
RuntimeClient: fakeruntimeclient.NewRuntimeClientBuilder().Build(),
6769
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 5}); err != nil {
6870
panic(fmt.Sprintf("unable to create clusterclass reconciler: %v", err))
6971
}

0 commit comments

Comments
 (0)