From 66df53ca6d899d9dbd9421e23ee006009e9c7101 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Fri, 29 Mar 2024 20:43:02 -0700 Subject: [PATCH] [processor/resourcedetection] Only attempt to detect k8s node resource attributes when enabled (#32039) **Description:** Detecting k8s node resource attributes requires specific permissions and configuration. If they're disabled we shouldn't hit errors trying to detect them. This change makes it so that these resource attributes are only attempted to be detected when enabled. **Link to tracking Issue:** Resolves https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/31941 **Testing:** Existing tests are passing. New test was failing before changes, succeeds after. --- .chloggen/resdetproc_check_enabled.yaml | 27 +++++++++++++++++++ .../internal/k8snode/k8snode.go | 22 +++++++++------ .../internal/k8snode/k8snode_test.go | 24 +++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 .chloggen/resdetproc_check_enabled.yaml diff --git a/.chloggen/resdetproc_check_enabled.yaml b/.chloggen/resdetproc_check_enabled.yaml new file mode 100644 index 000000000000..dc416d619004 --- /dev/null +++ b/.chloggen/resdetproc_check_enabled.yaml @@ -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: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: resourcedetectionprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Only attempt to detect Kubernetes node resource attributes when they're enabled. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [31941] + +# (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: [] diff --git a/processor/resourcedetectionprocessor/internal/k8snode/k8snode.go b/processor/resourcedetectionprocessor/internal/k8snode/k8snode.go index 425173d8e207..84cef65720a9 100644 --- a/processor/resourcedetectionprocessor/internal/k8snode/k8snode.go +++ b/processor/resourcedetectionprocessor/internal/k8snode/k8snode.go @@ -27,6 +27,7 @@ var _ internal.Detector = (*detector)(nil) type detector struct { provider k8snode.Provider logger *zap.Logger + ra *metadata.ResourceAttributesConfig rb *metadata.ResourceBuilder } @@ -43,22 +44,27 @@ func NewDetector(set processor.CreateSettings, dcfg internal.DetectorConfig) (in return &detector{ provider: k8snodeProvider, logger: set.Logger, + ra: &cfg.ResourceAttributes, rb: metadata.NewResourceBuilder(cfg.ResourceAttributes), }, nil } func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) { - nodeUID, err := d.provider.NodeUID(ctx) - if err != nil { - return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node UID: %w", err) + if d.ra.K8sNodeUID.Enabled { + nodeUID, err := d.provider.NodeUID(ctx) + if err != nil { + return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node UID: %w", err) + } + d.rb.SetK8sNodeUID(nodeUID) } - d.rb.SetK8sNodeUID(nodeUID) - nodeName, err := d.provider.NodeName(ctx) - if err != nil { - return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node name: %w", err) + if d.ra.K8sNodeName.Enabled { + nodeName, err := d.provider.NodeName(ctx) + if err != nil { + return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node name: %w", err) + } + d.rb.SetK8sNodeName(nodeName) } - d.rb.SetK8sNodeName(nodeName) return d.rb.Emit(), conventions.SchemaURL, nil } diff --git a/processor/resourcedetectionprocessor/internal/k8snode/k8snode_test.go b/processor/resourcedetectionprocessor/internal/k8snode/k8snode_test.go index 1aae5a0646c8..6034915bd65a 100644 --- a/processor/resourcedetectionprocessor/internal/k8snode/k8snode_test.go +++ b/processor/resourcedetectionprocessor/internal/k8snode/k8snode_test.go @@ -59,3 +59,27 @@ func TestDetect(t *testing.T) { assert.Equal(t, expected, res.Attributes().AsRaw()) } + +func TestDetectDisabledResourceAttributes(t *testing.T) { + md := &mockMetadata{} + cfg := CreateDefaultConfig() + cfg.ResourceAttributes.K8sNodeUID.Enabled = false + cfg.ResourceAttributes.K8sNodeName.Enabled = false + // set k8s cluster env variables and auth type to create a dummy API client + cfg.APIConfig.AuthType = k8sconfig.AuthTypeNone + t.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1") + t.Setenv("KUBERNETES_SERVICE_PORT", "6443") + t.Setenv("K8S_NODE_NAME", "mainNode") + + k8sDetector, err := NewDetector(processortest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + k8sDetector.(*detector).provider = md + res, schemaURL, err := k8sDetector.Detect(context.Background()) + require.NoError(t, err) + assert.Equal(t, conventions.SchemaURL, schemaURL) + md.AssertExpectations(t) + + expected := map[string]any{} + + assert.Equal(t, expected, res.Attributes().AsRaw()) +}