Skip to content

Commit

Permalink
Merge pull request #207 from srm09/vm-controller/fix-predicate
Browse files Browse the repository at this point in the history
🐛 Adds event filter to incoming VirtualMachine objects only
  • Loading branch information
srm09 authored Aug 22, 2023
2 parents 1d7ad0a + ba02f21 commit 0ba28e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
38 changes: 18 additions & 20 deletions controllers/virtualmachine/v1alpha1/virtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

apiequality "k8s.io/apimachinery/pkg/api/equality"
ctrl "sigs.k8s.io/controller-runtime"
ctrlbuilder "sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -79,7 +80,23 @@ func AddToManager(ctx *context.ControllerManagerContext, mgr manager.Manager) er
)

builder := ctrl.NewControllerManagedBy(mgr).
For(controlledType).
// Filter any VMs that reference a VM Class with a spec.controllerName set
// to a non-empty value other than "vmoperator.vmware.com/vsphere". Please
// note that a VM will *not* be filtered if the VM Class does not exist. A
// VM is also not filtered if the field spec.controllerName is missing or
// empty as long as the default VM Class controller is
// "vmoperator.vmware.com/vsphere".
For(controlledType, ctrlbuilder.WithPredicates(kubeutil.VMForControllerPredicate(
r.Client,
// These events can be very verbose, so be careful to not log them
// at too high of a log level.
r.Logger.WithName("VMForControllerPredicate").V(8),
vmClassControllerName,
kubeutil.VMForControllerPredicateOptions{
MatchIfVMClassNotFound: true,
MatchIfControllerNameFieldEmpty: isDefaultVMClassController,
MatchIfControllerNameFieldMissing: isDefaultVMClassController,
}))).
WithOptions(controller.Options{MaxConcurrentReconciles: ctx.MaxConcurrentReconciles})

if !lib.IsWCPVMImageRegistryEnabled() {
Expand All @@ -95,25 +112,6 @@ func AddToManager(ctx *context.ControllerManagerContext, mgr manager.Manager) er
handler.EnqueueRequestsFromMapFunc(classToVMMapperFn(ctx, r.Client)))
}

// Filter any VMs that reference a VM Class with a spec.controllerName set
// to a non-empty value other than "vmoperator.vmware.com/vsphere". Please
// note that a VM will *not* be filtered if the VM Class does not exist. A
// VM is also not filtered if the field spec.controllerName is missing or
// empty as long as the default VM Class controller is
// "vmoperator.vmware.com/vsphere".
builder = builder.WithEventFilter(
kubeutil.VMForControllerPredicate(
r.Client,
// These events can be very verbose, so be careful to not log them
// at too high of a log level.
r.Logger.WithName("VMForControllerPredicate").V(8),
vmClassControllerName,
kubeutil.VMForControllerPredicateOptions{
MatchIfVMClassNotFound: true,
MatchIfControllerNameFieldEmpty: isDefaultVMClassController,
MatchIfControllerNameFieldMissing: isDefaultVMClassController,
}))

return builder.Complete(r)
}

Expand Down
28 changes: 13 additions & 15 deletions controllers/virtualmachine/v1alpha2/virtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

apiequality "k8s.io/apimachinery/pkg/api/equality"
ctrl "sigs.k8s.io/controller-runtime"
ctrlbuilder "sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -80,20 +81,13 @@ func AddToManager(ctx *context.ControllerManagerContext, mgr manager.Manager) er
)

builder := ctrl.NewControllerManagedBy(mgr).
For(controlledType).
WithOptions(controller.Options{MaxConcurrentReconciles: ctx.MaxConcurrentReconciles})

builder = builder.Watches(&source.Kind{Type: &vmopv1.VirtualMachineClass{}},
handler.EnqueueRequestsFromMapFunc(classToVMMapperFn(ctx, r.Client)))

// Filter any VMs that reference a VM Class with a spec.controllerName set
// to a non-empty value other than "vmoperator.vmware.com/vsphere". Please
// note that a VM will *not* be filtered if the VM Class does not exist. A
// VM is also not filtered if the field spec.controllerName is missing or
// empty as long as the default VM Class controller is
// "vmoperator.vmware.com/vsphere".
builder = builder.WithEventFilter(
kubeutil.VMForControllerPredicate(
// Filter any VMs that reference a VM Class with a spec.controllerName set
// to a non-empty value other than "vmoperator.vmware.com/vsphere". Please
// note that a VM will *not* be filtered if the VM Class does not exist. A
// VM is also not filtered if the field spec.controllerName is missing or
// empty as long as the default VM Class controller is
// "vmoperator.vmware.com/vsphere".
For(controlledType, ctrlbuilder.WithPredicates(kubeutil.VMForControllerPredicate(
r.Client,
// These events can be very verbose, so be careful to not log them
// at too high of a log level.
Expand All @@ -103,7 +97,11 @@ func AddToManager(ctx *context.ControllerManagerContext, mgr manager.Manager) er
MatchIfVMClassNotFound: true,
MatchIfControllerNameFieldEmpty: isDefaultVMClassController,
MatchIfControllerNameFieldMissing: isDefaultVMClassController,
}))
}))).
WithOptions(controller.Options{MaxConcurrentReconciles: ctx.MaxConcurrentReconciles})

builder = builder.Watches(&source.Kind{Type: &vmopv1.VirtualMachineClass{}},
handler.EnqueueRequestsFromMapFunc(classToVMMapperFn(ctx, r.Client)))

return builder.Complete(r)
}
Expand Down

0 comments on commit 0ba28e4

Please sign in to comment.