Kubernetes controller-runtime: Predicate that checks annotation of namespace the object is in

438 Views Asked by At

I have created a custom predicate for my controller that checks an annotation of the namespace my object is in. Unfortunately, in order to get the namespace, I need to execute a client.Get against my cluster.

As my goal is to reduce the load on my controller, but also be smart about the amount of requests send to my api-server, I wonder if that's the right trade-off to make.

Here's the code of the predicate:

func checkNamespaceType(c client.Client, object client.Object, desiredType string) bool {
    namespace := &corev1.Namespace{}
    if err := c.Get(context.Background(), client.ObjectKey{Name: object.GetNamespace()}, namespace); err != nil {
        return false
    }

    annotations := namespace.GetAnnotations()
    return annotations["my.domain.com/type"] == desiredType
}
0

There are 0 best solutions below