Skip to content

Commit 5bd4b7a

Browse files
authored
Merge pull request #854 from bryanv/bryanv/vmsetrp-zone-watch
🐛 Reconcile VM SetResourcePolicy because of Zone change
2 parents 0936429 + d5ce029 commit 5bd4b7a

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

controllers/virtualmachinesetresourcepolicy/virtualmachinesetresourcepolicy_controller.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import (
1313
ctrl "sigs.k8s.io/controller-runtime"
1414
"sigs.k8s.io/controller-runtime/pkg/client"
1515
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
16+
"sigs.k8s.io/controller-runtime/pkg/handler"
1617
"sigs.k8s.io/controller-runtime/pkg/manager"
18+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1719

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

44-
return ctrl.NewControllerManagedBy(mgr).
45-
For(controlledType).
46-
Complete(r)
47+
builder := ctrl.NewControllerManagedBy(mgr).
48+
For(controlledType)
49+
50+
if pkgcfg.FromContext(ctx).Features.WorkloadDomainIsolation {
51+
builder.Watches(
52+
&topologyv1.Zone{},
53+
handler.EnqueueRequestsFromMapFunc(zoneToNamespaceVMSRP(mgr.GetClient())))
54+
}
55+
56+
return builder.Complete(r)
4757
}
4858

4959
func NewReconciler(
@@ -59,6 +69,27 @@ func NewReconciler(
5969
}
6070
}
6171

72+
func zoneToNamespaceVMSRP(
73+
c client.Client) func(context.Context, client.Object) []reconcile.Request {
74+
75+
return func(ctx context.Context, o client.Object) []reconcile.Request {
76+
zone := o.(*topologyv1.Zone)
77+
78+
list := vmopv1.VirtualMachineSetResourcePolicyList{}
79+
if err := c.List(ctx, &list, client.InNamespace(zone.Namespace)); err != nil {
80+
return nil
81+
}
82+
83+
var reconcileRequests []reconcile.Request
84+
for i := range list.Items {
85+
reconcileRequests = append(reconcileRequests, reconcile.Request{
86+
NamespacedName: client.ObjectKeyFromObject(&list.Items[i]),
87+
})
88+
}
89+
return reconcileRequests
90+
}
91+
}
92+
6293
// Reconciler reconciles a VirtualMachineSetResourcePolicy object.
6394
type Reconciler struct {
6495
Context context.Context

controllers/virtualmachinesetresourcepolicy/virtualmachinesetresourcepolicy_controller_intg_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"sigs.k8s.io/controller-runtime/pkg/client"
1616

1717
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
18+
topologyv1 "github.com/vmware-tanzu/vm-operator/external/tanzu-topology/api/v1alpha1"
1819
"github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels"
19-
2020
"github.com/vmware-tanzu/vm-operator/test/builder"
2121
)
2222

@@ -102,6 +102,19 @@ func intgTestsReconcile() {
102102
Eventually(called.Load).Should(BeTrue())
103103
})
104104

105+
By("Reconcile when Zone is added", func() {
106+
called.Store(false)
107+
108+
zone := &topologyv1.Zone{}
109+
zone.Name = "my-new-zone"
110+
zone.Namespace = ctx.Namespace
111+
Expect(ctx.Client.Create(ctx, zone)).To(Succeed())
112+
113+
By("Reconcile again because of Zone create", func() {
114+
Eventually(called.Load).Should(BeTrue())
115+
})
116+
})
117+
105118
By("Deleting the VirtualMachineSetResourcePolicy", func() {
106119
err := ctx.Client.Delete(ctx, resourcePolicy)
107120
Expect(err).ToNot(HaveOccurred())

controllers/virtualmachinesetresourcepolicy/virtualmachinesetresourcepolicy_controller_suite_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import (
2121
var intgFakeVMProvider = providerfake.NewVMProvider()
2222

2323
var suite = builder.NewTestSuiteForControllerWithContext(
24-
pkgcfg.NewContextWithDefaultConfig(),
24+
pkgcfg.UpdateContext(
25+
pkgcfg.NewContextWithDefaultConfig(),
26+
func(config *pkgcfg.Config) {
27+
config.Features.WorkloadDomainIsolation = true
28+
},
29+
),
2530
virtualmachinesetresourcepolicy.AddToManager,
2631
func(ctx *pkgctx.ControllerManagerContext, _ ctrlmgr.Manager) error {
2732
ctx.VMProvider = intgFakeVMProvider

0 commit comments

Comments
 (0)