From 71c9d676d7e85b8ad51692d318e484fa4cf48fce 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 | 25 ++++---- ...vspheredeploymentzone_controller_domain.go | 40 ++++++------- ...redeploymentzone_controller_domain_test.go | 57 +++++++++---------- .../vspheredeploymentzone_controller_test.go | 30 ++++------ pkg/context/vspheredeploymentzone_context.go | 8 +-- pkg/taggable/get.go | 5 +- 6 files changed, 72 insertions(+), 93 deletions(-) diff --git a/controllers/vspheredeploymentzone_controller.go b/controllers/vspheredeploymentzone_controller.go index dd1ee22803..703752b3e9 100644 --- a/controllers/vspheredeploymentzone_controller.go +++ b/controllers/vspheredeploymentzone_controller.go @@ -155,8 +155,7 @@ func (r vsphereDeploymentZoneReconciler) reconcileNormal(ctx context.Context, de if err := r.Client.Get(ctx, failureDomainKey, failureDomain); err != nil { return errors.Wrap(err, "VSphereFailureDomain could not be retrieved") } - deploymentZoneCtx.VSphereFailureDomain = failureDomain - authSession, err := r.getVCenterSession(ctx, deploymentZoneCtx) + authSession, err := r.getVCenterSession(ctx, 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()) @@ -173,7 +172,7 @@ func (r vsphereDeploymentZoneReconciler) reconcileNormal(ctx context.Context, de conditions.MarkTrue(deploymentZoneCtx.VSphereDeploymentZone, infrav1.PlacementConstraintMetCondition) // reconcile the failure domain - if err := r.reconcileFailureDomain(ctx, deploymentZoneCtx); err != nil { + if err := r.reconcileFailureDomain(ctx, 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") @@ -205,10 +204,10 @@ func (r vsphereDeploymentZoneReconciler) reconcilePlacementConstraint(ctx contex return nil } -func (r vsphereDeploymentZoneReconciler) getVCenterSession(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) (*session.Session, error) { +func (r vsphereDeploymentZoneReconciler) getVCenterSession(ctx context.Context, 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, @@ -259,8 +258,7 @@ func (r vsphereDeploymentZoneReconciler) reconcileDelete(ctx context.Context, de }) if len(machinesUsingDeploymentZone) > 0 { machineNamesStr := util.MachinesAsString(machinesUsingDeploymentZone.SortedByCreationTimestamp()) - err := errors.Errorf("error deleting VSphereDeploymentZone: %s is currently in use by machines: %s", deploymentZoneCtx.VSphereDeploymentZone.Name, machineNamesStr) - return err + return errors.Errorf("error deleting VSphereDeploymentZone: %s is currently in use by machines: %s", deploymentZoneCtx.VSphereDeploymentZone.Name, machineNamesStr) } failureDomain := &infrav1.VSphereFailureDomain{} @@ -275,11 +273,10 @@ func (r vsphereDeploymentZoneReconciler) reconcileDelete(ctx context.Context, de } 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(ctx, 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, @@ -288,10 +285,10 @@ func (r vsphereDeploymentZoneReconciler) reconcileDelete(ctx context.Context, de return err } - if len(deploymentZoneCtx.VSphereFailureDomain.OwnerReferences) == 0 { - deploymentZoneCtx.Logger.Info("deleting VSphereFailureDomain", "name", deploymentZoneCtx.VSphereFailureDomain.Name) - if err := r.Client.Delete(ctx, 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(ctx, 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 15da22a6b9..3d088475f5 100644 --- a/controllers/vspheredeploymentzone_controller_domain.go +++ b/controllers/vspheredeploymentzone_controller_domain.go @@ -35,40 +35,40 @@ import ( "sigs.k8s.io/cluster-api-provider-vsphere/pkg/taggable" ) -func (r vsphereDeploymentZoneReconciler) reconcileFailureDomain(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) error { - logger := ctrl.LoggerFrom(ctx).WithValues("VSphereFailureDomain", klog.KObj(deploymentZoneCtx.VSphereFailureDomain)) +func (r vsphereDeploymentZoneReconciler) reconcileFailureDomain(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain) error { + logger := ctrl.LoggerFrom(ctx).WithValues("VSphereFailureDomain", klog.KObj(vsphereFailureDomain)) // verify the failure domain for the region - if err := r.reconcileInfraFailureDomain(ctx, deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain.Spec.Region); err != nil { + if err := r.reconcileInfraFailureDomain(ctx, 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(ctx, deploymentZoneCtx, deploymentZoneCtx.VSphereFailureDomain.Spec.Zone); err != nil { + if err := r.reconcileInfraFailureDomain(ctx, 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(ctx, deploymentZoneCtx); err != nil { + if computeCluster := vsphereFailureDomain.Spec.Topology.ComputeCluster; computeCluster != nil { + if err := r.reconcileComputeCluster(ctx, 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(ctx, deploymentZoneCtx); err != nil { + if err := r.reconcileTopology(ctx, 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(ctx, deploymentZoneCtx.VSphereFailureDomain, r.Client, + if err := updateOwnerReferences(ctx, vsphereFailureDomain, r.Client, func() []metav1.OwnerReference { return clusterutilv1.EnsureOwnerRef( - deploymentZoneCtx.VSphereFailureDomain.OwnerReferences, + vsphereFailureDomain.OwnerReferences, metav1.OwnerReference{ APIVersion: infrav1.GroupVersion.String(), Kind: deploymentZoneCtx.VSphereDeploymentZone.Kind, @@ -84,15 +84,15 @@ func (r vsphereDeploymentZoneReconciler) reconcileFailureDomain(ctx context.Cont return nil } -func (r vsphereDeploymentZoneReconciler) reconcileInfraFailureDomain(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, failureDomain infrav1.FailureDomain) error { +func (r vsphereDeploymentZoneReconciler) reconcileInfraFailureDomain(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain, failureDomain infrav1.FailureDomain) error { if *failureDomain.AutoConfigure { //nolint:staticcheck - return r.createAndAttachMetadata(ctx, deploymentZoneCtx, failureDomain) + return r.createAndAttachMetadata(ctx, deploymentZoneCtx, vsphereFailureDomain, failureDomain) } - return r.verifyFailureDomain(ctx, deploymentZoneCtx, failureDomain) + return r.verifyFailureDomain(ctx, deploymentZoneCtx, vsphereFailureDomain, failureDomain) } -func (r vsphereDeploymentZoneReconciler) reconcileTopology(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) error { - topology := deploymentZoneCtx.VSphereFailureDomain.Spec.Topology +func (r vsphereDeploymentZoneReconciler) reconcileTopology(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain) error { + topology := vsphereFailureDomain.Spec.Topology if datastore := topology.Datastore; datastore != "" { if _, err := deploymentZoneCtx.AuthSession.Finder.Datastore(ctx, datastore); err != nil { conditions.MarkFalse(deploymentZoneCtx.VSphereDeploymentZone, infrav1.VSphereFailureDomainValidatedCondition, infrav1.DatastoreNotFoundReason, clusterv1.ConditionSeverityError, "datastore %s is misconfigured", datastore) @@ -123,8 +123,8 @@ func (r vsphereDeploymentZoneReconciler) reconcileTopology(ctx context.Context, return nil } -func (r vsphereDeploymentZoneReconciler) reconcileComputeCluster(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext) error { - computeCluster := deploymentZoneCtx.VSphereFailureDomain.Spec.Topology.ComputeCluster +func (r vsphereDeploymentZoneReconciler) reconcileComputeCluster(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain) error { + computeCluster := vsphereFailureDomain.Spec.Topology.ComputeCluster if computeCluster == nil { return nil } @@ -156,12 +156,12 @@ func (r vsphereDeploymentZoneReconciler) reconcileComputeCluster(ctx context.Con // 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(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, failureDomain infrav1.FailureDomain) error { +func (r vsphereDeploymentZoneReconciler) verifyFailureDomain(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain, failureDomain infrav1.FailureDomain) error { if _, err := deploymentZoneCtx.AuthSession.TagManager.GetTagForCategory(ctx, 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(ctx, deploymentZoneCtx, failureDomain.Type) + objects, err := taggable.GetObjects(ctx, deploymentZoneCtx, vsphereFailureDomain, failureDomain.Type) if err != nil { return errors.Wrapf(err, "failed to find object") } @@ -179,7 +179,7 @@ func (r vsphereDeploymentZoneReconciler) verifyFailureDomain(ctx context.Context return nil } -func (r vsphereDeploymentZoneReconciler) createAndAttachMetadata(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, failureDomain infrav1.FailureDomain) error { +func (r vsphereDeploymentZoneReconciler) createAndAttachMetadata(ctx context.Context, deploymentZoneCtx *capvcontext.VSphereDeploymentZoneContext, vsphereFailureDomain *infrav1.VSphereFailureDomain, failureDomain infrav1.FailureDomain) error { logger := ctrl.LoggerFrom(ctx, "tag", failureDomain.Name, "category", failureDomain.TagCategory) categoryID, err := metadata.CreateCategory(ctx, deploymentZoneCtx, failureDomain.TagCategory, failureDomain.Type) if err != nil { @@ -193,7 +193,7 @@ func (r vsphereDeploymentZoneReconciler) createAndAttachMetadata(ctx context.Con } logger = logger.WithValues("type", failureDomain.Type) - objects, err := taggable.GetObjects(ctx, deploymentZoneCtx, failureDomain.Type) + objects, err := taggable.GetObjects(ctx, 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 8521062980..6fc93dffa2 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Region)).To(Succeed()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(Succeed()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(Succeed()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, vsphereFailureDomain.Spec.Zone)).To(HaveOccurred()) + g.Expect(reconciler.verifyFailureDomain(ctx, 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(ctx, deploymentZoneCtx, tests[0].vsphereFailureDomainSpec.Region)).NotTo(HaveOccurred()) + g.Expect(reconciler.createAndAttachMetadata(ctx, 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(ctx, deploymentZoneCtx, tests[1].vsphereFailureDomainSpec.Zone)).NotTo(HaveOccurred()) + g.Expect(reconciler.createAndAttachMetadata(ctx, 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(ctx, deploymentZoneCtx, tests[2].vsphereFailureDomainSpec.Zone)).NotTo(HaveOccurred()) + g.Expect(reconciler.createAndAttachMetadata(ctx, 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 5c69af5e48..5a74a72c1b 100644 --- a/controllers/vspheredeploymentzone_controller_test.go +++ b/controllers/vspheredeploymentzone_controller_test.go @@ -495,12 +495,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{ @@ -618,6 +617,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, @@ -627,17 +637,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(), } @@ -685,7 +684,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -707,7 +705,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -726,7 +723,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -743,7 +739,6 @@ func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) { deploymentZoneCtx := &capvcontext.VSphereDeploymentZoneContext{ ControllerContext: controllerCtx, VSphereDeploymentZone: vsphereDeploymentZone, - VSphereFailureDomain: vsphereFailureDomain, Logger: logr.Discard(), } @@ -767,7 +762,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 c1a62e51d3..9296b5fc9a 100644 --- a/pkg/context/vspheredeploymentzone_context.go +++ b/pkg/context/vspheredeploymentzone_context.go @@ -31,7 +31,6 @@ import ( type VSphereDeploymentZoneContext struct { *ControllerContext VSphereDeploymentZone *infrav1.VSphereDeploymentZone - VSphereFailureDomain *infrav1.VSphereFailureDomain Logger logr.Logger PatchHelper *patch.Helper AuthSession *session.Session @@ -57,9 +56,4 @@ func (c *VSphereDeploymentZoneContext) String() string { // GetSession returns the session for the VSphereDeploymentZoneContext. func (c *VSphereDeploymentZoneContext) GetSession() *session.Session { return c.AuthSession -} - -// GetVsphereFailureDomain returns the VSphereFailureDomain for the VSphereDeploymentZoneContext. -func (c *VSphereDeploymentZoneContext) GetVsphereFailureDomain() infrav1.VSphereFailureDomain { - return *c.VSphereFailureDomain -} +} \ No newline at end of file diff --git a/pkg/taggable/get.go b/pkg/taggable/get.go index d5e727f271..933c61b051 100644 --- a/pkg/taggable/get.go +++ b/pkg/taggable/get.go @@ -29,12 +29,11 @@ import ( type taggableContext interface { GetSession() *session.Session - GetVsphereFailureDomain() infrav1.VSphereFailureDomain } // GetObjects returns the objects for a given failure domain. -func GetObjects(ctx context.Context, taggableCtx taggableContext, fdType infrav1.FailureDomainType) (Objects, error) { - finderFunc := find.ObjectFunc(fdType, taggableCtx.GetVsphereFailureDomain().Spec.Topology, taggableCtx.GetSession().Finder) +func GetObjects(ctx context.Context, taggableCtx taggableContext, failureDomain *infrav1.VSphereFailureDomain, fdType infrav1.FailureDomainType) (Objects, error) { + finderFunc := find.ObjectFunc(fdType, failureDomain.Spec.Topology, taggableCtx.GetSession().Finder) objRefs, err := finderFunc(ctx) if err != nil { return nil, err