From 6dbad8278e2cc92cd3bad09e48ea0d2eb7e0ab1d Mon Sep 17 00:00:00 2001 From: Karthik Bhat Date: Wed, 23 Oct 2024 10:47:37 +0530 Subject: [PATCH 1/3] Add input validations for controllers --- .../internal/controllers/kubeadmconfig_controller.go | 4 ++++ .../internal/controllers/clusterresourceset_controller.go | 4 ++++ .../controllers/clusterresourcesetbinding_controller.go | 4 ++++ exp/internal/controllers/machinepool_controller.go | 4 ++++ .../internal/controllers/extensionconfig_controller.go | 4 ++++ internal/controllers/cluster/cluster_controller.go | 4 ++++ .../controllers/clusterclass/clusterclass_controller.go | 4 ++++ internal/controllers/machine/machine_controller.go | 4 ++++ .../machinedeployment/machinedeployment_controller.go | 4 ++++ .../machinehealthcheck/machinehealthcheck_controller.go | 4 ++++ internal/controllers/machinehealthcheck/suite_test.go | 7 ++++--- internal/controllers/machineset/machineset_controller.go | 4 ++++ .../controllers/topology/cluster/cluster_controller.go | 4 ++++ .../machinedeployment/machinedeployment_controller.go | 4 ++++ .../topology/machineset/machineset_controller.go | 4 ++++ .../internal/controllers/dockermachinepool_controller.go | 4 ++++ .../internal/controllers/dockercluster_controller.go | 3 +++ .../internal/controllers/dockermachine_controller.go | 4 ++++ .../internal/controllers/inmemorycluster_controller.go | 4 ++++ .../internal/controllers/inmemorymachine_controller.go | 4 ++++ 20 files changed, 79 insertions(+), 3 deletions(-) diff --git a/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go b/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go index 61cf9233d989..b9a6ea250e7b 100644 --- a/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go +++ b/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go @@ -103,6 +103,10 @@ type Scope struct { // SetupWithManager sets up the reconciler with the Manager. func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.SecretCachingClient == nil || r.ClusterCache == nil || r.TokenTTL == time.Duration(0) { + return errors.New("Client, SecretCachingClient and ClusterCache must not be nil and TokenTTL must not be 0") + } + if r.KubeadmInitLock == nil { r.KubeadmInitLock = locking.NewControlPlaneInitMutex(mgr.GetClient()) } diff --git a/exp/addons/internal/controllers/clusterresourceset_controller.go b/exp/addons/internal/controllers/clusterresourceset_controller.go index 6981f9bf648a..9136f3118151 100644 --- a/exp/addons/internal/controllers/clusterresourceset_controller.go +++ b/exp/addons/internal/controllers/clusterresourceset_controller.go @@ -70,6 +70,10 @@ type ClusterResourceSetReconciler struct { } func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options, partialSecretCache cache.Cache) error { + if r.Client == nil || r.ClusterCache == nil { + return errors.New("Client and ClusterCache must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "clusterresourceset") err := ctrl.NewControllerManagedBy(mgr). For(&addonsv1.ClusterResourceSet{}). diff --git a/exp/addons/internal/controllers/clusterresourcesetbinding_controller.go b/exp/addons/internal/controllers/clusterresourcesetbinding_controller.go index 62d697b40192..79b46d516264 100644 --- a/exp/addons/internal/controllers/clusterresourcesetbinding_controller.go +++ b/exp/addons/internal/controllers/clusterresourcesetbinding_controller.go @@ -48,6 +48,10 @@ type ClusterResourceSetBindingReconciler struct { } func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil { + return errors.New("Client must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "clusterresourcesetbinding") err := ctrl.NewControllerManagedBy(mgr). For(&addonsv1.ClusterResourceSetBinding{}). diff --git a/exp/internal/controllers/machinepool_controller.go b/exp/internal/controllers/machinepool_controller.go index 89e44c946c8b..ee956dc0d7f2 100644 --- a/exp/internal/controllers/machinepool_controller.go +++ b/exp/internal/controllers/machinepool_controller.go @@ -100,6 +100,10 @@ type scope struct { } func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil { + return errors.New("Client, APIReader and ClusterCache must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machinepool") clusterToMachinePools, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &expv1.MachinePoolList{}, mgr.GetScheme()) if err != nil { diff --git a/exp/runtime/internal/controllers/extensionconfig_controller.go b/exp/runtime/internal/controllers/extensionconfig_controller.go index e4c6eba808f7..cbd344028c0f 100644 --- a/exp/runtime/internal/controllers/extensionconfig_controller.go +++ b/exp/runtime/internal/controllers/extensionconfig_controller.go @@ -63,6 +63,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options, partialSecretCache cache.Cache) error { + if r.Client == nil || r.APIReader == nil || r.RuntimeClient == nil { + return errors.New("Client, APIReader and RuntimeClient must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "extensionconfig") err := ctrl.NewControllerManagedBy(mgr). For(&runtimev1.ExtensionConfig{}). diff --git a/internal/controllers/cluster/cluster_controller.go b/internal/controllers/cluster/cluster_controller.go index 17a646bcfc32..78c5dc5d90e5 100644 --- a/internal/controllers/cluster/cluster_controller.go +++ b/internal/controllers/cluster/cluster_controller.go @@ -87,6 +87,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil || r.RemoteConnectionGracePeriod == time.Duration(0) { + return errors.New("Client, APIReader and ClusterCache must not be nil and RemoteConnectionGracePeriod must not be 0") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "cluster") c, err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1.Cluster{}). diff --git a/internal/controllers/clusterclass/clusterclass_controller.go b/internal/controllers/clusterclass/clusterclass_controller.go index 14ef02cea190..a12c06a0e676 100644 --- a/internal/controllers/clusterclass/clusterclass_controller.go +++ b/internal/controllers/clusterclass/clusterclass_controller.go @@ -71,6 +71,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.RuntimeClient == nil { + return errors.New("Client and RuntimeClient must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "clusterclass") err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1.ClusterClass{}). diff --git a/internal/controllers/machine/machine_controller.go b/internal/controllers/machine/machine_controller.go index 9babad1c13a3..4e85a0269d3f 100644 --- a/internal/controllers/machine/machine_controller.go +++ b/internal/controllers/machine/machine_controller.go @@ -108,6 +108,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil || r.RemoteConditionsGracePeriod == time.Duration(0) { + return errors.New("Client, APIReader and ClusterCache must not be nil and RemoteConditionsGracePeriod must not be 0") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machine") clusterToMachines, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineList{}, mgr.GetScheme()) if err != nil { diff --git a/internal/controllers/machinedeployment/machinedeployment_controller.go b/internal/controllers/machinedeployment/machinedeployment_controller.go index 9850391303f8..820e5f551ce6 100644 --- a/internal/controllers/machinedeployment/machinedeployment_controller.go +++ b/internal/controllers/machinedeployment/machinedeployment_controller.go @@ -79,6 +79,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil { + return errors.New("Client and APIReader must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machinedeployment") clusterToMachineDeployments, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineDeploymentList{}, mgr.GetScheme()) if err != nil { diff --git a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go index a355407901f9..03743d68d298 100644 --- a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go +++ b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go @@ -87,6 +87,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.ClusterCache == nil { + return errors.New("Client and ClusterCache must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machinehealthcheck") c, err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1.MachineHealthCheck{}). diff --git a/internal/controllers/machinehealthcheck/suite_test.go b/internal/controllers/machinehealthcheck/suite_test.go index 69dc6beea760..85d4ef9b8bc4 100644 --- a/internal/controllers/machinehealthcheck/suite_test.go +++ b/internal/controllers/machinehealthcheck/suite_test.go @@ -93,9 +93,10 @@ func TestMain(m *testing.M) { SetConnectionCreationRetryInterval(2 * time.Second) if err := (&clustercontroller.Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetClient(), - ClusterCache: clusterCache, + Client: mgr.GetClient(), + APIReader: mgr.GetClient(), + ClusterCache: clusterCache, + RemoteConnectionGracePeriod: 50 * time.Second, }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err)) } diff --git a/internal/controllers/machineset/machineset_controller.go b/internal/controllers/machineset/machineset_controller.go index ec81faa40593..05883f54b019 100644 --- a/internal/controllers/machineset/machineset_controller.go +++ b/internal/controllers/machineset/machineset_controller.go @@ -102,6 +102,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil { + return errors.New("Client, APIReader and ClusterCache must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "machineset") clusterToMachineSets, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineSetList{}, mgr.GetScheme()) if err != nil { diff --git a/internal/controllers/topology/cluster/cluster_controller.go b/internal/controllers/topology/cluster/cluster_controller.go index 684b280caa7c..ddbddbaf5e64 100644 --- a/internal/controllers/topology/cluster/cluster_controller.go +++ b/internal/controllers/topology/cluster/cluster_controller.go @@ -89,6 +89,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil || r.ClusterCache == nil { + return errors.New("Client, APIReader and ClusterCache must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "topology/cluster") c, err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1.Cluster{}, builder.WithPredicates( diff --git a/internal/controllers/topology/machinedeployment/machinedeployment_controller.go b/internal/controllers/topology/machinedeployment/machinedeployment_controller.go index a44073d1235d..b1a8e27ed564 100644 --- a/internal/controllers/topology/machinedeployment/machinedeployment_controller.go +++ b/internal/controllers/topology/machinedeployment/machinedeployment_controller.go @@ -59,6 +59,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil { + return errors.New("Client and APIReader must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "topology/machinedeployment") clusterToMachineDeployments, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineDeploymentList{}, mgr.GetScheme()) if err != nil { diff --git a/internal/controllers/topology/machineset/machineset_controller.go b/internal/controllers/topology/machineset/machineset_controller.go index 7cfc8e13173f..e2b119477ec0 100644 --- a/internal/controllers/topology/machineset/machineset_controller.go +++ b/internal/controllers/topology/machineset/machineset_controller.go @@ -61,6 +61,10 @@ type Reconciler struct { } func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.APIReader == nil { + return errors.New("Client and APIReader must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "topology/machineset") clusterToMachineSets, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineSetList{}, mgr.GetScheme()) if err != nil { diff --git a/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go b/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go index 1c9c1f39aa42..8ffb791f291a 100644 --- a/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go +++ b/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go @@ -157,6 +157,10 @@ func (r *DockerMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Re // SetupWithManager will add watches for this controller. func (r *DockerMachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.ContainerRuntime == nil || r.Scheme == nil { + return errors.New("Client, ContainerRuntime and Scheme must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "dockermachinepool") clusterToDockerMachinePools, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &infraexpv1.DockerMachinePoolList{}, mgr.GetScheme()) if err != nil { diff --git a/test/infrastructure/docker/internal/controllers/dockercluster_controller.go b/test/infrastructure/docker/internal/controllers/dockercluster_controller.go index a831cd9f127b..6eba1c266cb1 100644 --- a/test/infrastructure/docker/internal/controllers/dockercluster_controller.go +++ b/test/infrastructure/docker/internal/controllers/dockercluster_controller.go @@ -202,6 +202,9 @@ func (r *DockerClusterReconciler) reconcileDelete(ctx context.Context, dockerClu // SetupWithManager will add watches for this controller. func (r *DockerClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.ContainerRuntime == nil { + return errors.New("Client and ContainerRuntime must not be nil") + } predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "dockercluster") err := ctrl.NewControllerManagedBy(mgr). For(&infrav1.DockerCluster{}). diff --git a/test/infrastructure/docker/internal/controllers/dockermachine_controller.go b/test/infrastructure/docker/internal/controllers/dockermachine_controller.go index 8726bb468ea1..fd855062948f 100644 --- a/test/infrastructure/docker/internal/controllers/dockermachine_controller.go +++ b/test/infrastructure/docker/internal/controllers/dockermachine_controller.go @@ -476,6 +476,10 @@ func (r *DockerMachineReconciler) reconcileDelete(ctx context.Context, dockerClu // SetupWithManager will add watches for this controller. func (r *DockerMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.ContainerRuntime == nil || r.ClusterCache == nil { + return errors.New("Client, ContainerRuntime and ClusterCache must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "dockermachine") clusterToDockerMachines, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &infrav1.DockerMachineList{}, mgr.GetScheme()) if err != nil { diff --git a/test/infrastructure/inmemory/internal/controllers/inmemorycluster_controller.go b/test/infrastructure/inmemory/internal/controllers/inmemorycluster_controller.go index f59322771fa8..d98c302e37b1 100644 --- a/test/infrastructure/inmemory/internal/controllers/inmemorycluster_controller.go +++ b/test/infrastructure/inmemory/internal/controllers/inmemorycluster_controller.go @@ -212,6 +212,10 @@ func (r *InMemoryClusterReconciler) reconcileDelete(_ context.Context, cluster * // SetupWithManager will add watches for this controller. func (r *InMemoryClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.InMemoryManager == nil || r.APIServerMux == nil { + return errors.New("Client, InMemoryManager and APIServerMux must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "inmemorycluster") err := ctrl.NewControllerManagedBy(mgr). For(&infrav1.InMemoryCluster{}). diff --git a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go index 0f74968402c1..748cfb4fc018 100644 --- a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go +++ b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go @@ -1135,6 +1135,10 @@ func (r *InMemoryMachineReconciler) reconcileDeleteControllerManager(ctx context // SetupWithManager will add watches for this controller. func (r *InMemoryMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { + if r.Client == nil || r.InMemoryManager == nil || r.APIServerMux == nil { + return errors.New("Client, InMemoryManager and APIServerMux must not be nil") + } + predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "inmemorymachine") clusterToInMemoryMachines, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &infrav1.InMemoryMachineList{}, mgr.GetScheme()) if err != nil { From 728ca8a2c49e6b584eb7874b2f80e0f3057e6873 Mon Sep 17 00:00:00 2001 From: Karthik Bhat Date: Thu, 24 Oct 2024 16:56:13 +0530 Subject: [PATCH 2/3] Fix test cases after input validation --- exp/internal/controllers/suite_test.go | 1 + internal/controllers/cluster/suite_test.go | 7 ++++--- internal/controllers/clusterclass/suite_test.go | 4 +++- internal/controllers/machine/suite_test.go | 7 ++++--- internal/controllers/machinedeployment/suite_test.go | 7 ++++--- internal/controllers/machinehealthcheck/suite_test.go | 7 ++++--- internal/controllers/machineset/suite_test.go | 7 ++++--- internal/controllers/topology/cluster/suite_test.go | 4 +++- internal/webhooks/test/suite_test.go | 4 +++- 9 files changed, 30 insertions(+), 18 deletions(-) diff --git a/exp/internal/controllers/suite_test.go b/exp/internal/controllers/suite_test.go index 86446177ac29..3aaff11385ca 100644 --- a/exp/internal/controllers/suite_test.go +++ b/exp/internal/controllers/suite_test.go @@ -68,6 +68,7 @@ func TestMain(m *testing.M) { if err := (&MachinePoolReconciler{ Client: mgr.GetClient(), + APIReader: mgr.GetAPIReader(), ClusterCache: clusterCache, recorder: mgr.GetEventRecorderFor("machinepool-controller"), }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { diff --git a/internal/controllers/cluster/suite_test.go b/internal/controllers/cluster/suite_test.go index dbcc68edab03..122a405cad73 100644 --- a/internal/controllers/cluster/suite_test.go +++ b/internal/controllers/cluster/suite_test.go @@ -98,9 +98,10 @@ func TestMain(m *testing.M) { panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err)) } if err := (&machinecontroller.Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetAPIReader(), - ClusterCache: clusterCache, + Client: mgr.GetClient(), + APIReader: mgr.GetAPIReader(), + ClusterCache: clusterCache, + RemoteConditionsGracePeriod: 5 * time.Minute, }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) } diff --git a/internal/controllers/clusterclass/suite_test.go b/internal/controllers/clusterclass/suite_test.go index 3a9e4b37af41..d945d10bdcac 100644 --- a/internal/controllers/clusterclass/suite_test.go +++ b/internal/controllers/clusterclass/suite_test.go @@ -38,6 +38,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/api/v1beta1/index" "sigs.k8s.io/cluster-api/feature" + fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake" "sigs.k8s.io/cluster-api/internal/test/envtest" ) @@ -63,7 +64,8 @@ func TestMain(m *testing.M) { } setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) { if err := (&Reconciler{ - Client: mgr.GetClient(), + Client: mgr.GetClient(), + RuntimeClient: fakeruntimeclient.NewRuntimeClientBuilder().Build(), }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 5}); err != nil { panic(fmt.Sprintf("unable to create clusterclass reconciler: %v", err)) } diff --git a/internal/controllers/machine/suite_test.go b/internal/controllers/machine/suite_test.go index 94c68649886c..35fb52d9aadc 100644 --- a/internal/controllers/machine/suite_test.go +++ b/internal/controllers/machine/suite_test.go @@ -94,9 +94,10 @@ func TestMain(m *testing.M) { SetConnectionCreationRetryInterval(2 * time.Second) if err := (&Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetAPIReader(), - ClusterCache: clusterCache, + Client: mgr.GetClient(), + APIReader: mgr.GetAPIReader(), + ClusterCache: clusterCache, + RemoteConditionsGracePeriod: 5 * time.Minute, }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) } diff --git a/internal/controllers/machinedeployment/suite_test.go b/internal/controllers/machinedeployment/suite_test.go index 81ebe8c5ab52..ff041b191bbd 100644 --- a/internal/controllers/machinedeployment/suite_test.go +++ b/internal/controllers/machinedeployment/suite_test.go @@ -91,9 +91,10 @@ func TestMain(m *testing.M) { } if err := (&machinecontroller.Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetAPIReader(), - ClusterCache: clusterCache, + Client: mgr.GetClient(), + APIReader: mgr.GetAPIReader(), + ClusterCache: clusterCache, + RemoteConditionsGracePeriod: 5 * time.Minute, }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) } diff --git a/internal/controllers/machinehealthcheck/suite_test.go b/internal/controllers/machinehealthcheck/suite_test.go index 85d4ef9b8bc4..57f926650c49 100644 --- a/internal/controllers/machinehealthcheck/suite_test.go +++ b/internal/controllers/machinehealthcheck/suite_test.go @@ -107,9 +107,10 @@ func TestMain(m *testing.M) { panic(fmt.Sprintf("Failed to start Reconciler : %v", err)) } if err := (&machinecontroller.Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetAPIReader(), - ClusterCache: clusterCache, + Client: mgr.GetClient(), + APIReader: mgr.GetAPIReader(), + ClusterCache: clusterCache, + RemoteConditionsGracePeriod: 5 * time.Minute, }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) } diff --git a/internal/controllers/machineset/suite_test.go b/internal/controllers/machineset/suite_test.go index 59c592951382..ec9aa076bfb2 100644 --- a/internal/controllers/machineset/suite_test.go +++ b/internal/controllers/machineset/suite_test.go @@ -97,9 +97,10 @@ func TestMain(m *testing.M) { panic(fmt.Sprintf("Failed to start MMachineSetReconciler: %v", err)) } if err := (&machinecontroller.Reconciler{ - Client: mgr.GetClient(), - APIReader: mgr.GetAPIReader(), - ClusterCache: clusterCache, + Client: mgr.GetClient(), + APIReader: mgr.GetAPIReader(), + ClusterCache: clusterCache, + RemoteConditionsGracePeriod: 5 * time.Minute, }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil { panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) } diff --git a/internal/controllers/topology/cluster/suite_test.go b/internal/controllers/topology/cluster/suite_test.go index 41f997b73c04..b92128a834fe 100644 --- a/internal/controllers/topology/cluster/suite_test.go +++ b/internal/controllers/topology/cluster/suite_test.go @@ -38,6 +38,7 @@ import ( "sigs.k8s.io/cluster-api/controllers/remote" expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" "sigs.k8s.io/cluster-api/internal/controllers/clusterclass" + fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake" "sigs.k8s.io/cluster-api/internal/test/envtest" ) @@ -94,7 +95,8 @@ func TestMain(m *testing.M) { panic(fmt.Sprintf("unable to create topology cluster reconciler: %v", err)) } if err := (&clusterclass.Reconciler{ - Client: mgr.GetClient(), + Client: mgr.GetClient(), + RuntimeClient: fakeruntimeclient.NewRuntimeClientBuilder().Build(), }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 5}); err != nil { panic(fmt.Sprintf("unable to create clusterclass reconciler: %v", err)) } diff --git a/internal/webhooks/test/suite_test.go b/internal/webhooks/test/suite_test.go index e592897427a3..a80f88eb680b 100644 --- a/internal/webhooks/test/suite_test.go +++ b/internal/webhooks/test/suite_test.go @@ -29,6 +29,7 @@ import ( "sigs.k8s.io/cluster-api/api/v1beta1/index" "sigs.k8s.io/cluster-api/feature" "sigs.k8s.io/cluster-api/internal/controllers/clusterclass" + fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake" "sigs.k8s.io/cluster-api/internal/test/envtest" ) @@ -48,7 +49,8 @@ func TestMain(m *testing.M) { } setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) { if err := (&clusterclass.Reconciler{ - Client: mgr.GetClient(), + Client: mgr.GetClient(), + RuntimeClient: fakeruntimeclient.NewRuntimeClientBuilder().Build(), }).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 5}); err != nil { panic(fmt.Sprintf("unable to create clusterclass reconciler: %v", err)) } From d1b3fa48f414aac845a7e47844979929c6273d27 Mon Sep 17 00:00:00 2001 From: Karthik Bhat Date: Fri, 25 Oct 2024 12:32:01 +0530 Subject: [PATCH 3/3] Update DockerMachinePoolReconciler to remove unused Scheme --- test/infrastructure/docker/exp/controllers/alias.go | 3 --- .../internal/controllers/dockermachinepool_controller.go | 6 ++---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/test/infrastructure/docker/exp/controllers/alias.go b/test/infrastructure/docker/exp/controllers/alias.go index a7778328d0e9..adf1beac6676 100644 --- a/test/infrastructure/docker/exp/controllers/alias.go +++ b/test/infrastructure/docker/exp/controllers/alias.go @@ -20,7 +20,6 @@ package controllers import ( "context" - "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -32,7 +31,6 @@ import ( // DockerMachinePoolReconciler reconciles a DockerMachinePool object. type DockerMachinePoolReconciler struct { Client client.Client - Scheme *runtime.Scheme ContainerRuntime container.Runtime // WatchFilterValue is the label value used to filter events prior to reconciliation. @@ -43,7 +41,6 @@ type DockerMachinePoolReconciler struct { func (r *DockerMachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { return (&dockermachinepoolcontrollers.DockerMachinePoolReconciler{ Client: r.Client, - Scheme: r.Scheme, ContainerRuntime: r.ContainerRuntime, WatchFilterValue: r.WatchFilterValue, }).SetupWithManager(ctx, mgr, options) diff --git a/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go b/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go index 8ffb791f291a..160178d71176 100644 --- a/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go +++ b/test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go @@ -24,7 +24,6 @@ import ( "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" kerrors "k8s.io/apimachinery/pkg/util/errors" @@ -63,7 +62,6 @@ const ( // DockerMachinePoolReconciler reconciles a DockerMachinePool object. type DockerMachinePoolReconciler struct { Client client.Client - Scheme *runtime.Scheme ContainerRuntime container.Runtime // WatchFilterValue is the label value used to filter events prior to reconciliation. @@ -157,8 +155,8 @@ func (r *DockerMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Re // SetupWithManager will add watches for this controller. func (r *DockerMachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { - if r.Client == nil || r.ContainerRuntime == nil || r.Scheme == nil { - return errors.New("Client, ContainerRuntime and Scheme must not be nil") + if r.Client == nil || r.ContainerRuntime == nil { + return errors.New("Client and ContainerRuntime must not be nil") } predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "dockermachinepool")