Skip to content

Commit bbb6493

Browse files
committed
Change k8sattribute to return error if unset envvar is used for node_from_env_var
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 8c7ae54 commit bbb6493

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

.chloggen/ret-error-k8s.yaml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: processor/k8sattributes
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Change processor/k8sattributes to return error if unset envvar is used for `node_from_env_var`
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39447]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: Before this was a valid configuration, but had an unexpected behavior to monitor the entire cluster. |
19+
To keep the same behavior simply do not set the `node_from_env_var` value or use empty string.
20+
21+
# If your change doesn't affect end users or the exported elements of any package,
22+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
23+
# Optional: The change log or logs in which this entry should be included.
24+
# e.g. '[user]' or '[user, api]'
25+
# Include 'user' if the change is relevant to end users.
26+
# Include 'api' if there is a change to a library API.
27+
# Default: '[user]'
28+
change_logs: []

processor/k8sattributesprocessor/config.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package k8sattributesprocessor // import "github.com/open-telemetry/opentelemetr
55

66
import (
77
"fmt"
8+
"os"
89
"regexp"
910
"time"
1011

@@ -259,6 +260,15 @@ type FilterConfig struct {
259260
Labels []FieldFilterConfig `mapstructure:"labels"`
260261
}
261262

263+
func (cfg *FilterConfig) Validate() error {
264+
if cfg.NodeFromEnvVar != "" {
265+
if _, ok := os.LookupEnv(cfg.NodeFromEnvVar); !ok {
266+
return fmt.Errorf("`node_from_env_var` is configured but envvar %q is not set", cfg.NodeFromEnvVar)
267+
}
268+
}
269+
return nil
270+
}
271+
262272
// FieldFilterConfig allows specifying exactly one filter by a field.
263273
// It can be used to represent a label or generic field filter.
264274
type FieldFilterConfig struct {

processor/k8sattributesprocessor/config_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
)
2121

2222
func TestLoadConfig(t *testing.T) {
23-
t.Parallel()
24-
2523
tests := []struct {
2624
id component.ID
2725
expected component.Config
@@ -173,6 +171,8 @@ func TestLoadConfig(t *testing.T) {
173171
require.NoError(t, err)
174172
require.NoError(t, sub.Unmarshal(cfg))
175173

174+
// Set "K8S_NODE" to pass validation.
175+
t.Setenv("K8S_NODE", "ip-111.us-west-2.compute.internal")
176176
if tt.expected == nil {
177177
err = xconfmap.Validate(cfg)
178178
assert.Error(t, err)
@@ -183,3 +183,13 @@ func TestLoadConfig(t *testing.T) {
183183
})
184184
}
185185
}
186+
187+
func TestFilterConfigInvalidEnvVar(t *testing.T) {
188+
f := FilterConfig{
189+
Namespace: "ns2",
190+
NodeFromEnvVar: "K8S_NODE",
191+
Labels: []FieldFilterConfig{},
192+
Fields: []FieldFilterConfig{},
193+
}
194+
assert.Error(t, xconfmap.Validate(f))
195+
}

0 commit comments

Comments
 (0)