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

[CONTP-656] wmeta kubelet unknown envvar handling #34450

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
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 @@ -416,9 +416,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this file is in the third_party directory, it was likely copy-pasted. It might be helpful to add a brief comment explaining any modifications to ensure we remember the changes.

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