Skip to content

Commit

Permalink
fix(operator): truncate sidecar pod injected label to 63 chars
Browse files Browse the repository at this point in the history
Refs: #1032
  • Loading branch information
RoVernekar committed Oct 20, 2023
1 parent 6cc60bd commit 74211ad
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
16 changes: 16 additions & 0 deletions .chloggen/truncate-pod-label.yaml
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: Truncate `sidecar.opentelemetry.io/injected` sidecar pod label to 63 characters

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

# (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:
2 changes: 1 addition & 1 deletion internal/manifests/manifestutils/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Labels(instance metav1.ObjectMeta, name string, image string, component str
func SelectorLabels(instance metav1.ObjectMeta, component string) map[string]string {
return map[string]string{
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name),
"app.kubernetes.io/instance": naming.PodInstanceLabel(instance.Namespace, instance.Name),
"app.kubernetes.io/part-of": "opentelemetry",
"app.kubernetes.io/component": component,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/targetallocator/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Labels(instance v1alpha1.OpenTelemetryCollector, name string) map[string]st
}

base["app.kubernetes.io/managed-by"] = "opentelemetry-operator"
base["app.kubernetes.io/instance"] = naming.Truncate("%s.%s", 63, instance.Namespace, instance.Name)
base["app.kubernetes.io/instance"] = naming.PodInstanceLabel(instance.Namespace, instance.Name)
base["app.kubernetes.io/part-of"] = "opentelemetry"
base["app.kubernetes.io/component"] = "opentelemetry-targetallocator"

Expand Down
5 changes: 5 additions & 0 deletions internal/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ func ServiceMonitor(otelcol string) string {
func TargetAllocatorServiceAccount(otelcol string) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol))
}

// PodInstanceLabel returns a label value containing the namespace and instance name.
func PodInstanceLabel(namespace string, otelcol string) string {
return Truncate("%s.%s", 63, namespace, otelcol)
}
6 changes: 3 additions & 3 deletions pkg/sidecar/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
)

const (
label = "sidecar.opentelemetry.io/injected"
confEnvVar = "OTEL_CONFIG"
injectedLabel = "sidecar.opentelemetry.io/injected"
confEnvVar = "OTEL_CONFIG"
)

// add a new sidecar container to the given pod, based on the given OpenTelemetryCollector.
Expand All @@ -53,7 +53,7 @@ func add(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemetryCo
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
pod.Labels[label] = fmt.Sprintf("%s.%s", otelcol.Namespace, otelcol.Name)
pod.Labels[injectedLabel] = naming.PodInstanceLabel(otelcol.Namespace, otelcol.Name)

return pod, nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/sidecar/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAddSidecarWhenNoSidecarExists(t *testing.T) {
}
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "otelcol-sample",
Name: "otelcol-sample-with-a-name-that-is-longer-than-sixty-three-characters",
Namespace: "some-app",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Expand All @@ -74,7 +74,8 @@ processors:
require.Len(t, changed.Spec.Containers, 2)
require.Len(t, changed.Spec.InitContainers, 2)
require.Len(t, changed.Spec.Volumes, 1)
assert.Equal(t, "some-app.otelcol-sample", changed.Labels["sidecar.opentelemetry.io/injected"])
assert.Equal(t, "otelcol-sample-with-a-name-that-is-longer-than-sixty-three-cha",
changed.Labels["sidecar.opentelemetry.io/injected"])
assert.Equal(t, corev1.Container{
Name: "otc-container",
Image: "some-default-image",
Expand Down

0 comments on commit 74211ad

Please sign in to comment.