Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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=
like image 921
JJD Avatar asked Oct 24 '25 08:10

JJD


1 Answers

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")
        },
    })
like image 134
Jacob Malachowski Avatar answered Oct 26 '25 00:10

Jacob Malachowski