Skip to content

Commit

Permalink
Merge pull request #854 from bryanv/bryanv/vmsetrp-zone-watch
Browse files Browse the repository at this point in the history
🐛 Reconcile VM SetResourcePolicy because of Zone change
  • Loading branch information
bryanv authored Jan 10, 2025
2 parents 0936429 + d5ce029 commit 5bd4b7a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
topologyv1 "github.com/vmware-tanzu/vm-operator/external/tanzu-topology/api/v1alpha1"
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context"
"github.com/vmware-tanzu/vm-operator/pkg/patch"
Expand All @@ -41,9 +44,16 @@ func AddToManager(ctx *pkgctx.ControllerManagerContext, mgr manager.Manager) err
ctx.VMProvider,
)

return ctrl.NewControllerManagedBy(mgr).
For(controlledType).
Complete(r)
builder := ctrl.NewControllerManagedBy(mgr).
For(controlledType)

if pkgcfg.FromContext(ctx).Features.WorkloadDomainIsolation {
builder.Watches(
&topologyv1.Zone{},
handler.EnqueueRequestsFromMapFunc(zoneToNamespaceVMSRP(mgr.GetClient())))
}

return builder.Complete(r)
}

func NewReconciler(
Expand All @@ -59,6 +69,27 @@ func NewReconciler(
}
}

func zoneToNamespaceVMSRP(
c client.Client) func(context.Context, client.Object) []reconcile.Request {

return func(ctx context.Context, o client.Object) []reconcile.Request {
zone := o.(*topologyv1.Zone)

list := vmopv1.VirtualMachineSetResourcePolicyList{}
if err := c.List(ctx, &list, client.InNamespace(zone.Namespace)); err != nil {
return nil
}

var reconcileRequests []reconcile.Request
for i := range list.Items {
reconcileRequests = append(reconcileRequests, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&list.Items[i]),
})
}
return reconcileRequests
}
}

// Reconciler reconciles a VirtualMachineSetResourcePolicy object.
type Reconciler struct {
Context context.Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
topologyv1 "github.com/vmware-tanzu/vm-operator/external/tanzu-topology/api/v1alpha1"
"github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels"

"github.com/vmware-tanzu/vm-operator/test/builder"
)

Expand Down Expand Up @@ -102,6 +102,19 @@ func intgTestsReconcile() {
Eventually(called.Load).Should(BeTrue())
})

By("Reconcile when Zone is added", func() {
called.Store(false)

zone := &topologyv1.Zone{}
zone.Name = "my-new-zone"
zone.Namespace = ctx.Namespace
Expect(ctx.Client.Create(ctx, zone)).To(Succeed())

By("Reconcile again because of Zone create", func() {
Eventually(called.Load).Should(BeTrue())
})
})

By("Deleting the VirtualMachineSetResourcePolicy", func() {
err := ctx.Client.Delete(ctx, resourcePolicy)
Expect(err).ToNot(HaveOccurred())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import (
var intgFakeVMProvider = providerfake.NewVMProvider()

var suite = builder.NewTestSuiteForControllerWithContext(
pkgcfg.NewContextWithDefaultConfig(),
pkgcfg.UpdateContext(
pkgcfg.NewContextWithDefaultConfig(),
func(config *pkgcfg.Config) {
config.Features.WorkloadDomainIsolation = true
},
),
virtualmachinesetresourcepolicy.AddToManager,
func(ctx *pkgctx.ControllerManagerContext, _ ctrlmgr.Manager) error {
ctx.VMProvider = intgFakeVMProvider
Expand Down

0 comments on commit 5bd4b7a

Please sign in to comment.