3

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=
PJEM
  • 557
  • 7
  • 33

1 Answers1

2

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")
        },
    })
Jacob Malachowski
  • 911
  • 1
  • 9
  • 18
  • It seems `.Has("foo", "bar")`is invalid since since `.Has()`expects only a single string. I think a correct example would be only with a label map key e.g. like so: `.Has("sealedsecrets.bitnami.com/sealed-secrets-key")` – rufreakde Jan 24 '23 at 10:05