Skip to content

Commit

Permalink
Avoid rerunning the pod mutator for the instrumentation injection mor…
Browse files Browse the repository at this point in the history
…e than one time (#2048)

* Avoid running the instrumentation pod mutator for already instrumented pods

Signed-off-by: Israel Blancas <[email protected]>

* Add missing changelog

Signed-off-by: Israel Blancas <[email protected]>

* Fix E2E tests

Signed-off-by: Israel Blancas <[email protected]>

* Apply changes requested in code review

Signed-off-by: Israel Blancas <[email protected]>

* Add unit test for hte golang and env variable situations

Signed-off-by: Israel Blancas <[email protected]>

* Fix lint

Signed-off-by: Israel Blancas <[email protected]>

---------

Signed-off-by: Israel Blancas <[email protected]>
  • Loading branch information
iblancasa committed Aug 29, 2023
1 parent d3ebde2 commit e1b0305
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Avoid running the auto-instrumentation pod mutator for pods already auto-instrumented"

# One or more tracking issues related to the change
issues: [1366]

# (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:
18 changes: 17 additions & 1 deletion pkg/instrumentation/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package instrumentation

import (
corev1 "k8s.io/api/core/v1"

"github.com/open-telemetry/opentelemetry-operator/internal/naming"
"github.com/open-telemetry/opentelemetry-operator/pkg/constants"
)

// Calculate if we already inject InitContainers.
Expand All @@ -35,11 +38,24 @@ func isAutoInstrumentationInjected(pod corev1.Pod) bool {
return true
}
}
// Go uses a side car

for _, cont := range pod.Spec.Containers {
// Go uses a sidecar
if cont.Name == sideCarName {
return true
}

// This environment variable is set in the sidecar and in the
// collector containers. We look for it in any container that is not
// the sidecar container to check if we already injected the
// instrumentation or not
if cont.Name != naming.Container() {
for _, envVar := range cont.Env {
if envVar.Name == constants.EnvNodeName {
return true
}
}
}
}
return false
}
36 changes: 36 additions & 0 deletions pkg/instrumentation/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"

"github.com/open-telemetry/opentelemetry-operator/pkg/constants"
)

func TestInitContainerMissing(t *testing.T) {
Expand Down Expand Up @@ -95,6 +97,40 @@ func TestAutoInstrumentationInjected(t *testing.T) {
},
expected: true,
},
{
name: "AutoInstrumentation_Already_Inject_go",
pod: corev1.Pod{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{},
Containers: []corev1.Container{
{
Name: sideCarName,
},
},
},
},
expected: true,
},
{
name: "AutoInstrumentation_Already_Inject_no_init_containers",
pod: corev1.Pod{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{},
Containers: []corev1.Container{
{
Name: "my-app",
Env: []corev1.EnvVar{
{
Name: constants.EnvNodeName,
Value: "value",
},
},
},
},
},
},
expected: true,
},
{
name: "AutoInstrumentation_Absent_1",
pod: corev1.Pod{
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/instrumentation-sdk/00-install-collector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ spec:
receivers: [otlp]
processors: []
exporters: [logging]
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
# Annotate the namespace to allow the application to run using an specific group and user in OpenShift
# https://docs.openshift.com/dedicated/authentication/managing-security-context-constraints.html
# This annotation has no effect in Kubernetes
- command: kubectl annotate namespace ${NAMESPACE} openshift.io/sa.scc.uid-range=1000/1000 --overwrite
- command: kubectl annotate namespace ${NAMESPACE} openshift.io/sa.scc.supplemental-groups=2000/1000 --overwrite

0 comments on commit e1b0305

Please sign in to comment.