amazon-web-servicesgokuberneteskubebuilder

Controller Reconcile on object changes


Im trying to listen to secret change using the operator sdk The problem is that im not getting the reconcile event when I apply the secret with the labels that I define in the operator

I did the following

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
    Scheme:                 scheme,
        …
    NewCache: cache.BuilderWithOptions(cache.Options{
        SelectorsByObject: cache.SelectorsByObject{
            &corev1.Secret{}: {
                Label: labels.SelectorFromSet(labels.Set{"foo": "bar"}),
            },
        },
    }),

I run the operator and apply the following secret and the reconcile is not invoked, any idea?

apiVersion: v1
kind: Secret
metadata:
  labels:
    foo: bar
  name: mysecret
  namespace: dev
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: dGVzdBo=

Solution

  • It looks like you are using the cache.Options.SelectorsByObject field to specify the labels that should trigger a reconcile event. However, this field is used to specify the labels that should be used to select objects from the cache, not the labels that should trigger a reconcile event.

    To specify the labels that should trigger a reconcile event, you can use the ctrl.Watch function, like this:

    mgr.Watch(&source.Kind{Type: &corev1.Secret{}},
        &handler.EnqueueRequestForObject{},
        predicate.Funcs{
            UpdateFunc: func(e event.UpdateEvent) bool {
                return labels.Set(e.MetaNew.GetLabels()).Has("foo", "bar")
            },
        })