Skip to content

Commit

Permalink
Fix NodeConfig controller handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tnozicka committed Jul 3, 2024
1 parent 6eb2c75 commit 7bd9ccd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
60 changes: 49 additions & 11 deletions pkg/controller/nodeconfig/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
scyllav1alpha1listers "github.com/scylladb/scylla-operator/pkg/client/scylla/listers/scylla/v1alpha1"
"github.com/scylladb/scylla-operator/pkg/controllerhelpers"
"github.com/scylladb/scylla-operator/pkg/kubeinterfaces"
"github.com/scylladb/scylla-operator/pkg/naming"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -73,6 +74,14 @@ type Controller struct {
operatorImage string
}

func isManagedByNodeConfigController(nc *scyllav1alpha1.NodeConfig) bool {
if nc.Labels == nil {
return false
}

return nc.Labels[naming.NodeConfigNameLabel] == naming.NodeConfigAppName
}

func NewController(
kubeClient kubernetes.Interface,
scyllaClient scyllav1alpha1client.ScyllaV1alpha1Interface,
Expand Down Expand Up @@ -182,6 +191,12 @@ func NewController(
DeleteFunc: ncc.deleteDaemonSet,
})

namespaceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ncc.addNamespace,
UpdateFunc: ncc.updateNamespace,
DeleteFunc: ncc.deleteNamespace,
})

return ncc, nil
}

Expand All @@ -208,72 +223,95 @@ func (ncc *Controller) deleteDaemonSet(obj interface{}) {
)
}

func (ncc *Controller) addNamespace(obj interface{}) {
ncc.handlers.HandleAdd(
obj.(*corev1.Namespace),
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) updateNamespace(old, cur interface{}) {
ncc.handlers.HandleUpdate(
old.(*corev1.Namespace),
cur.(*corev1.Namespace),
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
ncc.deleteNamespace,
)
}

func (ncc *Controller) deleteNamespace(obj interface{}) {
ncc.handlers.HandleDelete(
obj,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) addServiceAccount(obj interface{}) {
ncc.handlers.HandleAdd(
obj.(*corev1.ServiceAccount),
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) updateServiceAccount(old, cur interface{}) {
ncc.handlers.HandleUpdate(
old.(*corev1.ServiceAccount),
cur.(*corev1.ServiceAccount),
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
ncc.deleteServiceAccount,
)
}

func (ncc *Controller) deleteServiceAccount(obj interface{}) {
ncc.handlers.HandleDelete(
obj,
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) addClusterRoleBinding(obj interface{}) {
ncc.handlers.HandleAdd(
obj.(*rbacv1.ClusterRoleBinding),
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) updateClusterRoleBinding(old, cur interface{}) {
ncc.handlers.HandleUpdate(
old.(*rbacv1.ClusterRoleBinding),
cur.(*rbacv1.ClusterRoleBinding),
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
ncc.deleteClusterRoleBinding,
)
}

func (ncc *Controller) deleteClusterRoleBinding(obj interface{}) {
ncc.handlers.HandleDelete(
obj,
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) addClusterRole(obj interface{}) {
ncc.handlers.HandleAdd(
obj.(*rbacv1.ClusterRole),
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) updateClusterRole(old, cur interface{}) {
ncc.handlers.HandleUpdate(
old.(*rbacv1.ClusterRole),
cur.(*rbacv1.ClusterRole),
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
ncc.deleteClusterRole,
)
}

func (ncc *Controller) deleteClusterRole(obj interface{}) {
ncc.handlers.HandleDelete(
obj,
ncc.handlers.EnqueueOwner,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

Expand Down Expand Up @@ -303,15 +341,15 @@ func (ncc *Controller) deleteNodeConfig(obj interface{}) {
func (ncc *Controller) addScyllaOperatorConfig(obj interface{}) {
ncc.handlers.HandleAdd(
obj.(*scyllav1alpha1.ScyllaOperatorConfig),
ncc.handlers.EnqueueAll,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
)
}

func (ncc *Controller) updateScyllaOperatorConfig(old, cur interface{}) {
ncc.handlers.HandleUpdate(
old.(*scyllav1alpha1.ScyllaOperatorConfig),
cur.(*scyllav1alpha1.ScyllaOperatorConfig),
ncc.handlers.EnqueueAll,
ncc.handlers.EnqueueAllWithFilterFunc(isManagedByNodeConfigController),
nil,
)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/controllerhelpers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ func (h *Handlers[T]) EnqueueAll(depth int, untypedObj kubeinterfaces.ObjectInte
h.EnqueueAllFunc(h.Enqueue)(depth+1, untypedObj, op)
}

func (h *Handlers[T]) EnqueueAllWithFilterFunc(filterFunc func(T) bool) EnqueueFuncType {
return func(depth int, untypedObj kubeinterfaces.ObjectInterface, op HandlerOperationType) {
obj := untypedObj.(T)

if !filterFunc(obj) {
return
}

h.Enqueue(depth+1, untypedObj, op)
}
}

func (h *Handlers[T]) EnqueueAllFunc(enqueueFunc EnqueueFuncType) EnqueueFuncType {
return func(depth int, untypedObj kubeinterfaces.ObjectInterface, op HandlerOperationType) {
controllerObjs, err := h.getterLister.List(untypedObj.GetNamespace(), labels.Everything())
Expand Down

0 comments on commit 7bd9ccd

Please sign in to comment.