From 5dbcc1384c32e99fb0f248ad7dea56346a86ba42 Mon Sep 17 00:00:00 2001 From: Rahul Ganesh Date: Thu, 29 Feb 2024 16:58:25 -0800 Subject: [PATCH] Use an uncached direct client to read the kcp and md to bypass delay in cache Signed-off-by: Rahul Ganesh --- controllers/factory.go | 2 + controllers/factory_test.go | 2 + controllers/kubeadmcontrolplane_controller.go | 52 ++++++++++--------- .../kubeadmcontrolplane_controller_test.go | 20 +++---- controllers/machinedeployment_controller.go | 52 ++++++++++--------- .../machinedeployment_controller_test.go | 18 +++---- 6 files changed, 79 insertions(+), 67 deletions(-) diff --git a/controllers/factory.go b/controllers/factory.go index 30e86ca2176e..517c54b511af 100644 --- a/controllers/factory.go +++ b/controllers/factory.go @@ -602,6 +602,7 @@ func (f *Factory) WithKubeadmControlPlaneReconciler() *Factory { f.reconcilers.KubeadmControlPlaneReconciler = NewKubeadmControlPlaneReconciler( f.manager.GetClient(), + f.manager.GetAPIReader(), ) return nil @@ -619,6 +620,7 @@ func (f *Factory) WithMachineDeploymentReconciler() *Factory { f.reconcilers.MachineDeploymentReconciler = NewMachineDeploymentReconciler( f.manager.GetClient(), + f.manager.GetAPIReader(), ) return nil diff --git a/controllers/factory_test.go b/controllers/factory_test.go index bfa229eb2799..f53360f66298 100644 --- a/controllers/factory_test.go +++ b/controllers/factory_test.go @@ -237,6 +237,7 @@ func TestFactoryWithKubeadmControlPlaneReconciler(t *testing.T) { ctrl := gomock.NewController(t) manager := mocks.NewMockManager(ctrl) manager.EXPECT().GetClient().AnyTimes() + manager.EXPECT().GetAPIReader().AnyTimes() manager.EXPECT().GetScheme().AnyTimes() f := controllers.NewFactory(logger, manager). @@ -257,6 +258,7 @@ func TestFactoryWithMachineDeploymentReconciler(t *testing.T) { ctrl := gomock.NewController(t) manager := mocks.NewMockManager(ctrl) manager.EXPECT().GetClient().AnyTimes() + manager.EXPECT().GetAPIReader().AnyTimes() manager.EXPECT().GetScheme().AnyTimes() f := controllers.NewFactory(logger, manager). diff --git a/controllers/kubeadmcontrolplane_controller.go b/controllers/kubeadmcontrolplane_controller.go index 14602e278ed2..5aedaca3268a 100644 --- a/controllers/kubeadmcontrolplane_controller.go +++ b/controllers/kubeadmcontrolplane_controller.go @@ -45,19 +45,24 @@ import ( const ( kcpInPlaceUpgradeNeededAnnotation = "controlplane.clusters.x-k8s.io/in-place-upgrade-needed" controlPlaneMachineLabel = "cluster.x-k8s.io/control-plane-name" + kubeadmControlPlaneKind = "KubeadmControlPlane" ) // KubeadmControlPlaneReconciler reconciles a KubeadmControlPlaneReconciler object. type KubeadmControlPlaneReconciler struct { + // client reads from a cache and is not a fully direct client. client client.Client - log logr.Logger + // uncachedClient reads directly from the API server and is slightly slower. + uncachedClient client.Reader + log logr.Logger } // NewKubeadmControlPlaneReconciler returns a new instance of KubeadmControlPlaneReconciler. -func NewKubeadmControlPlaneReconciler(client client.Client) *KubeadmControlPlaneReconciler { +func NewKubeadmControlPlaneReconciler(client client.Client, uncachedClient client.Reader) *KubeadmControlPlaneReconciler { return &KubeadmControlPlaneReconciler{ - client: client, - log: ctrl.Log.WithName("KubeadmControlPlaneController"), + client: client, + uncachedClient: uncachedClient, + log: ctrl.Log.WithName("KubeadmControlPlaneController"), } } @@ -69,7 +74,7 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl. log := r.log.WithValues("KubeadmControlPlane", req.NamespacedName) kcp := &controlplanev1.KubeadmControlPlane{} - if err := r.client.Get(ctx, req.NamespacedName, kcp); err != nil { + if err := r.uncachedClient.Get(ctx, req.NamespacedName, kcp); err != nil { if apierrors.IsNotFound(err) { return reconcile.Result{}, err } @@ -117,11 +122,11 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, log logr. } mhc := &clusterv1.MachineHealthCheck{} - if err := r.client.Get(ctx, GetNamespacedNameType(cpMachineHealthCheckName(kcp.Name), constants.EksaSystemNamespace), mhc); err != nil { + if err := r.client.Get(ctx, GetNamespacedNameType(cpMachineHealthCheckName(kcp.ObjectMeta.Name), constants.EksaSystemNamespace), mhc); err != nil { if apierrors.IsNotFound(err) { return reconcile.Result{}, err } - return ctrl.Result{}, fmt.Errorf("getting MachineHealthCheck %s: %v", cpMachineHealthCheckName(kcp.Name), err) + return ctrl.Result{}, fmt.Errorf("getting MachineHealthCheck %s: %v", cpMachineHealthCheckName(kcp.ObjectMeta.Name), err) } mhcPatchHelper, err := patch.NewHelper(mhc, r.client) if err != nil { @@ -129,14 +134,14 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, log logr. } cpUpgrade := &anywherev1.ControlPlaneUpgrade{} - cpuGetErr := r.client.Get(ctx, GetNamespacedNameType(cpUpgradeName(kcp.Name), constants.EksaSystemNamespace), cpUpgrade) + cpuGetErr := r.client.Get(ctx, GetNamespacedNameType(cpUpgradeName(kcp.ObjectMeta.Name), constants.EksaSystemNamespace), cpUpgrade) if cpuGetErr == nil { if cpUpgrade.Status.Ready && kcp.Status.Version != nil && *kcp.Status.Version == cpUpgrade.Spec.KubernetesVersion { log.Info("Control plane upgrade complete, deleting object", "ControlPlaneUpgrade", cpUpgrade.Name) if err := r.client.Delete(ctx, cpUpgrade); err != nil { return ctrl.Result{}, fmt.Errorf("deleting ControlPlaneUpgrade object: %v", err) } - log.Info("Resuming control plane machine health check", "MachineHealthCheck", cpMachineHealthCheckName(kcp.Name)) + log.Info("Resuming control plane machine health check", "MachineHealthCheck", cpMachineHealthCheckName(kcp.ObjectMeta.Name)) if err := resumeMachineHealthCheck(ctx, mhc, mhcPatchHelper); err != nil { return ctrl.Result{}, fmt.Errorf("updating annotations for machine health check: %v", err) } @@ -159,19 +164,18 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, log logr. return ctrl.Result{}, fmt.Errorf("generating ControlPlaneUpgrade: %v", err) } - log.Info("Pausing control plane machine health check", "MachineHealthCheck", cpMachineHealthCheckName(kcp.Name)) + log.Info("Pausing control plane machine health check", "MachineHealthCheck", cpMachineHealthCheckName(kcp.ObjectMeta.Name)) if err := pauseMachineHealthCheck(ctx, mhc, mhcPatchHelper); err != nil { return ctrl.Result{}, fmt.Errorf("updating annotations for machine health check: %v", err) } if err := r.client.Create(ctx, cpUpgrade); client.IgnoreAlreadyExists(err) != nil { - return ctrl.Result{}, fmt.Errorf("failed to create ControlPlaneUpgrade for KubeadmControlPlane %s: %v", kcp.Name, err) + return ctrl.Result{}, fmt.Errorf("failed to create ControlPlaneUpgrade for KubeadmControlPlane %s: %v", kcp.ObjectMeta.Name, err) } return ctrl.Result{}, nil } - return ctrl.Result{}, fmt.Errorf("getting ControlPlaneUpgrade for KubeadmControlPlane %s: %v", kcp.Name, err) - + return ctrl.Result{}, fmt.Errorf("getting ControlPlaneUpgrade for KubeadmControlPlane %s: %v", kcp.ObjectMeta.Name, err) } func (r *KubeadmControlPlaneReconciler) inPlaceUpgradeNeeded(kcp *controlplanev1.KubeadmControlPlane) bool { @@ -179,12 +183,12 @@ func (r *KubeadmControlPlaneReconciler) inPlaceUpgradeNeeded(kcp *controlplanev1 } func (r *KubeadmControlPlaneReconciler) machinesToUpgrade(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane) ([]corev1.ObjectReference, error) { - selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{controlPlaneMachineLabel: kcp.Name}}) + selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{controlPlaneMachineLabel: kcp.ObjectMeta.Name}}) if err != nil { return nil, err } machineList := &clusterv1.MachineList{} - if err := r.client.List(ctx, machineList, &client.ListOptions{LabelSelector: selector, Namespace: kcp.Namespace}); err != nil { + if err := r.client.List(ctx, machineList, &client.ListOptions{LabelSelector: selector, Namespace: kcp.ObjectMeta.Namespace}); err != nil { return nil, err } machines := collections.FromMachineList(machineList).SortedByCreationTimestamp() @@ -203,7 +207,7 @@ func (r *KubeadmControlPlaneReconciler) machinesToUpgrade(ctx context.Context, k func (r *KubeadmControlPlaneReconciler) validateStackedEtcd(kcp *controlplanev1.KubeadmControlPlane) error { if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration == nil { - return fmt.Errorf("ClusterConfiguration not set for KubeadmControlPlane \"%s\", unable to retrieve etcd information", kcp.Name) + return fmt.Errorf("ClusterConfiguration not set for KubeadmControlPlane \"%s\", unable to retrieve etcd information", kcp.ObjectMeta.Name) } if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.Etcd.Local == nil { return fmt.Errorf("local etcd configuration is missing") @@ -229,20 +233,20 @@ func controlPlaneUpgrade(kcp *controlplanev1.KubeadmControlPlane, machines []cor return &anywherev1.ControlPlaneUpgrade{ ObjectMeta: metav1.ObjectMeta{ - Name: cpUpgradeName(kcp.Name), + Name: cpUpgradeName(kcp.ObjectMeta.Name), Namespace: constants.EksaSystemNamespace, OwnerReferences: []metav1.OwnerReference{{ - APIVersion: kcp.APIVersion, - Kind: kcp.Kind, - Name: kcp.Name, - UID: kcp.UID, + APIVersion: controlplanev1.GroupVersion.String(), + Kind: kubeadmControlPlaneKind, + Name: kcp.ObjectMeta.Name, + UID: kcp.ObjectMeta.UID, }}, }, Spec: anywherev1.ControlPlaneUpgradeSpec{ ControlPlane: corev1.ObjectReference{ - Kind: kcp.Kind, - Namespace: kcp.Namespace, - Name: kcp.Name, + Kind: kubeadmControlPlaneKind, + Namespace: kcp.ObjectMeta.Namespace, + Name: kcp.ObjectMeta.Name, }, KubernetesVersion: kcp.Spec.Version, EtcdVersion: kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.Etcd.Local.ImageTag, diff --git a/controllers/kubeadmcontrolplane_controller_test.go b/controllers/kubeadmcontrolplane_controller_test.go index 7670b20fb34b..b07e725a6dcd 100644 --- a/controllers/kubeadmcontrolplane_controller_test.go +++ b/controllers/kubeadmcontrolplane_controller_test.go @@ -39,7 +39,7 @@ type kcpObjects struct { func TestKCPSetupWithManager(t *testing.T) { client := env.Client() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) g := NewWithT(t) g.Expect(r.SetupWithManager(env.Manager())).To(Succeed()) @@ -54,7 +54,7 @@ func TestKCPReconcileNotNeeded(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.kcp, kcpObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -72,7 +72,7 @@ func TestKCPReconcile(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.machines[0], kcpObjs.machines[1], kcpObjs.cpUpgrade, kcpObjs.kcp, kcpObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -89,7 +89,7 @@ func TestKCPReconcileCreateControlPlaneUpgrade(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.machines[0], kcpObjs.machines[1], kcpObjs.kcp, kcpObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -121,7 +121,7 @@ func TestKCPReconcileKCPAndControlPlaneUpgradeReady(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.machines[0], kcpObjs.machines[1], kcpObjs.cpUpgrade, kcpObjs.kcp, kcpObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -149,7 +149,7 @@ func TestKCPReconcileFullFlow(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.machines[0], kcpObjs.machines[1], kcpObjs.kcp, kcpObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -207,7 +207,7 @@ func TestKCPReconcileNotFound(t *testing.T) { kcpObjs := getObjectsForKCP() client := fake.NewClientBuilder().WithRuntimeObjects().Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("kubeadmcontrolplanes.controlplane.cluster.x-k8s.io \"my-cluster\" not found")) @@ -220,7 +220,7 @@ func TestKCPReconcileMHCNotFound(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.machines[0], kcpObjs.machines[1], kcpObjs.kcp} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("machinehealthchecks.cluster.x-k8s.io \"my-cluster-kcp-unhealthy\" not found")) @@ -235,7 +235,7 @@ func TestKCPReconcileClusterConfigurationMissing(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.kcp} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("ClusterConfiguration not set for KubeadmControlPlane \"my-cluster\", unable to retrieve etcd information")) @@ -250,7 +250,7 @@ func TestKCPReconcileStackedEtcdMissing(t *testing.T) { runtimeObjs := []runtime.Object{kcpObjs.kcp} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewKubeadmControlPlaneReconciler(client) + r := controllers.NewKubeadmControlPlaneReconciler(client, client) req := kcpRequest(kcpObjs.kcp) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("local etcd configuration is missing")) diff --git a/controllers/machinedeployment_controller.go b/controllers/machinedeployment_controller.go index e07b04b80dbf..bc1108217846 100644 --- a/controllers/machinedeployment_controller.go +++ b/controllers/machinedeployment_controller.go @@ -43,19 +43,24 @@ import ( const ( mdInPlaceUpgradeNeededAnnotation = "machinedeployment.clusters.x-k8s.io/in-place-upgrade-needed" workerMachineLabel = "cluster.x-k8s.io/deployment-name" + machineDeploymentKind = "MachineDeployment" ) // MachineDeploymentReconciler reconciles a MachineDeploymentReconciler object. type MachineDeploymentReconciler struct { + // client reads from a cache and is not a fully direct client. client client.Client - log logr.Logger + // uncachedClient reads directly from the API server and is slightly slower. + uncachedClient client.Reader + log logr.Logger } // NewMachineDeploymentReconciler returns a new instance of MachineDeploymentReconciler. -func NewMachineDeploymentReconciler(client client.Client) *MachineDeploymentReconciler { +func NewMachineDeploymentReconciler(client client.Client, uncachedClient client.Reader) *MachineDeploymentReconciler { return &MachineDeploymentReconciler{ - client: client, - log: ctrl.Log.WithName("MachineDeploymentController"), + client: client, + uncachedClient: uncachedClient, + log: ctrl.Log.WithName("MachineDeploymentController"), } } @@ -67,7 +72,7 @@ func (r *MachineDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Re log := r.log.WithValues("MachineDeployment", req.NamespacedName) md := &clusterv1.MachineDeployment{} - if err := r.client.Get(ctx, req.NamespacedName, md); err != nil { + if err := r.uncachedClient.Get(ctx, req.NamespacedName, md); err != nil { if apierrors.IsNotFound(err) { return reconcile.Result{}, err } @@ -111,15 +116,15 @@ func (r *MachineDeploymentReconciler) reconcile(ctx context.Context, log logr.Lo log.Info("Reconciling in place upgrade for workers") if md.Spec.Template.Spec.Version == nil { log.Info("Kubernetes version not present, unable to reconcile for in place upgrade") - return ctrl.Result{}, fmt.Errorf("unable to retrieve kubernetes version from MachineDeployment \"%s\"", md.Name) + return ctrl.Result{}, fmt.Errorf("unable to retrieve kubernetes version from MachineDeployment \"%s\"", md.ObjectMeta.Name) } mhc := &clusterv1.MachineHealthCheck{} - if err := r.client.Get(ctx, GetNamespacedNameType(mdMachineHealthCheckName(md.Name), constants.EksaSystemNamespace), mhc); err != nil { + if err := r.client.Get(ctx, GetNamespacedNameType(mdMachineHealthCheckName(md.ObjectMeta.Name), constants.EksaSystemNamespace), mhc); err != nil { if apierrors.IsNotFound(err) { return reconcile.Result{}, err } - return ctrl.Result{}, fmt.Errorf("getting MachineHealthCheck %s: %v", mdMachineHealthCheckName(md.Name), err) + return ctrl.Result{}, fmt.Errorf("getting MachineHealthCheck %s: %v", mdMachineHealthCheckName(md.ObjectMeta.Name), err) } mhcPatchHelper, err := patch.NewHelper(mhc, r.client) if err != nil { @@ -132,7 +137,7 @@ func (r *MachineDeploymentReconciler) reconcile(ctx context.Context, log logr.Lo } mdUpgrade := &anywherev1.MachineDeploymentUpgrade{} - mduGetErr := r.client.Get(ctx, GetNamespacedNameType(mdUpgradeName(md.Name), constants.EksaSystemNamespace), mdUpgrade) + mduGetErr := r.client.Get(ctx, GetNamespacedNameType(mdUpgradeName(md.ObjectMeta.Name), constants.EksaSystemNamespace), mdUpgrade) if mduGetErr == nil { machinesUpgraded := true for i := range machineList { @@ -147,7 +152,7 @@ func (r *MachineDeploymentReconciler) reconcile(ctx context.Context, log logr.Lo if err := r.client.Delete(ctx, mdUpgrade); err != nil { return ctrl.Result{}, fmt.Errorf("deleting MachineDeploymentUpgrade object: %v", err) } - log.Info("Resuming machine deployment machine health check", "MachineHealthCheck", mdMachineHealthCheckName(md.Name)) + log.Info("Resuming machine deployment machine health check", "MachineHealthCheck", mdMachineHealthCheckName(md.ObjectMeta.Name)) if err := resumeMachineHealthCheck(ctx, mhc, mhcPatchHelper); err != nil { return ctrl.Result{}, fmt.Errorf("updating annotations for machine health check: %v", err) } @@ -166,19 +171,18 @@ func (r *MachineDeploymentReconciler) reconcile(ctx context.Context, log logr.Lo return ctrl.Result{}, fmt.Errorf("generating MachineDeploymentUpgrade: %v", err) } - log.Info("Pausing machine deployment machine health check", "MachineHealthCheck", mdMachineHealthCheckName(md.Name)) + log.Info("Pausing machine deployment machine health check", "MachineHealthCheck", mdMachineHealthCheckName(md.ObjectMeta.Name)) if err := pauseMachineHealthCheck(ctx, mhc, mhcPatchHelper); err != nil { return ctrl.Result{}, fmt.Errorf("updating annotations for machine health check: %v", err) } if err := r.client.Create(ctx, mdUpgrade); client.IgnoreAlreadyExists(err) != nil { - return ctrl.Result{}, fmt.Errorf("failed to create MachineDeploymentUpgrade for MachineDeployment %s: %v", md.Name, err) + return ctrl.Result{}, fmt.Errorf("failed to create MachineDeploymentUpgrade for MachineDeployment %s: %v", md.ObjectMeta.Name, err) } return ctrl.Result{}, nil } - return ctrl.Result{}, fmt.Errorf("getting MachineDeploymentUpgrade for MachineDeployment %s: %v", md.Name, err) - + return ctrl.Result{}, fmt.Errorf("getting MachineDeploymentUpgrade for MachineDeployment %s: %v", md.ObjectMeta.Name, err) } func (r *MachineDeploymentReconciler) inPlaceUpgradeNeeded(md *clusterv1.MachineDeployment) bool { @@ -186,12 +190,12 @@ func (r *MachineDeploymentReconciler) inPlaceUpgradeNeeded(md *clusterv1.Machine } func (r *MachineDeploymentReconciler) machinesToUpgrade(ctx context.Context, md *clusterv1.MachineDeployment) ([]*clusterv1.Machine, []corev1.ObjectReference, error) { - selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{workerMachineLabel: md.Name}}) + selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{workerMachineLabel: md.ObjectMeta.Name}}) if err != nil { return nil, nil, err } machineList := &clusterv1.MachineList{} - if err := r.client.List(ctx, machineList, &client.ListOptions{LabelSelector: selector, Namespace: md.Namespace}); err != nil { + if err := r.client.List(ctx, machineList, &client.ListOptions{LabelSelector: selector, Namespace: md.ObjectMeta.Namespace}); err != nil { return nil, nil, err } machines := collections.FromMachineList(machineList).SortedByCreationTimestamp() @@ -215,20 +219,20 @@ func machineDeploymentUpgrade(md *clusterv1.MachineDeployment, machines []corev1 } return &anywherev1.MachineDeploymentUpgrade{ ObjectMeta: metav1.ObjectMeta{ - Name: mdUpgradeName(md.Name), + Name: mdUpgradeName(md.ObjectMeta.Name), Namespace: constants.EksaSystemNamespace, OwnerReferences: []metav1.OwnerReference{{ - APIVersion: md.APIVersion, - Kind: md.Kind, - Name: md.Name, - UID: md.UID, + APIVersion: clusterv1.GroupVersion.String(), + Kind: machineDeploymentKind, + Name: md.ObjectMeta.Name, + UID: md.ObjectMeta.UID, }}, }, Spec: anywherev1.MachineDeploymentUpgradeSpec{ MachineDeployment: corev1.ObjectReference{ - Kind: md.Kind, - Namespace: md.Namespace, - Name: md.Name, + Kind: machineDeploymentKind, + Namespace: md.ObjectMeta.Namespace, + Name: md.ObjectMeta.Name, }, KubernetesVersion: *md.Spec.Template.Spec.Version, MachinesRequireUpgrade: machines, diff --git a/controllers/machinedeployment_controller_test.go b/controllers/machinedeployment_controller_test.go index b14a63da3c8a..e4307e289372 100644 --- a/controllers/machinedeployment_controller_test.go +++ b/controllers/machinedeployment_controller_test.go @@ -31,7 +31,7 @@ type mdObjects struct { func TestMDSetupWithManager(t *testing.T) { client := env.Client() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) g := NewWithT(t) g.Expect(r.SetupWithManager(env.Manager())).To(Succeed()) @@ -46,7 +46,7 @@ func TestMDReconcileNotNeeded(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.md, mdObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -64,7 +64,7 @@ func TestMDReconcile(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.machine, mdObjs.mdUpgrade, mdObjs.md, mdObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -86,7 +86,7 @@ func TestMDReconcileCreateMachineDeploymentUpgrade(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.machine, mdObjs.md, mdObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -114,7 +114,7 @@ func TestMDReconcileMDAndMachineDeploymentUpgradeReady(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.machine, mdObjs.md, mdObjs.mdUpgrade, mdObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -142,7 +142,7 @@ func TestMDReconcileFullFlow(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.machine, mdObjs.md, mdObjs.mhc} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).ToNot(HaveOccurred()) @@ -204,7 +204,7 @@ func TestMDReconcileNotFound(t *testing.T) { mdObjs := getObjectsForMD() client := fake.NewClientBuilder().WithRuntimeObjects().Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("machinedeployments.cluster.x-k8s.io \"my-cluster\" not found")) @@ -217,7 +217,7 @@ func TestMDReconcileMHCNotFound(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.machine, mdObjs.md} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("machinehealthchecks.cluster.x-k8s.io \"my-cluster-worker-unhealthy\" not found")) @@ -232,7 +232,7 @@ func TestMDReconcileVersionMissing(t *testing.T) { runtimeObjs := []runtime.Object{mdObjs.md} client := fake.NewClientBuilder().WithRuntimeObjects(runtimeObjs...).Build() - r := controllers.NewMachineDeploymentReconciler(client) + r := controllers.NewMachineDeploymentReconciler(client, client) req := mdRequest(mdObjs.md) _, err := r.Reconcile(ctx, req) g.Expect(err).To(MatchError("unable to retrieve kubernetes version from MachineDeployment \"my-cluster\""))