Skip to content

Commit

Permalink
Kubelet ignoring unknown envvars
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedos committed Feb 25, 2025
1 parent dc80555 commit f8eef53
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 249 deletions.
11 changes: 10 additions & 1 deletion comp/core/workloadmeta/collectors/internal/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,18 @@ func extractEnvFromSpec(envSpec []kubelet.EnvVar) map[string]string {
continue
}

ok := true
runtimeVal := e.Value
if runtimeVal != "" {
runtimeVal = expansion.Expand(runtimeVal, mappingFunc)
runtimeVal, ok = expansion.Expand(runtimeVal, mappingFunc)
}

// Ignore environment variables that failed to expand
// This occurs when the env var references another env var
// that has its value sourced from an external source
// (eg. ConfigMap, Secret, DownwardAPI)
if !ok {
continue
}

env[e.Name] = runtimeVal
Expand Down
11 changes: 11 additions & 0 deletions comp/core/workloadmeta/def/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func container1(testTime time.Time) Container {
Health: ContainerHealthUnknown,
},
CollectorTags: []string{"tag1", "tag2"},
EnvVars: map[string]string{
"DD_SERVICE-partial": "my-svc",
},
}
}

Expand Down Expand Up @@ -91,6 +94,10 @@ func container2() Container {
Health: ContainerHealthHealthy,
},
CollectorTags: []string{"tag3"},
EnvVars: map[string]string{
"DD_SERVICE-partial": "my-svc",
"DD_ENV-extra": "prod",
},
}
}

Expand All @@ -114,6 +121,10 @@ func TestMerge(t *testing.T) {
ExitCode: pointer.Ptr(int64(100)),
Health: ContainerHealthHealthy,
},
EnvVars: map[string]string{
"DD_SERVICE-partial": "my-svc",
"DD_ENV-extra": "prod",
},
}

expectedPorts := []ContainerPort{
Expand Down
18 changes: 11 additions & 7 deletions internal/third_party/golang/expansion/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ func syntaxWrap(input string) string {
// implements the expansion semantics defined in the expansion spec; it
// returns the input string wrapped in the expansion syntax if no mapping
// for the input is found.
func MappingFuncFor(context ...map[string]string) func(string) string {
return func(input string) string {
func MappingFuncFor(context ...map[string]string) func(string) (string, bool) {
return func(input string) (string, bool) {
for _, vars := range context {
val, ok := vars[input]
if ok {
return val
return val, true
}
}

return syntaxWrap(input)
return syntaxWrap(input), false
}
}

// Expand replaces variable references in the input string according to
// the expansion spec using the given mapping function to resolve the
// values of variables.
func Expand(input string, mapping func(string) string) string {
func Expand(input string, mapping func(string) (string, bool)) (string, bool) {
var buf bytes.Buffer
checkpoint := 0
for cursor := 0; cursor < len(input); cursor++ {
Expand All @@ -52,7 +52,11 @@ func Expand(input string, mapping func(string) string) string {
// We were able to read a variable name correctly;
// apply the mapping to the variable name and copy the
// bytes into the buffer
buf.WriteString(mapping(read))
mappedValue, found := mapping(read)
if !found {
return "", false
}
buf.WriteString(mappedValue)
} else {
// Not a variable name; copy the read bytes into the buffer
buf.WriteString(read)
Expand All @@ -69,7 +73,7 @@ func Expand(input string, mapping func(string) string) string {

// Return the buffer and any remaining unwritten bytes in the
// input string.
return buf.String() + input[checkpoint:]
return buf.String() + input[checkpoint:], true
}

// tryReadVariableName attempts to read a variable name from the input
Expand Down
Loading

0 comments on commit f8eef53

Please sign in to comment.