Skip to content

Conversation

@bacherfl
Copy link
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

# Conflicts:
#	receiver/k8sobjectsreceiver/config.go
#	receiver/k8sobjectsreceiver/receiver.go
Signed-off-by: Florian Bacher <[email protected]>
Signed-off-by: Florian Bacher <[email protected]>
Signed-off-by: Florian Bacher <[email protected]>
@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
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.


defaultPullInterval time.Duration = time.Hour
defaultMode mode = PullMode
defaultResourceVersion = "1"
Copy link
Member

Choose a reason for hiding this comment

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

Is this still needed/used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, the defaultResourceVersion is not used anymore. will remove that

},
)
}
return nil, fmt.Errorf("invalid observer mode mode: %s", object.Mode)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return nil, fmt.Errorf("invalid observer mode mode: %s", object.Mode)
return nil, fmt.Errorf("invalid observer mode: %s", object.Mode)

o.logger.Error("error in watching object",
zap.String("resource", o.config.Gvr.String()),
zap.Error(err))
return true
Copy link
Member

Choose a reason for hiding this comment

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

Should that still be true given that the watch was not successful?

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 think you are right - here it would make sense to return false instead, to trigger a restart attempt in case of an error

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pull // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sobjectsreceiver/observer/pull"
Copy link
Member

Choose a reason for hiding this comment

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

Is the plan to further move this common code-base to a shared place? Otherwise it would be weird to import in the k8sevents receiver this code from the k8sobjects receiver, no?

Maybe we can use either pkg or internal? Probably internal since those are not expected to be used outside of this repository?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point - definitely makes sense to move that there. As this will be a new module within internal, is it ok if i set the code owners of that module to the ones that are currently listed in the k8sobjects receiver?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it seems like we will have to split the PR in this case, since we need to have the new module in internal pushed to the main branch, in order to be able to reference it in the k8sobserver go.mod file

Copy link
Member

Choose a reason for hiding this comment

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

Happy to hear what other think here before doing it. @TylerHelmuth @dmitryax any thoughts on this?

@bacherfl
Copy link
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

…er-code' into feat/40825/reusable-k8s-receiver-code
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.

3 participants