Skip to content

[chore] [receiver/k8sobjects] Extract logic for watching/pulling K8s resources into separate package#43108

Merged
ChrsMark merged 38 commits into
open-telemetry:mainfrom
bacherfl:feat/40825/reusable-k8s-receiver-code
Nov 25, 2025
Merged

[chore] [receiver/k8sobjects] Extract logic for watching/pulling K8s resources into separate package#43108
ChrsMark merged 38 commits into
open-telemetry:mainfrom
bacherfl:feat/40825/reusable-k8s-receiver-code

Conversation

@bacherfl
Copy link
Copy Markdown
Contributor

@bacherfl bacherfl commented Oct 2, 2025

Description

This PR refactors the k8sobjects receiver by extracting the components responsible for receiving the K8s resources in the cluster into a separate package with an Observer interface, and the pull/watch based implementations thereof.
This package can then also be used by the k8sevents receiver, which would then only need to format the received events to conform with the semantic conventions.

Link to tracking issue

Part of #40825 - next steps as part of this issue would be to also make use of the observer components in the k8seventsreceiver

Testing

Added tests for the new components. The tests for the receiver itself could be refactored to not use the concrete implementations of the Observer interface, but that should probably be done as a follow up to not make this PR too large

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
# Conflicts:
#	receiver/k8sobjectsreceiver/config.go
#	receiver/k8sobjectsreceiver/receiver.go
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
@bacherfl bacherfl changed the title [receiver/k8sobjects] Extract logic for watching/pulling K8s resources into separate package [chore] [receiver/k8sobjects] Extract logic for watching/pulling K8s resources into separate package Oct 3, 2025
@bacherfl bacherfl marked this pull request as ready for review October 14, 2025 08:23
Copy link
Copy Markdown
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.

Thank's for working on this!
Overall looks good with some nits.

Comment thread receiver/k8sobjectsreceiver/config.go
Comment thread receiver/k8sobjectsreceiver/receiver.go Outdated
Comment thread receiver/k8sobjectsreceiver/observer/watch/observer.go Outdated
Comment thread receiver/k8sobjectsreceiver/observer/pull/observer.go Outdated
@bacherfl
Copy link
Copy Markdown
Contributor Author

Thank's for working on this! Overall looks good with some nits.

Thanks for the feedback @ChrsMark! I will address the comments and update the PR in the next few days

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
…er-code' into feat/40825/reusable-k8s-receiver-code
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
…and its sub-packages

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
…andling

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
cancelCtx, cancel := context.WithCancel(ctx)

wait.UntilWithContext(cancelCtx, func(newCtx context.Context) {
resourceVersion, err := getResourceVersion(newCtx, o.config.Config, resource)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the current code, watchers have their own copy of the config:

cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
cfgCopy := *config
wait.UntilWithContext(cancelCtx, func(newCtx context.Context) {
resourceVersion, err := getResourceVersion(newCtx, &cfgCopy, resource)
if err != nil {
kr.setting.Logger.Error("could not retrieve a resourceVersion",
zap.String("resource", cfgCopy.gvr.String()),
zap.Error(err))
cancel()
return
}
done := kr.doWatch(newCtx, &cfgCopy, resourceVersion, watchFunc, stopperChan)
if done {
cancel()
return
}
// need to restart with a fresh resource version
cfgCopy.ResourceVersion = ""
}, 0)
}

while here, config seems to be shared, so all goroutines will operate on the same ResourceVersion, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There would be one instance of an Observer per resource type, where each one has its own copy of the config, so they should not use the same resource version. An example of how this would be used in the k8sobjectsreceiver can be seen in the following commit: 553fc36#diff-50ed6c76b6431c811210dccb90da133a8ef78e9321de67aceb801301b21ad723L110-L138 - I had to undo this change for this PR because the introduction of the new module and the adaptation of the k8sobserver to use the new module has to be done in two separate PRs

@atoulme
Copy link
Copy Markdown
Contributor

atoulme commented Nov 25, 2025

@hvaghani221, @TylerHelmuth, @ChrsMark, @krisztianfekete please review as codeowners?

@ChrsMark
Copy link
Copy Markdown
Member

3/5 code-owners approved and Tyler is out this week. I think we can merge this since it's an internal package addition and the actual refactoring will come as follow-up.

@ChrsMark ChrsMark merged commit fbe07da into open-telemetry:main Nov 25, 2025
190 checks passed
@github-actions github-actions Bot added this to the next release milestone Nov 25, 2025
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.

6 participants