From 32a3f09f5ad699fa950bfa3344de887db24aa992 Mon Sep 17 00:00:00 2001 From: killianmuldoon Date: Wed, 20 Sep 2023 16:56:46 +0100 Subject: [PATCH] Remove VsphereFailureDomain from context Signed-off-by: killianmuldoon --- .../vspheredeploymentzone_controller.go | 22 ++++--- ...vspheredeploymentzone_controller_domain.go | 40 ++++++------- ...redeploymentzone_controller_domain_test.go | 57 +++++++++---------- .../vspheredeploymentzone_controller_test.go | 30 ++++------ pkg/context/vspheredeploymentzone_context.go | 5 -- pkg/taggable/get.go | 5 +- 6 files changed, 70 insertions(+), 89 deletions(-) diff --git a/controllers/vspheredeploymentzone_controller.go b/controllers/vspheredeploymentzone_controller.go index 0b2cb0a466..e2adf3cd7f 100644 --- a/controllers/vspheredeploymentzone_controller.go +++ b/controllers/vspheredeploymentzone_controller.go @@ -156,8 +156,7 @@ func (r vsphereDeploymentZoneReconciler) reconcileNormal(deploymentZoneCtx *capv if err := r.Client.Get(deploymentZoneCtx, failureDomainKey, failureDomain); err != nil { return errors.Wrap(err, "VSphereFailureDomain could not be retrieved") } - deploymentZoneCtx.VSphereFailureDomain = failureDomain - authSession, err := r.getVCenterSession(deploymentZoneCtx) + authSession, err := r.getVCenterSession(deploymentZoneCtx, failureDomain.Spec.Topology.Datacenter) if err != nil { deploymentZoneCtx.Logger.V(4).Error(err, "unable to create session") conditions.MarkFalse(deploymentZoneCtx.VSphereDeploymentZone, infrav1.VCenterAvailableCondition, infrav1.VCenterUnreachableReason, clusterv1.ConditionSeverityError, err.Error()) @@ -174,7 +173,7 @@ func (r vsphereDeploymentZoneReconciler) reconcileNormal(deploymentZoneCtx *capv conditions.MarkTrue(deploymentZoneCtx.VSphereDeploymentZone, infrav1.PlacementConstraintMetCondition) // reconcile the failure domain - if err := r.reconcileFailureDomain(deploymentZoneCtx); err != nil { + if err := r.reconcileFailureDomain(deploymentZoneCtx, failureDomain); err != nil { deploymentZoneCtx.Logger.V(4).Error(err, "failed to reconcile failure domain", "failureDomain", deploymentZoneCtx.VSphereDeploymentZone.Spec.FailureDomain) deploymentZoneCtx.VSphereDeploymentZone.Status.Ready = pointer.Bool(false) return errors.Wrapf(err, "failed to reconcile failure domain") @@ -206,10 +205,10 @@ func (r vsphereDeploymentZoneReconciler) reconcilePlacementConstraint(deployment return nil } -func (r vsphereDeploymentZoneReconciler) getVCenterSession(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) (*session.Session, error) { +func (r vsphereDeploymentZoneReconciler) getVCenterSession(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, datacenter string) (*session.Session, error) { params := session.NewParams(). WithServer(deploymentZoneCtx.VSphereDeploymentZone.Spec.Server). - WithDatacenter(deploymentZoneCtx.VSphereFailureDomain.Spec.Topology.Datacenter). + WithDatacenter(datacenter). WithUserInfo(r.ControllerContext.Username, r.ControllerContext.Password). WithFeatures(session.Feature{ EnableKeepAlive: r.EnableKeepAlive, @@ -276,11 +275,10 @@ func (r vsphereDeploymentZoneReconciler) reconcileDelete(deploymentZoneCtx *capv } return err } - deploymentZoneCtx.VSphereFailureDomain = failureDomain // Reconcile the deletion of the VSphereFailureDomain by removing ownerReferences and deleting if necessary. - if err := updateOwnerReferences(deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain, r.Client, func() []metav1.OwnerReference { - return clusterutilv1.RemoveOwnerRef(deploymentZoneCtx.VSphereFailureDomain.OwnerReferences, metav1.OwnerReference{ + if err := updateOwnerReferences(deploymentZoneCtx, failureDomain, r.Client, func() []metav1.OwnerReference { + return clusterutilv1.RemoveOwnerRef(failureDomain.OwnerReferences, metav1.OwnerReference{ APIVersion: infrav1.GroupVersion.String(), Kind: deploymentZoneCtx.VSphereDeploymentZone.Kind, Name: deploymentZoneCtx.VSphereDeploymentZone.Name, @@ -289,10 +287,10 @@ func (r vsphereDeploymentZoneReconciler) reconcileDelete(deploymentZoneCtx *capv return err } - if len(deploymentZoneCtx.VSphereFailureDomain.OwnerReferences) == 0 { - deploymentZoneCtx.Logger.Info("deleting VSphereFailureDomain", "name", deploymentZoneCtx.VSphereFailureDomain.Name) - if err := r.Client.Delete(deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain); err != nil && !apierrors.IsNotFound(err) { - return errors.Wrapf(err, "failed to delete VSphereFailureDomain %s", deploymentZoneCtx.VSphereFailureDomain.Name) + if len(failureDomain.OwnerReferences) == 0 { + deploymentZoneCtx.Logger.Info("deleting VSphereFailureDomain", "name", failureDomain.Name) + if err := r.Client.Delete(deploymentZoneCtx, failureDomain); err != nil && !apierrors.IsNotFound(err) { + return errors.Wrapf(err, "failed to delete VSphereFailureDomain %s", failureDomain.Name) } } diff --git a/controllers/vspheredeploymentzone_controller_domain.go b/controllers/vspheredeploymentzone_controller_domain.go index 5fbc6e2f48..d9a380f098 100644 --- a/controllers/vspheredeploymentzone_controller_domain.go +++ b/controllers/vspheredeploymentzone_controller_domain.go @@ -33,40 +33,40 @@ import ( "sigs.k8s.io/cluster-api-provider-vsphere/pkg/taggable" ) -func (r vsphereDeploymentZoneReconciler) reconcileFailureDomain(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) error { - logger := ctrl.LoggerFrom(deploymentZoneCtx).WithValues("VSphereFailureDomain", klog.KObj(deploymentZoneCtx.VSphereFailureDomain)) +func (r vsphereDeploymentZoneReconciler) reconcileFailureDomain(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain) error { + logger := ctrl.LoggerFrom(deploymentZoneCtx).WithValues("VSphereFailureDomain", klog.KObj(vsphereFailureDomain)) // verify the failure domain for the region - if err := r.reconcileInfraFailureDomain(deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain.Spec.Region); err != nil { + if err := r.reconcileInfraFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Region); err != nil { conditions.MarkFalse(deploymentZoneCtx.VSphereDeploymentZone, infrav1.VSphereFailureDomainValidatedCondition, infrav1.RegionMisconfiguredReason, clusterv1.ConditionSeverityError, err.Error()) logger.Error(err, "region is not configured correctly") return errors.Wrapf(err, "region is not configured correctly") } // verify the failure domain for the zone - if err := r.reconcileInfraFailureDomain(deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain.Spec.Zone); err != nil { + if err := r.reconcileInfraFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone); err != nil { conditions.MarkFalse(deploymentZoneCtx.VSphereDeploymentZone, infrav1.VSphereFailureDomainValidatedCondition, infrav1.ZoneMisconfiguredReason, clusterv1.ConditionSeverityError, err.Error()) logger.Error(err, "zone is not configured correctly") return errors.Wrapf(err, "zone is not configured correctly") } - if computeCluster := deploymentZoneCtx.VSphereFailureDomain.Spec.Topology.ComputeCluster; computeCluster != nil { - if err := r.reconcileComputeCluster(deploymentZoneCtx); err != nil { + if computeCluster := vsphereFailureDomain.Spec.Topology.ComputeCluster; computeCluster != nil { + if err := r.reconcileComputeCluster(deploymentZoneCtx, vsphereFailureDomain); err != nil { logger.Error(err, "compute cluster is not configured correctly", "name", *computeCluster) return errors.Wrap(err, "compute cluster is not configured correctly") } } - if err := r.reconcileTopology(deploymentZoneCtx); err != nil { + if err := r.reconcileTopology(deploymentZoneCtx, vsphereFailureDomain); err != nil { logger.Error(err, "topology is not configured correctly") return errors.Wrap(err, "topology is not configured correctly") } // Ensure the VSphereDeploymentZone is marked as an owner of the VSphereFailureDomain. - if err := updateOwnerReferences(deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain, r.Client, + if err := updateOwnerReferences(deploymentZoneCtx, vsphereFailureDomain, r.Client, func() []metav1.OwnerReference { return clusterutilv1.EnsureOwnerRef( - deploymentZoneCtx.VSphereFailureDomain.OwnerReferences, + vsphereFailureDomain.OwnerReferences, metav1.OwnerReference{ APIVersion: infrav1.GroupVersion.String(), Kind: deploymentZoneCtx.VSphereDeploymentZone.Kind, @@ -82,15 +82,15 @@ func (r vsphereDeploymentZoneReconciler) reconcileFailureDomain(deploymentZoneCt return nil } -func (r vsphereDeploymentZoneReconciler) reconcileInfraFailureDomain(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, failureDomain infrav1.FailureDomain) error { +func (r vsphereDeploymentZoneReconciler) reconcileInfraFailureDomain(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain, failureDomain infrav1.FailureDomain) error { if *failureDomain.AutoConfigure { - return r.createAndAttachMetadata(deploymentZoneCtx, failureDomain) + return r.createAndAttachMetadata(deploymentZoneCtx, vsphereFailureDomain, failureDomain) } - return r.verifyFailureDomain(deploymentZoneCtx, failureDomain) + return r.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, failureDomain) } -func (r vsphereDeploymentZoneReconciler) reconcileTopology(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) error { - topology := deploymentZoneCtx.VSphereFailureDomain.Spec.Topology +func (r vsphereDeploymentZoneReconciler) reconcileTopology(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain) error { + topology := vsphereFailureDomain.Spec.Topology if datastore := topology.Datastore; datastore != "" { if _, err := deploymentZoneCtx.AuthSession.Finder.Datastore(deploymentZoneCtx, datastore); err != nil { conditions.MarkFalse(deploymentZoneCtx.VSphereDeploymentZone, infrav1.VSphereFailureDomainValidatedCondition, infrav1.DatastoreNotFoundReason, clusterv1.ConditionSeverityError, "datastore %s is misconfigured", datastore) @@ -121,8 +121,8 @@ func (r vsphereDeploymentZoneReconciler) reconcileTopology(deploymentZoneCtx *ca return nil } -func (r vsphereDeploymentZoneReconciler) reconcileComputeCluster(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) error { - computeCluster := deploymentZoneCtx.VSphereFailureDomain.Spec.Topology.ComputeCluster +func (r vsphereDeploymentZoneReconciler) reconcileComputeCluster(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain) error { + computeCluster := vsphereFailureDomain.Spec.Topology.ComputeCluster if computeCluster == nil { return nil } @@ -154,12 +154,12 @@ func (r vsphereDeploymentZoneReconciler) reconcileComputeCluster(deploymentZoneC // verifyFailureDomain verifies the Failure Domain. It verifies the existence of tag and category specified and // checks whether the specified tags exist on the DataCenter or Compute Cluster or Hosts (in a HostGroup). -func (r vsphereDeploymentZoneReconciler) verifyFailureDomain(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, failureDomain infrav1.FailureDomain) error { +func (r vsphereDeploymentZoneReconciler) verifyFailureDomain(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain, failureDomain infrav1.FailureDomain) error { if _, err := deploymentZoneCtx.AuthSession.TagManager.GetTagForCategory(deploymentZoneCtx, failureDomain.Name, failureDomain.TagCategory); err != nil { return errors.Wrapf(err, "failed to verify tag %s and category %s", failureDomain.Name, failureDomain.TagCategory) } - objects, err := taggable.GetObjects(deploymentZoneCtx, failureDomain.Type) + objects, err := taggable.GetObjects(deploymentZoneCtx, vsphereFailureDomain, failureDomain.Type) if err != nil { return errors.Wrapf(err, "failed to find object") } @@ -177,7 +177,7 @@ func (r vsphereDeploymentZoneReconciler) verifyFailureDomain(deploymentZoneCtx * return nil } -func (r vsphereDeploymentZoneReconciler) createAndAttachMetadata(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, failureDomain infrav1.FailureDomain) error { +func (r vsphereDeploymentZoneReconciler) createAndAttachMetadata(deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain, failureDomain infrav1.FailureDomain) error { logger := ctrl.LoggerFrom(deploymentZoneCtx, "tag", failureDomain.Name, "category", failureDomain.TagCategory) categoryID, err := metadata.CreateCategory(deploymentZoneCtx, failureDomain.TagCategory, failureDomain.Type) if err != nil { @@ -191,7 +191,7 @@ func (r vsphereDeploymentZoneReconciler) createAndAttachMetadata(deploymentZoneC } logger = logger.WithValues("type", failureDomain.Type) - objects, err := taggable.GetObjects(deploymentZoneCtx, failureDomain.Type) + objects, err := taggable.GetObjects(deploymentZoneCtx, vsphereFailureDomain, failureDomain.Type) if err != nil { logger.V(4).Error(err, "failed to find object") return err diff --git a/controllers/vspheredeploymentzone_controller_domain_test.go b/controllers/vspheredeploymentzone_controller_domain_test.go index bce0507249..5fa3b472b8 100644 --- a/controllers/vspheredeploymentzone_controller_domain_test.go +++ b/controllers/vspheredeploymentzone_controller_domain_test.go @@ -87,31 +87,30 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_ComputeCl } deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ - ControllerContext: controllerCtx, - VSphereFailureDomain: vsphereFailureDomain, - Logger: logr.Discard(), - AuthSession: authSession, + ControllerContext: controllerCtx, + Logger: logr.Discard(), + AuthSession: authSession, } reconciler := vsphereDeploymentZoneReconciler{controllerCtx} - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Region)).To(Succeed()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Region)).To(Succeed()) stdout := gbytes.NewBuffer() g.Expect(simr.Run("tags.attached.ls k8s-region-west", stdout)).To(Succeed()) g.Expect(stdout).Should(gbytes.Say("Datacenter")) - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(Succeed()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(Succeed()) stdout = gbytes.NewBuffer() g.Expect(simr.Run("tags.attached.ls k8s-region-west-2", stdout)).To(Succeed()) g.Expect(stdout).Should(gbytes.Say("ClusterComputeResource")) vsphereFailureDomain.Spec.Topology.ComputeCluster = pointer.String("DC0_C1") // Since association is verified, the method errors since the tag is not associated to the object. - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) // Since the tag does not belong to the category vsphereFailureDomain.Spec.Zone.TagCategory = "diff-k8s-region" - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) } func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_HostGroupZone(t *testing.T) { @@ -171,29 +170,28 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_HostGroup } deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ - ControllerContext: controllerCtx, - VSphereFailureDomain: vsphereFailureDomain, - Logger: logr.Discard(), - AuthSession: authSession, + ControllerContext: controllerCtx, + Logger: logr.Discard(), + AuthSession: authSession, } reconciler := vsphereDeploymentZoneReconciler{controllerCtx} // Fails since no hosts are tagged - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) stdout := gbytes.NewBuffer() g.Expect(simr.Run("tags.attach k8s-region-west-2 /DC0/host/DC0_C0/DC0_C0_H0", stdout)).To(Succeed()) // Fails as not all hosts are tagged - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) g.Expect(simr.Run("tags.attach k8s-region-west-2 /DC0/host/DC0_C0/DC0_C0_H1", stdout)).To(Succeed()) // Succeeds as all hosts are tagged - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(Succeed()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(Succeed()) // Since the tag does not belong to the category vsphereFailureDomain.Spec.Zone.TagCategory = "diff-k8s-region" - g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(deploymentZoneCtx, vsphereFailureDomain, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) } func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *testing.T) { @@ -282,13 +280,12 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te } deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ - ControllerContext: controllerCtx, - VSphereFailureDomain: vsphereFailureDomain, - Logger: logr.Discard(), - AuthSession: authSession, + ControllerContext: controllerCtx, + Logger: logr.Discard(), + AuthSession: authSession, } - g.Expect(reconciler.createAndAttachMetadata(deploymentZoneCtx, tests[0].vsphereFailureDomainSpec.Region)).NotTo(HaveOccurred()) + g.Expect(reconciler.createAndAttachMetadata(deploymentZoneCtx, vsphereFailureDomain, tests[0].vsphereFailureDomainSpec.Region)).NotTo(HaveOccurred()) stdout := gbytes.NewBuffer() g.Expect(simr.Run("tags.category.info k8s-region", stdout)).To(Succeed()) g.Expect(stdout).To(gbytes.Say("k8s-region")) @@ -303,13 +300,12 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te } deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ - ControllerContext: controllerCtx, - VSphereFailureDomain: vsphereFailureDomain, - Logger: logr.Discard(), - AuthSession: authSession, + ControllerContext: controllerCtx, + Logger: logr.Discard(), + AuthSession: authSession, } - g.Expect(reconciler.createAndAttachMetadata(deploymentZoneCtx, tests[1].vsphereFailureDomainSpec.Zone)).NotTo(HaveOccurred()) + g.Expect(reconciler.createAndAttachMetadata(deploymentZoneCtx, vsphereFailureDomain, tests[1].vsphereFailureDomainSpec.Zone)).NotTo(HaveOccurred()) stdout := gbytes.NewBuffer() g.Expect(simr.Run("tags.category.info k8s-zone", stdout)).To(Succeed()) g.Expect(stdout).To(gbytes.Say("k8s-zone")) @@ -324,13 +320,12 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te } deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ - ControllerContext: controllerCtx, - VSphereFailureDomain: vsphereFailureDomain, - Logger: logr.Discard(), - AuthSession: authSession, + ControllerContext: controllerCtx, + Logger: logr.Discard(), + AuthSession: authSession, } - g.Expect(reconciler.createAndAttachMetadata(deploymentZoneCtx, tests[2].vsphereFailureDomainSpec.Zone)).NotTo(HaveOccurred()) + g.Expect(reconciler.createAndAttachMetadata(deploymentZoneCtx, vsphereFailureDomain, tests[2].vsphereFailureDomainSpec.Zone)).NotTo(HaveOccurred()) stdout := gbytes.NewBuffer() g.Expect(simr.Run("tags.category.info foo", stdout)).To(Succeed()) g.Expect(stdout).To(gbytes.Say("foo")) diff --git a/controllers/vspheredeploymentzone_controller_test.go b/controllers/vspheredeploymentzone_controller_test.go index 8458a49ff3..b783e9ec71 100644 --- a/controllers/vspheredeploymentzone_controller_test.go +++ b/controllers/vspheredeploymentzone_controller_test.go @@ -497,12 +497,11 @@ func TestVSphereDeploymentZone_Reconcile(t *testing.T) { g.Consistently(func(g Gomega) bool { g.Expect(testEnv.Get(ctx, client.ObjectKeyFromObject(vsphereDeploymentZone), vsphereDeploymentZone)).To(Succeed()) - return vsphereDeploymentZone.Status.Ready != nil && !*vsphereDeploymentZone.Status.Ready }, timeout).Should(BeFalse()) }) - t.Run("it should delete the VSphereDeploymentZone when the associated VSphereFailureDomain does not exist", func(t *testing.T) { + t.Run("Delete the VSphereDeploymentZone when the associated VSphereFailureDomain does not exist", func(t *testing.T) { g := NewWithT(t) vsphereDeploymentZone := &infrav1.VSphereDeploymentZone{ @@ -620,6 +619,17 @@ func TestVsphereDeploymentZone_Failed_ReconcilePlacementConstraint(t *testing.T) mgmtContext.Password = pass controllerCtx := fake.NewControllerContext(mgmtContext) + Expect(controllerCtx.Client.Create(controllerCtx, &infrav1.VSphereFailureDomain{ + ObjectMeta: metav1.ObjectMeta{ + Name: "blah", + }, + Spec: infrav1.VSphereFailureDomainSpec{ + Topology: infrav1.Topology{ + Datacenter: "DC0", + ComputeCluster: pointer.String("DC0_C0"), + }, + }, + })).To(Succeed()) deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, @@ -629,17 +639,6 @@ func TestVsphereDeploymentZone_Failed_ReconcilePlacementConstraint(t *testing.T) ControlPlane: pointer.Bool(true), PlacementConstraint: tt.placementConstraint, }}, - VSphereFailureDomain: &infrav1.VSphereFailureDomain{ - ObjectMeta: metav1.ObjectMeta{ - Name: "blah", - }, - Spec: infrav1.VSphereFailureDomainSpec{ - Topology: infrav1.Topology{ - Datacenter: "DC0", - ComputeCluster: pointer.String("DC0_C0"), - }, - }, - }, Logger: logr.Discard(), } @@ -687,7 +686,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -709,7 +707,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -728,7 +725,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -745,7 +741,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -769,7 +764,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } diff --git a/pkg/context/vspheredeploymentzone_context.go b/pkg/context/vspheredeploymentzone_context.go index c93ffe6e5a..36f80ffd15 100644 --- a/pkg/context/vspheredeploymentzone_context.go +++ b/pkg/context/vspheredeploymentzone_context.go @@ -30,7 +30,6 @@ import ( type VSphereDeploymentZoneContext struct { *ControllerContext VSphereDeploymentZone *infrav1.VSphereDeploymentZone - VSphereFailureDomain *infrav1.VSphereFailureDomain Logger logr.Logger PatchHelper *patch.Helper AuthSession *session.Session @@ -54,7 +53,3 @@ func (c *VSphereDeploymentZoneContext) String() string { func (c *VSphereDeploymentZoneContext) GetSession() *session.Session { return c.AuthSession } - -func (c *VSphereDeploymentZoneContext) GetVsphereFailureDomain() infrav1.VSphereFailureDomain { - return *c.VSphereFailureDomain -} diff --git a/pkg/taggable/get.go b/pkg/taggable/get.go index 40dc2833bf..56ea21fa15 100644 --- a/pkg/taggable/get.go +++ b/pkg/taggable/get.go @@ -29,11 +29,10 @@ import ( type taggableContext interface { context.Context GetSession() *session.Session - GetVsphereFailureDomain() infrav1.VSphereFailureDomain } -func GetObjects(ctx taggableContext, fdType infrav1.FailureDomainType) (Objects, error) { - finderFunc := find.ObjectFunc(fdType, ctx.GetVsphereFailureDomain().Spec.Topology, ctx.GetSession().Finder) +func GetObjects(ctx taggableContext, failureDomain *infrav1.VSphereFailureDomain, fdType infrav1.FailureDomainType) (Objects, error) { + finderFunc := find.ObjectFunc(fdType, failureDomain.Spec.Topology, ctx.GetSession().Finder) objRefs, err := finderFunc(ctx) if err != nil { return nil, err