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

K8sattributes lifecycle #30387

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/k8sattributes_lifecycle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: k8sattributesprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Apply lifecycle tests to k8sprocessor, change its behavior to report fatal error

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [30387]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
39 changes: 9 additions & 30 deletions processor/k8sattributesprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ func createTracesProcessorWithOptions(
next consumer.Traces,
options ...option,
) (processor.Traces, error) {
kp, err := createKubernetesProcessor(set, cfg, options...)
if err != nil {
return nil, err
}
kp := createKubernetesProcessor(set, cfg, options...)

return processorhelper.NewTracesProcessor(
ctx,
Expand All @@ -98,10 +95,7 @@ func createMetricsProcessorWithOptions(
nextMetricsConsumer consumer.Metrics,
options ...option,
) (processor.Metrics, error) {
kp, err := createKubernetesProcessor(set, cfg, options...)
if err != nil {
return nil, err
}
kp := createKubernetesProcessor(set, cfg, options...)

return processorhelper.NewMetricsProcessor(
ctx,
Expand All @@ -121,10 +115,7 @@ func createLogsProcessorWithOptions(
nextLogsConsumer consumer.Logs,
options ...option,
) (processor.Logs, error) {
kp, err := createKubernetesProcessor(set, cfg, options...)
if err != nil {
return nil, err
}
kp := createKubernetesProcessor(set, cfg, options...)

return processorhelper.NewLogsProcessor(
ctx,
Expand All @@ -141,26 +132,14 @@ func createKubernetesProcessor(
params processor.CreateSettings,
cfg component.Config,
options ...option,
) (*kubernetesprocessor, error) {
kp := &kubernetesprocessor{logger: params.Logger}

allOptions := append(createProcessorOpts(cfg), options...)

for _, opt := range allOptions {
if err := opt(kp); err != nil {
return nil, err
}
}

// This might have been set by an option already
if kp.kc == nil {
err := kp.initKubeClient(kp.logger, kubeClientProvider)
if err != nil {
return nil, err
}
) *kubernetesprocessor {
kp := &kubernetesprocessor{logger: params.Logger,
cfg: cfg,
options: options,
telemetrySettings: params.TelemetrySettings,
}

return kp, nil
return kp
}

func createProcessorOpts(cfg component.Config) []option {
Expand Down
122 changes: 122 additions & 0 deletions processor/k8sattributesprocessor/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions processor/k8sattributesprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.5.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.92.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.92.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest v0.92.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -34,7 +35,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
Expand Down Expand Up @@ -68,7 +69,7 @@ require (
github.com/mostynb/go-grpc-compression v1.2.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down Expand Up @@ -127,3 +128,11 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8ste

// ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules
replace cloud.google.com/go v0.54.0 => cloud.google.com/go v0.110.10

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden
8 changes: 4 additions & 4 deletions processor/k8sattributesprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions processor/k8sattributesprocessor/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ resource_attributes:
description: Container image tag. Requires container.id or k8s.container.name.
type: string
enabled: true

tests:
config:
36 changes: 28 additions & 8 deletions processor/k8sattributesprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ const (
)

type kubernetesprocessor struct {
logger *zap.Logger
apiConfig k8sconfig.APIConfig
kc kube.Client
passthroughMode bool
rules kube.ExtractionRules
filters kube.Filters
podAssociations []kube.Association
podIgnore kube.Excludes
cfg component.Config
options []option
telemetrySettings component.TelemetrySettings
logger *zap.Logger
apiConfig k8sconfig.APIConfig
kc kube.Client
passthroughMode bool
rules kube.ExtractionRules
filters kube.Filters
podAssociations []kube.Association
podIgnore kube.Excludes
}

func (kp *kubernetesprocessor) initKubeClient(logger *zap.Logger, kubeClient kube.ClientProvider) error {
Expand All @@ -50,6 +53,23 @@ func (kp *kubernetesprocessor) initKubeClient(logger *zap.Logger, kubeClient kub
}

func (kp *kubernetesprocessor) Start(_ context.Context, _ component.Host) error {
allOptions := append(createProcessorOpts(kp.cfg), kp.options...)

for _, opt := range allOptions {
if err := opt(kp); err != nil {
kp.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
return nil
}
}

// This might have been set by an option already
if kp.kc == nil {
err := kp.initKubeClient(kp.logger, kubeClientProvider)
if err != nil {
kp.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
return nil
}
}
if !kp.passthroughMode {
go kp.kc.Start()
}
Expand Down
Loading