Skip to content

[receiver/k8scluster] Add support for observing multiple namespaces #40220

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

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

Conversation

bacherfl
Copy link
Contributor

@bacherfl bacherfl commented May 22, 2025

Description

This PR adapts the k8scluster receiver to support retrieving resources from multiple namespaces.
Due to the way the informer stores are maintained internally the required changes for this feature are more complex than anticipated: previously, as there was only the option to either observe all namespaces, or only one, there was one informer per GVK, whose store was used to regularly collect the metrics for the observed resources. Now, as the informers provided by the k8s library can only observe either one, or all namespaces, we can potentially have multiple informers per namespace for the same GVK. This is now done as a map[GVK]map[string]informer.Store, where the string represents the namespace.

Link to tracking issue

Fixes #40089

Testing

Added unit tests and a new e2e test

Documentation

Adapted readme to describe the added option

@bacherfl bacherfl marked this pull request as ready for review May 27, 2025 08:25
@bacherfl bacherfl requested a review from a team as a code owner May 27, 2025 08:25
Copy link
Member

@ChrsMark ChrsMark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature makes sense to me and the code-changes look mostly good from my perspective. I would be fine making this feature happen if other code-owners do not have any concern with this.

@ChrsMark
Copy link
Member

@povilasv @TylerHelmuth @dmitryax please a look when you get the chance.

Comment on lines 374 to 376
if len(namespaces) == 1 {
// if there is only ony namespace in the provided namespaces array, use the deprecated field to still cover the code related to that
config.Namespace = namespaces[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure if this is needed? Why use old field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left that here for now to still cover the deprecated field in the tests as well, until it is removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need to do it, since we want to remove it in future, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes probably, since it's going to be removed anyway I will update the PR to remove that

Comment on lines 194 to 212
return nil
var job any
var err error
exists := false

// first, check if there is a store for all namespaces
if jobStore, ok := jobStores[""]; ok {
job, exists, err = jobStore.GetByKey(utils.GetIDForCache(pod.Namespace, jobRef.Name))
if err != nil {
logError(err, jobRef, pod.UID, logger)
return nil
}
}
if !exists {
// check if there is a store for the namespace of the pod
if jobStore, ok := jobStores[pod.Namespace]; ok {
job, exists, err = jobStore.GetByKey(utils.GetIDForCache(pod.Namespace, jobRef.Name))
if err != nil {
logError(err, jobRef, pod.UID, logger)
return nil
}
}
if !exists {
logDebug(jobRef, pod.UID, logger)
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this logic be abstracted away somehow into one method?

as we are repeating it on all stores

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i now moved that logic into the internal/utils package, along with some tests for it

Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot removed the Stale label Jun 25, 2025
Copy link
Member

@ChrsMark ChrsMark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ChrsMark ChrsMark requested a review from povilasv June 26, 2025 08:14
bacherfl added 3 commits June 26, 2025 10:16
Signed-off-by: Florian Bacher <[email protected]>
Signed-off-by: Florian Bacher <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[receiver/k8scluster]: Support list of namespaces to be observed
5 participants