Skip to content

Change k8sattribute to return error if unset envvar is used for node_from_env_var #39447

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

Merged
merged 1 commit into from
Apr 16, 2025
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
28 changes: 28 additions & 0 deletions .chloggen/ret-error-k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use this changelog template to create an entry for release notes.

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

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Change processor/k8sattributes to return error if unset envvar is used for `node_from_env_var`

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

# (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: Before this was a valid configuration, but had an unexpected behavior to monitor the entire cluster. |
To keep the same behavior simply do not set the `node_from_env_var` value or use empty string.

# 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: []
10 changes: 10 additions & 0 deletions processor/k8sattributesprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package k8sattributesprocessor // import "github.com/open-telemetry/opentelemetr

import (
"fmt"
"os"
"regexp"
"time"

Expand Down Expand Up @@ -259,6 +260,15 @@ type FilterConfig struct {
Labels []FieldFilterConfig `mapstructure:"labels"`
}

func (cfg *FilterConfig) Validate() error {
if cfg.NodeFromEnvVar != "" {
if _, ok := os.LookupEnv(cfg.NodeFromEnvVar); !ok {
return fmt.Errorf("`node_from_env_var` is configured but envvar %q is not set", cfg.NodeFromEnvVar)
}
}
return nil
}

// FieldFilterConfig allows specifying exactly one filter by a field.
// It can be used to represent a label or generic field filter.
type FieldFilterConfig struct {
Expand Down
14 changes: 12 additions & 2 deletions processor/k8sattributesprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
)

func TestLoadConfig(t *testing.T) {
t.Parallel()

tests := []struct {
id component.ID
expected component.Config
Expand Down Expand Up @@ -173,6 +171,8 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, err)
require.NoError(t, sub.Unmarshal(cfg))

// Set "K8S_NODE" to pass validation.
t.Setenv("K8S_NODE", "ip-111.us-west-2.compute.internal")
if tt.expected == nil {
err = xconfmap.Validate(cfg)
assert.Error(t, err)
Expand All @@ -183,3 +183,13 @@ func TestLoadConfig(t *testing.T) {
})
}
}

func TestFilterConfigInvalidEnvVar(t *testing.T) {
f := FilterConfig{
Namespace: "ns2",
NodeFromEnvVar: "K8S_NODE",
Labels: []FieldFilterConfig{},
Fields: []FieldFilterConfig{},
}
assert.Error(t, xconfmap.Validate(f))
}
Loading