0

I am working on a project that watches resources of all object Kinds within a Namespace and perform an identical action as a response to change of status of said resource. Kind of resources includes CRDs and could be different from one Namespace to another depending on the resources created within a Namespace.

Is there a known pattern for dynamically creating controllers for all object Kinds that exist in a Namespace?

A close example of what i'm looking to achieve is a watcher that watches all resources in a Namespace and send a message to a receiver whenever said resources are changed.

Take this scenario for example, the following actions are taken by this watcher when it is started:

  1. For each namespace annotated with watch-namespace: true do the following:
  2. Send manifest of each resources in Namespace to receiver
  3. When resource is changed, send the new manifest to receiver

The watcher must be able to watch all Kinds currently existing in namespace or created in the future. It is also important to note that this watcher does not alter or reconcile the state of the resources it is watching.

Fares7
  • 41
  • 3
  • 1
    how would that work? If you create your controller today. And tomorrow I create a new object with a new kind in that namespace, what would happen? – The Fool Feb 16 '22 at 12:21
  • Could you please clarify the details of what you are going to do? Maybe some examples? – mozello Feb 16 '22 at 14:41
  • I've added an example to my question details. – Fares7 Feb 17 '22 at 12:36
  • 1
    There is no good way to watch "all resources in a namespace". There's not even a good way to *list* all resources in a namespace -- the only available mechanism for that is really (a) iterate over available namespaced resources (`kubectl api-resources --namespaced=true`) and then (b) explicitly request a list of resources of each type in the target namespace. – larsks Feb 17 '22 at 12:50
  • This workaround covers listing all resources in a Namespace, however, it does not allow for watching changes to them. – Fares7 Feb 17 '22 at 12:56
  • Check this [answer](https://stackoverflow.com/a/53273666/17739440). Let me know if it helped, please. – mozello Feb 17 '22 at 20:08
  • Unfortunetlay, it doesn't help. The suggested answer discusses watching versioned resources. The method of watching used in the answer use [Watch](https://pkg.go.dev/github.com/kubernetes-sigs/kubebuilder/pkg/controller#GenericController.Watch) function that requires an Object Kind, which maybe unknown at compile time in the solution i'm after. – Fares7 Feb 18 '22 at 18:38
  • What about this [topic](https://stackoverflow.com/questions/40975307/how-to-watch-events-on-a-kubernetes-service-using-its-go-client)? There are some solutions to watch k8s events. Let me know if it helped, please. – mozello Feb 21 '22 at 17:10

0 Answers0