From 270c8333923a87b1f41bdea69d817b06a2bc5ac2 Mon Sep 17 00:00:00 2001
From: Pavol Loffay
Date: Tue, 24 Jan 2023 10:50:16 +0100
Subject: [PATCH] Inject otelcol sidecar into any namespace
Signed-off-by: Pavol Loffay
---
.chloggen/inject-any-namespace.yaml | 16 +++++++++
README.md | 9 ++++-
config/manager/kustomization.yaml | 2 +-
internal/config/main.go | 7 ++++
internal/config/main_test.go | 2 ++
internal/config/options.go | 6 ++++
.../webhookhandler/webhookhandler_test.go | 9 ++---
main.go | 3 ++
pkg/collector/reconcile/config_replace.go | 13 +++----
.../reconcile/config_replace_test.go | 4 +--
pkg/collector/reconcile/configmap.go | 2 +-
pkg/sidecar/pod.go | 35 ++++++++++++++++++-
pkg/sidecar/pod_test.go | 31 +++++++++++++++-
pkg/sidecar/podmutator.go | 9 ++++-
.../e2e/instrumentation-dotnet/01-assert.yaml | 1 +
.../01-assert.yaml | 1 +
tests/e2e/instrumentation-java/01-assert.yaml | 1 +
.../e2e/instrumentation-nodejs/01-assert.yaml | 1 +
.../e2e/instrumentation-python/01-assert.yaml | 1 +
.../00-install.yaml | 29 +++++++++++++++
.../01-assert.yaml | 24 +++++++++++++
.../01-install-app.yaml | 20 +++++++++++
tests/e2e/smoke-sidecar/01-assert.yaml | 4 +++
23 files changed, 212 insertions(+), 18 deletions(-)
create mode 100755 .chloggen/inject-any-namespace.yaml
create mode 100644 tests/e2e/smoke-sidecar-other-namespace/00-install.yaml
create mode 100644 tests/e2e/smoke-sidecar-other-namespace/01-assert.yaml
create mode 100644 tests/e2e/smoke-sidecar-other-namespace/01-install-app.yaml
diff --git a/.chloggen/inject-any-namespace.yaml b/.chloggen/inject-any-namespace.yaml
new file mode 100755
index 0000000000..9288e9c39d
--- /dev/null
+++ b/.chloggen/inject-any-namespace.yaml
@@ -0,0 +1,16 @@
+# 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. 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: Support sidecar injecton into any namespace.
+
+# One or more tracking issues related to the change
+issues: [199]
+
+# (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:
diff --git a/README.md b/README.md
index 39dbf9266e..840051d942 100644
--- a/README.md
+++ b/README.md
@@ -87,7 +87,7 @@ The `CustomResource` for the `OpenTelemetryCollector` exposes a property named `
#### Sidecar injection
-A sidecar with the OpenTelemetry Collector can be injected into pod-based workloads by setting the pod annotation `sidecar.opentelemetry.io/inject` to either `"true"`, or to the name of a concrete `OpenTelemetryCollector` from the same namespace, like in the following example:
+A sidecar with the OpenTelemetry Collector can be injected into pod-based workloads by setting the pod annotation `sidecar.opentelemetry.io/inject` to either `"true"`, or to the name of a concrete `OpenTelemetryCollector`, like in the following example:
```yaml
kubectl apply -f - < /conf/collector.yaml && cat /conf/collector.yaml"},
+ Env: []corev1.EnvVar{
+ {
+ Name: "OTEL_CONFIG",
+ Value: otelColCfgStr,
+ },
+ },
+ VolumeMounts: []corev1.VolumeMount{volumeMount},
+ })
+
container := collector.Container(cfg, logger, otelcol)
if !hasResourceAttributeEnvVar(container.Env) {
container.Env = append(container.Env, attributes...)
diff --git a/pkg/sidecar/pod_test.go b/pkg/sidecar/pod_test.go
index 1982e53153..83d5c4ed3d 100644
--- a/pkg/sidecar/pod_test.go
+++ b/pkg/sidecar/pod_test.go
@@ -15,9 +15,11 @@
package sidecar
import (
+ "encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -45,14 +47,41 @@ func TestAddSidecarWhenNoSidecarExists(t *testing.T) {
Name: "otelcol-sample",
Namespace: "some-app",
},
+ Spec: v1alpha1.OpenTelemetryCollectorSpec{
+ Config: `
+receivers:
+exporters:
+processors:
+`,
+ },
}
- cfg := config.New(config.WithCollectorImage("some-default-image"))
+ cfg := config.New(config.WithCollectorImage("some-default-image"), config.WithSidecarConfigPrepperImage("alpine:latest"))
// test
changed, err := add(cfg, logger, otelcol, pod, nil)
// verify
assert.NoError(t, err)
+ require.Len(t, changed.Spec.InitContainers, 1)
+ cfgBase64 := base64.StdEncoding.EncodeToString([]byte(otelcol.Spec.Config))
+ assert.Equal(t, changed.Spec.InitContainers[0], corev1.Container{
+ Name: "otc-container-config-prepper",
+ Image: "alpine:latest",
+ Command: []string{"/bin/sh"},
+ Args: []string{"-c", "echo ${OTEL_CONFIG} | base64 -d > /conf/collector.yaml && cat /conf/collector.yaml"},
+ Env: []corev1.EnvVar{
+ {
+ Name: "OTEL_CONFIG",
+ Value: cfgBase64,
+ },
+ },
+ VolumeMounts: []corev1.VolumeMount{
+ {
+ Name: "otc-internal",
+ MountPath: "/conf",
+ },
+ },
+ })
assert.Len(t, changed.Spec.Containers, 2)
assert.Len(t, changed.Spec.Volumes, 2)
assert.Equal(t, "some-app.otelcol-sample", changed.Labels["sidecar.opentelemetry.io/injected"])
diff --git a/pkg/sidecar/podmutator.go b/pkg/sidecar/podmutator.go
index 22302a44ea..7f1956e4dd 100644
--- a/pkg/sidecar/podmutator.go
+++ b/pkg/sidecar/podmutator.go
@@ -106,7 +106,14 @@ func (p *sidecarPodMutator) getCollectorInstance(ctx context.Context, ns corev1.
}
otelcol := v1alpha1.OpenTelemetryCollector{}
- err := p.client.Get(ctx, types.NamespacedName{Name: ann, Namespace: ns.Name}, &otelcol)
+ var nsnOtelcol types.NamespacedName
+ instNamespace, instName, namespaced := strings.Cut(ann, "/")
+ if namespaced {
+ nsnOtelcol = types.NamespacedName{Name: instName, Namespace: instNamespace}
+ } else {
+ nsnOtelcol = types.NamespacedName{Name: ann, Namespace: ns.Name}
+ }
+ err := p.client.Get(ctx, nsnOtelcol, &otelcol)
if err != nil {
return otelcol, err
}
diff --git a/tests/e2e/instrumentation-dotnet/01-assert.yaml b/tests/e2e/instrumentation-dotnet/01-assert.yaml
index 2705499653..4286729fc5 100644
--- a/tests/e2e/instrumentation-dotnet/01-assert.yaml
+++ b/tests/e2e/instrumentation-dotnet/01-assert.yaml
@@ -49,6 +49,7 @@ spec:
name: opentelemetry-auto-instrumentation
- name: otc-container
initContainers:
+ - name: otc-container-config-prepper
- name: opentelemetry-auto-instrumentation
status:
phase: Running
diff --git a/tests/e2e/instrumentation-java-other-ns/01-assert.yaml b/tests/e2e/instrumentation-java-other-ns/01-assert.yaml
index fc4dd75353..de00d8b892 100644
--- a/tests/e2e/instrumentation-java-other-ns/01-assert.yaml
+++ b/tests/e2e/instrumentation-java-other-ns/01-assert.yaml
@@ -43,6 +43,7 @@ spec:
name: opentelemetry-auto-instrumentation
- name: otc-container
initContainers:
+ - name: otc-container-config-prepper
- name: opentelemetry-auto-instrumentation
status:
phase: Running
diff --git a/tests/e2e/instrumentation-java/01-assert.yaml b/tests/e2e/instrumentation-java/01-assert.yaml
index d3505b046c..b97d3ffe59 100644
--- a/tests/e2e/instrumentation-java/01-assert.yaml
+++ b/tests/e2e/instrumentation-java/01-assert.yaml
@@ -43,6 +43,7 @@ spec:
name: opentelemetry-auto-instrumentation
- name: otc-container
initContainers:
+ - name: otc-container-config-prepper
- name: opentelemetry-auto-instrumentation
status:
phase: Running
diff --git a/tests/e2e/instrumentation-nodejs/01-assert.yaml b/tests/e2e/instrumentation-nodejs/01-assert.yaml
index 3190895a59..b405897bc6 100644
--- a/tests/e2e/instrumentation-nodejs/01-assert.yaml
+++ b/tests/e2e/instrumentation-nodejs/01-assert.yaml
@@ -39,6 +39,7 @@ spec:
name: opentelemetry-auto-instrumentation
- name: otc-container
initContainers:
+ - name: otc-container-config-prepper
- name: opentelemetry-auto-instrumentation
status:
phase: Running
diff --git a/tests/e2e/instrumentation-python/01-assert.yaml b/tests/e2e/instrumentation-python/01-assert.yaml
index 4b7c122ae0..d7b11df9f2 100644
--- a/tests/e2e/instrumentation-python/01-assert.yaml
+++ b/tests/e2e/instrumentation-python/01-assert.yaml
@@ -45,6 +45,7 @@ spec:
name: opentelemetry-auto-instrumentation
- name: otc-container
initContainers:
+ - name: otc-container-config-prepper
- name: opentelemetry-auto-instrumentation
status:
phase: Running
diff --git a/tests/e2e/smoke-sidecar-other-namespace/00-install.yaml b/tests/e2e/smoke-sidecar-other-namespace/00-install.yaml
new file mode 100644
index 0000000000..9f64141343
--- /dev/null
+++ b/tests/e2e/smoke-sidecar-other-namespace/00-install.yaml
@@ -0,0 +1,29 @@
+---
+apiVersion: v1
+kind: Namespace
+metadata:
+ name: kuttl-otel-sidecar-other-namespace
+---
+apiVersion: opentelemetry.io/v1alpha1
+kind: OpenTelemetryCollector
+metadata:
+ name: sidecar-for-my-app
+ namespace: kuttl-otel-sidecar-other-namespace
+spec:
+ mode: sidecar
+ config: |
+ receivers:
+ jaeger:
+ protocols:
+ grpc:
+ processors:
+
+ exporters:
+ logging:
+
+ service:
+ pipelines:
+ traces:
+ receivers: [jaeger]
+ processors: []
+ exporters: [logging]
diff --git a/tests/e2e/smoke-sidecar-other-namespace/01-assert.yaml b/tests/e2e/smoke-sidecar-other-namespace/01-assert.yaml
new file mode 100644
index 0000000000..db7959b65c
--- /dev/null
+++ b/tests/e2e/smoke-sidecar-other-namespace/01-assert.yaml
@@ -0,0 +1,24 @@
+apiVersion: v1
+kind: Pod
+metadata:
+ namespace: kuttl-otel-sidecar-other-namespace
+ annotations:
+ sidecar.opentelemetry.io/inject: "kuttl-otel-sidecar-other-namespace/sidecar-for-my-app"
+ labels:
+ app: my-pod-with-sidecar
+spec:
+ containers:
+ - name: myapp
+ - name: otc-container
+ env:
+ - name: POD_NAME
+ - name: OTEL_RESOURCE_ATTRIBUTES_POD_NAME
+ - name: OTEL_RESOURCE_ATTRIBUTES_POD_UID
+ - name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME
+ - name: OTEL_RESOURCE_ATTRIBUTES
+ initContainers:
+ - name: otc-container-config-prepper
+ env:
+ - name: OTEL_CONFIG
+status:
+ phase: Running
diff --git a/tests/e2e/smoke-sidecar-other-namespace/01-install-app.yaml b/tests/e2e/smoke-sidecar-other-namespace/01-install-app.yaml
new file mode 100644
index 0000000000..b85e38b91d
--- /dev/null
+++ b/tests/e2e/smoke-sidecar-other-namespace/01-install-app.yaml
@@ -0,0 +1,20 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: my-deployment-with-sidecar
+ namespace: kuttl-otel-sidecar-other-namespace
+spec:
+ selector:
+ matchLabels:
+ app: my-pod-with-sidecar
+ replicas: 1
+ template:
+ metadata:
+ labels:
+ app: my-pod-with-sidecar
+ annotations:
+ sidecar.opentelemetry.io/inject: "kuttl-otel-sidecar-other-namespace/sidecar-for-my-app"
+ spec:
+ containers:
+ - name: myapp
+ image: k8s.gcr.io/echoserver:1.4
diff --git a/tests/e2e/smoke-sidecar/01-assert.yaml b/tests/e2e/smoke-sidecar/01-assert.yaml
index 358711a288..ee8c6fb394 100644
--- a/tests/e2e/smoke-sidecar/01-assert.yaml
+++ b/tests/e2e/smoke-sidecar/01-assert.yaml
@@ -15,5 +15,9 @@ spec:
- name: OTEL_RESOURCE_ATTRIBUTES_POD_UID
- name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME
- name: OTEL_RESOURCE_ATTRIBUTES
+ initContainers:
+ - name: otc-container-config-prepper
+ env:
+ - name: OTEL_CONFIG
status:
phase: Running