Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watcher: Refactor K8sWatcher to reuse config and factories #3413

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

lambdanis
Copy link
Contributor

The previous K8sWatcher implementation allowed extending it with extra
informers via AddInformers method, but didn't provide a way to reuse shared
informer factories between AddInformers calls. That's wasteful, as the whole
point of a factory is to be reused.

This PR adds shared informer factories to the K8sWatcher struct: one for
global k8s built-in resources, one for node-local k8s built-in resources, and
one for CRDs. They are initialized in NewK8sWatcher function. The AddInformers
method is replaced by AddInformer, which simply adds a single informer with
indexers to the list. The Start method starts all informer factories - thanks
to storing them, there is no more need to dynamically extend a start function.

Initialization of the pod informer is moved out of NewK8sWatcher to a dedicated
function called from main, AddPodInformer.

Watching TracingPolicy(Namespaced) now reuses the K8sWatcher. Before, we were
reading Kubernetes config, creating an informer factory and starting it, all in
the WatchTracePolicy function. Now it's replaced by AddTracingPolicyInformer,
which only adds informers to the K8sWatcher.

There are no functional changes in this commit, only moving code around. It's a
preparation to further refactoring of the k8s watcher that will decouple the
core k8s watcher and resource-specific functionalities, and allow for reusing
of informer factories. Also removed unused global constants.

Signed-off-by: Anna Kapuscinska <[email protected]>
K8sResourceWatcher interface covers two distinct functionalities: setting up
Kubernetes informers and accessing information about pods. These make sense as
separate interfaces, so the K8sResourceWatcher methods for accessing pods are
split out into a dedicated interface, PodAccessor, and setup methods - into
Watcher interface. The K8sResourceWatcher interface stays in place for now,
embedding both.

Signed-off-by: Anna Kapuscinska <[email protected]>
@lambdanis lambdanis added the release-note/misc This PR makes changes that have no direct user impact. label Feb 17, 2025
The previous K8sWatcher implementation allowed extending it with extra
informers via AddInformers method, but didn't provide a way to reuse shared
informer factories between AddInformers calls. That's wasteful, as the whole
point of a factory is to be reused.

This commit adds shared informer factories to the K8sWatcher struct: one for
global k8s built-in resources, one for node-local k8s built-in resources, and
one for CRDs. They are initialized in NewK8sWatcher function. The AddInformers
method is replaced by AddInformer, which simply adds a single informer with
indexers to the list. The Start method starts all informer factories - thanks
to storing them, there is no more need to dynamically extend a start function.

Initialization of the pod informer is moved out of NewK8sWatcher to a dedicated
function called from main, AddPodInformer. The idea is that K8sWatcher provides
a config and interface for adding informers, and different packages can use it
to watch specific resourced. A following commit will use this functionality for
watching TracingPolicy CRD.

Signed-off-by: Anna Kapuscinska <[email protected]>
Let's use a more descriptive name. Also renamed crd.go to tracingpolicy.go.
There are no functional changes in this commit.

Signed-off-by: Anna Kapuscinska <[email protected]>
Unexport informer factories in the K8sWatcher struct and implement exported
Get* methods instead. This will make it easier to reuse them and test.

Signed-off-by: Anna Kapuscinska <[email protected]>
@lambdanis lambdanis force-pushed the pr/lambdanis/factory branch from 12ec831 to 76ec341 Compare February 19, 2025 20:26
Copy link

netlify bot commented Feb 19, 2025

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit 76ec341
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/67b63e93544ab00008aef204
😎 Deploy Preview https://deploy-preview-3413--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Before, to watch TracingPolicy(Namespaced) custom resources, we were reading
Kubernetes config, creating an informer factory and starting it, all in the
WatchTracePolicy function. As now the watcher.K8sWatcher struct initializes
informer factories, we can reuse it instead of configuring everything from
scratch. This commit replaces the WatchTracePolicy function with
AddTracingPolicyInformer, which only adds informers to the K8sWatcher.
Initialization and start of the K8sWatcher happen in main.

Signed-off-by: Anna Kapuscinska <[email protected]>
@lambdanis lambdanis force-pushed the pr/lambdanis/factory branch from 76ec341 to a051c49 Compare February 19, 2025 21:20
@lambdanis lambdanis marked this pull request as ready for review February 19, 2025 22:32
@lambdanis lambdanis requested a review from a team as a code owner February 19, 2025 22:32
@lambdanis lambdanis requested a review from olsajiri February 19, 2025 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant