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

Add handling of environment variables from ConfigMaps and Secrets #3373

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions .chloggen/enhancement-envs-from-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for detecting and reading environment variables from POD's ConfigMap resources.

# One or more tracking issues related to the change
issues: [1393, 1814]

# (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: |
Now the auto-instrumentation will see the environment variables defined via POD's `env.valueFrom.configMapKeyRef` and
`envFrom.configMapRef` fields, so the auto-instrumentation will be able to prevent overriding them, or it will be able
to extend the existing definition.
19 changes: 19 additions & 0 deletions .chloggen/enhancement-envs-from-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for detecting and reading environment variables from POD's Secret resources.

# One or more tracking issues related to the change
issues: [1393, 1814]

# (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: |
Now the auto-instrumentation will see the environment variables defined via POD's `env.valueFrom.secretKeyRef` and
`envFrom.secretRef` fields, so the auto-instrumentation will be able to prevent overriding them, or it will be able to
extend the existing definition.
24 changes: 9 additions & 15 deletions pkg/instrumentation/apachehttpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,13 @@ const (
6) Inject mounting of volumes / files into appropriate directories in application container
*/

func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod corev1.Pod, useLabelsForResourceAttributes bool, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {
func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod corev1.Pod, useLabelsForResourceAttributes bool, container Container, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {

volume := instrVolume(apacheSpec.VolumeClaimTemplate, apacheAgentVolume, apacheSpec.VolumeSizeLimit)

// caller checks if there is at least one container
container := &pod.Spec.Containers[index]

// inject env vars
for _, env := range apacheSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
container.appendEnvVarIfNotExists(&pod, env)
}

// First make a clone of the instrumented container to take the existing Apache configuration from
Expand All @@ -88,7 +82,7 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod

apacheConfDir := getApacheConfDir(apacheSpec.ConfigPath)

cloneContainer := container.DeepCopy()
cloneContainer := pod.Spec.Containers[container.index].DeepCopy()
cloneContainer.Name = apacheAgentCloneContainerName
cloneContainer.Command = []string{"/bin/sh", "-c"}
cloneContainer.Args = []string{"cp -r " + apacheConfDir + "/* " + apacheAgentConfDirFull}
Expand All @@ -109,24 +103,24 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
// drop volume mount with volume-provided Apache config from original container
// since it could over-write configuration provided by the injection
idxFound := -1
for idx, volume := range container.VolumeMounts {
for idx, volume := range pod.Spec.Containers[container.index].VolumeMounts {
if strings.Contains(volume.MountPath, apacheConfDir) { // potentially passes config, which we want to pass to init copy only
idxFound = idx
break
}
}
if idxFound >= 0 {
volumeMounts := container.VolumeMounts
volumeMounts := pod.Spec.Containers[container.index].VolumeMounts
volumeMounts = append(volumeMounts[:idxFound], volumeMounts[idxFound+1:]...)
container.VolumeMounts = volumeMounts
pod.Spec.Containers[container.index].VolumeMounts = volumeMounts
}

// Inject volumes info instrumented container - Apache config dir + Apache agent
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
pod.Spec.Containers[container.index].VolumeMounts = append(pod.Spec.Containers[container.index].VolumeMounts, corev1.VolumeMount{
Name: apacheAgentVolume,
MountPath: apacheAgentDirFull,
})
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
pod.Spec.Containers[container.index].VolumeMounts = append(pod.Spec.Containers[container.index].VolumeMounts, corev1.VolumeMount{
Name: apacheAgentConfigVolume,
MountPath: apacheConfDir,
})
Expand Down Expand Up @@ -157,7 +151,7 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
Env: []corev1.EnvVar{
{
Name: apacheAttributesEnvVar,
Value: getApacheOtelConfig(pod, useLabelsForResourceAttributes, apacheSpec, index, otlpEndpoint, resourceMap),
Value: getApacheOtelConfig(pod, useLabelsForResourceAttributes, apacheSpec, container.index, otlpEndpoint, resourceMap),
},
{Name: apacheServiceInstanceIdEnvVar,
ValueFrom: &corev1.EnvVarSource{
Expand Down
12 changes: 10 additions & 2 deletions pkg/instrumentation/apachehttpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package instrumentation

import (
"context"
"testing"

"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -417,7 +419,10 @@ func TestInjectApacheHttpdagent(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pod := injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, test.pod, false, 0, "http://otlp-endpoint:4317", resourceMap)
pod := test.pod
container, err := NewContainer(k8sClient, context.Background(), logr.Discard(), "req-namespace", &pod, 0)
require.NoError(t, err)
pod = injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, pod, false, container, "http://otlp-endpoint:4317", resourceMap)
assert.Equal(t, test.expected, pod)
})
}
Expand Down Expand Up @@ -527,7 +532,10 @@ func TestInjectApacheHttpdagentUnknownNamespace(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pod := injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, test.pod, false, 0, "http://otlp-endpoint:4317", resourceMap)
pod := test.pod
container, err := NewContainer(k8sClient, context.Background(), logr.Discard(), "", &pod, 0)
require.NoError(t, err)
pod = injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, pod, false, container, "http://otlp-endpoint:4317", resourceMap)
assert.Equal(t, test.expected, pod)
})
}
Expand Down
Loading
Loading