Skip to content

Commit 2b36f0d

Browse files
authored
Support configuring java runtime from configmap or secret (env.valueFrom) (#3379)
Signed-off-by: Pavol Loffay <[email protected]>
1 parent 22e8c06 commit 2b36f0d

File tree

12 files changed

+97
-43
lines changed

12 files changed

+97
-43
lines changed

.chloggen/1814-java-configmap.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: auto-instrumentation
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Support configuring Java auto-instrumentation when runtime configuration is provided from configmap or secret.
9+
10+
# One or more tracking issues related to the change
11+
issues: [1814]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: |
17+
This change allows users to configure JAVA_TOOL_OPTIONS in config map or secret.
18+
The operator in this case set another JAVA_TOOL_OPTIONS that references the original value
19+
e.g. `JAVA_TOOL_OPTIONS=$(JAVA_TOOL_OPTIONS) -javaagent:/otel-auto-instrumentation-java/javaagent.jar`.

config/manager/kustomization.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
resources:
22
- manager.yaml
3-

pkg/instrumentation/javaagent.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,17 @@ import (
2424

2525
const (
2626
envJavaToolsOptions = "JAVA_TOOL_OPTIONS"
27-
javaAgent = " -javaagent:/otel-auto-instrumentation-java/javaagent.jar"
27+
javaAgent = "-javaagent:/otel-auto-instrumentation-java/javaagent.jar"
2828
javaInitContainerName = initContainerName + "-java"
2929
javaVolumeName = volumeName + "-java"
3030
javaInstrMountPath = "/otel-auto-instrumentation-java"
3131
)
3232

33-
func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.Pod, error) {
33+
func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) corev1.Pod {
3434
volume := instrVolume(javaSpec.VolumeClaimTemplate, javaVolumeName, javaSpec.VolumeSizeLimit)
35-
3635
// caller checks if there is at least one container.
3736
container := &pod.Spec.Containers[index]
3837

39-
err := validateContainerEnv(container.Env, envJavaToolsOptions)
40-
if err != nil {
41-
return pod, err
42-
}
43-
4438
// inject Java instrumentation spec env vars.
4539
for _, env := range javaSpec.Env {
4640
idx := getIndexOfEnv(container.Env, env.Name)
@@ -55,14 +49,14 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
5549
}
5650

5751
idx := getIndexOfEnv(container.Env, envJavaToolsOptions)
58-
if idx == -1 {
59-
container.Env = append(container.Env, corev1.EnvVar{
60-
Name: envJavaToolsOptions,
61-
Value: javaJVMArgument,
62-
})
63-
} else {
64-
container.Env[idx].Value = container.Env[idx].Value + javaJVMArgument
52+
if idx != -1 {
53+
// https://kubernetes.io/docs/tasks/inject-data-application/define-interdependent-environment-variables/
54+
javaJVMArgument = fmt.Sprintf("$(%s) %s", envJavaToolsOptions, javaJVMArgument)
6555
}
56+
container.Env = append(container.Env, corev1.EnvVar{
57+
Name: envJavaToolsOptions,
58+
Value: javaJVMArgument,
59+
})
6660

6761
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
6862
Name: volume.Name,
@@ -97,5 +91,5 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
9791
}
9892

9993
}
100-
return pod, err
94+
return pod
10195
}

pkg/instrumentation/javaagent_test.go

+39-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package instrumentation
1616

1717
import (
18-
"fmt"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"
@@ -30,7 +29,6 @@ func TestInjectJavaagent(t *testing.T) {
3029
v1alpha1.Java
3130
pod corev1.Pod
3231
expected corev1.Pod
33-
err error
3432
}{
3533
{
3634
name: "JAVA_TOOL_OPTIONS not defined",
@@ -83,7 +81,6 @@ func TestInjectJavaagent(t *testing.T) {
8381
},
8482
},
8583
},
86-
err: nil,
8784
},
8885
{
8986
name: "add extensions to JAVA_TOOL_OPTIONS",
@@ -157,7 +154,6 @@ func TestInjectJavaagent(t *testing.T) {
157154
},
158155
},
159156
},
160-
err: nil,
161157
},
162158
{
163159
name: "JAVA_TOOL_OPTIONS defined",
@@ -211,18 +207,21 @@ func TestInjectJavaagent(t *testing.T) {
211207
Env: []corev1.EnvVar{
212208
{
213209
Name: "JAVA_TOOL_OPTIONS",
214-
Value: "-Dbaz=bar" + javaAgent,
210+
Value: "-Dbaz=bar",
211+
},
212+
{
213+
Name: "JAVA_TOOL_OPTIONS",
214+
Value: "$(JAVA_TOOL_OPTIONS) " + javaAgent,
215215
},
216216
},
217217
},
218218
},
219219
},
220220
},
221-
err: nil,
222221
},
223222
{
224223
name: "JAVA_TOOL_OPTIONS defined as ValueFrom",
225-
Java: v1alpha1.Java{Image: "foo/bar:1"},
224+
Java: v1alpha1.Java{Image: "foo/bar:1", Resources: testResourceRequirements},
226225
pod: corev1.Pod{
227226
Spec: corev1.PodSpec{
228227
Containers: []corev1.Container{
@@ -239,27 +238,57 @@ func TestInjectJavaagent(t *testing.T) {
239238
},
240239
expected: corev1.Pod{
241240
Spec: corev1.PodSpec{
241+
Volumes: []corev1.Volume{
242+
{
243+
Name: "opentelemetry-auto-instrumentation-java",
244+
VolumeSource: corev1.VolumeSource{
245+
EmptyDir: &corev1.EmptyDirVolumeSource{
246+
SizeLimit: &defaultVolumeLimitSize,
247+
},
248+
},
249+
},
250+
},
251+
InitContainers: []corev1.Container{
252+
{
253+
Name: "opentelemetry-auto-instrumentation-java",
254+
Image: "foo/bar:1",
255+
Command: []string{"cp", "/javaagent.jar", "/otel-auto-instrumentation-java/javaagent.jar"},
256+
VolumeMounts: []corev1.VolumeMount{{
257+
Name: "opentelemetry-auto-instrumentation-java",
258+
MountPath: "/otel-auto-instrumentation-java",
259+
}},
260+
Resources: testResourceRequirements,
261+
},
262+
},
242263
Containers: []corev1.Container{
243264
{
265+
VolumeMounts: []corev1.VolumeMount{
266+
{
267+
Name: "opentelemetry-auto-instrumentation-java",
268+
MountPath: "/otel-auto-instrumentation-java",
269+
},
270+
},
244271
Env: []corev1.EnvVar{
245272
{
246273
Name: "JAVA_TOOL_OPTIONS",
247274
ValueFrom: &corev1.EnvVarSource{},
248275
},
276+
{
277+
Name: "JAVA_TOOL_OPTIONS",
278+
Value: "$(JAVA_TOOL_OPTIONS) " + javaAgent,
279+
},
249280
},
250281
},
251282
},
252283
},
253284
},
254-
err: fmt.Errorf("the container defines env var value via ValueFrom, envVar: %s", envJavaToolsOptions),
255285
},
256286
}
257287

258288
for _, test := range tests {
259289
t.Run(test.name, func(t *testing.T) {
260-
pod, err := injectJavaagent(test.Java, test.pod, 0)
290+
pod := injectJavaagent(test.Java, test.pod, 0)
261291
assert.Equal(t, test.expected, pod)
262-
assert.Equal(t, test.err, err)
263292
})
264293
}
265294
}

pkg/instrumentation/sdk.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
5959
}
6060
if insts.Java.Instrumentation != nil {
6161
otelinst := *insts.Java.Instrumentation
62-
var err error
6362
i.logger.V(1).Info("injecting Java instrumentation into pod", "otelinst-namespace", otelinst.Namespace, "otelinst-name", otelinst.Name)
6463

6564
if len(insts.Java.Containers) == 0 {
@@ -68,14 +67,10 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
6867

6968
for _, container := range insts.Java.Containers {
7069
index := getContainerIndex(container, pod)
71-
pod, err = injectJavaagent(otelinst.Spec.Java, pod, index)
72-
if err != nil {
73-
i.logger.Info("Skipping javaagent injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
74-
} else {
75-
pod = i.injectCommonEnvVar(otelinst, pod, index)
76-
pod = i.injectCommonSDKConfig(ctx, otelinst, ns, pod, index, index)
77-
pod = i.setInitContainerSecurityContext(pod, pod.Spec.Containers[index].SecurityContext, javaInitContainerName)
78-
}
70+
pod = injectJavaagent(otelinst.Spec.Java, pod, index)
71+
pod = i.injectCommonEnvVar(otelinst, pod, index)
72+
pod = i.injectCommonSDKConfig(ctx, otelinst, ns, pod, index, index)
73+
pod = i.setInitContainerSecurityContext(pod, pod.Spec.Containers[index].SecurityContext, javaInitContainerName)
7974
}
8075
}
8176
if insts.NodeJS.Instrumentation != nil {

tests/e2e-instrumentation/instrumentation-java-multicontainer/01-assert.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spec:
2525
- name: SPLUNK_PROFILER_ENABLED
2626
value: "false"
2727
- name: JAVA_TOOL_OPTIONS
28-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
28+
value: '-javaagent:/otel-auto-instrumentation-java/javaagent.jar'
2929
- name: OTEL_TRACES_EXPORTER
3030
value: otlp
3131
- name: OTEL_EXPORTER_OTLP_ENDPOINT
@@ -75,7 +75,7 @@ spec:
7575
- name: SPLUNK_PROFILER_ENABLED
7676
value: "false"
7777
- name: JAVA_TOOL_OPTIONS
78-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
78+
value: '-javaagent:/otel-auto-instrumentation-java/javaagent.jar'
7979
- name: OTEL_TRACES_EXPORTER
8080
value: otlp
8181
- name: OTEL_EXPORTER_OTLP_ENDPOINT

tests/e2e-instrumentation/instrumentation-java-multicontainer/02-assert.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ spec:
3636
- name: SPLUNK_PROFILER_ENABLED
3737
value: "false"
3838
- name: JAVA_TOOL_OPTIONS
39-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
39+
value: '-javaagent:/otel-auto-instrumentation-java/javaagent.jar'
4040
- name: OTEL_TRACES_EXPORTER
4141
value: otlp
4242
- name: OTEL_EXPORTER_OTLP_ENDPOINT

tests/e2e-instrumentation/instrumentation-java-other-ns/03-assert.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ spec:
2424
- name: SPLUNK_PROFILER_ENABLED
2525
value: "false"
2626
- name: JAVA_TOOL_OPTIONS
27-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
27+
value: '-javaagent:/otel-auto-instrumentation-java/javaagent.jar'
2828
- name: OTEL_TRACES_EXPORTER
2929
value: otlp
3030
- name: OTEL_EXPORTER_OTLP_ENDPOINT

tests/e2e-instrumentation/instrumentation-java-tls/01-assert.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ spec:
1717
fieldRef:
1818
fieldPath: status.podIP
1919
- name: JAVA_TOOL_OPTIONS
20-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
20+
value: '-javaagent:/otel-auto-instrumentation-java/javaagent.jar'
2121
- name: OTEL_SERVICE_NAME
2222
value: my-java
2323
- name: OTEL_EXPORTER_OTLP_ENDPOINT

tests/e2e-instrumentation/instrumentation-java/01-assert.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ spec:
1717
valueFrom:
1818
fieldRef:
1919
fieldPath: status.podIP
20+
- name: JAVA_TOOL_OPTIONS
21+
valueFrom:
22+
configMapKeyRef:
23+
name: config-java
24+
key: system-properties
2025
- name: OTEL_JAVAAGENT_DEBUG
2126
value: "true"
2227
- name: OTEL_INSTRUMENTATION_JDBC_ENABLED
2328
value: "false"
2429
- name: SPLUNK_PROFILER_ENABLED
2530
value: "false"
2631
- name: JAVA_TOOL_OPTIONS
27-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
32+
value: '$(JAVA_TOOL_OPTIONS) -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
2833
- name: OTEL_TRACES_EXPORTER
2934
value: otlp
3035
- name: OTEL_EXPORTER_OTLP_ENDPOINT

tests/e2e-instrumentation/instrumentation-java/01-install-app.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: config-java
5+
data:
6+
system-properties: "-Xmx256m -Xms64m"
7+
---
18
apiVersion: apps/v1
29
kind: Deployment
310
metadata:
@@ -22,6 +29,12 @@ spec:
2229
containers:
2330
- name: myapp
2431
image: ghcr.io/open-telemetry/opentelemetry-operator/e2e-test-app-java:main
32+
env:
33+
- name: JAVA_TOOL_OPTIONS
34+
valueFrom:
35+
configMapKeyRef:
36+
name: config-java
37+
key: system-properties
2538
securityContext:
2639
allowPrivilegeEscalation: false
2740
capabilities:

tests/e2e-multi-instrumentation/instrumentation-multi-multicontainer/01-assert.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ spec:
8989
- name: OTEL_SERVICE_NAME
9090
value: javaapp
9191
- name: JAVA_TOOL_OPTIONS
92-
value: ' -javaagent:/otel-auto-instrumentation-java/javaagent.jar'
92+
value: '-javaagent:/otel-auto-instrumentation-java/javaagent.jar'
9393
- name: OTEL_TRACES_SAMPLER
9494
value: parentbased_traceidratio
9595
- name: OTEL_TRACES_SAMPLER_ARG

0 commit comments

Comments
 (0)