Skip to content

Commit cfed899

Browse files
authored
fix(proxy-injector): handle nil pointer in inject (#14631)
The injector was dereferencing a nil pointer when the linkerd-config missed a 'tracing' section. This fix handles the nil pointer case explicitly and adds tests to cover this scenario.
1 parent 834b32e commit cfed899

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

pkg/inject/inject.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -554,16 +554,22 @@ func ApplyAnnotationOverrides(values *l5dcharts.Values, annotations map[string]s
554554
values.Proxy.AccessLog = override
555555
}
556556

557-
if override, ok := labels[k8s.TracingInstanceLabel]; ok {
558-
values.Proxy.Tracing.Labels[k8s.TracingServiceName] = override
559-
} else if override, ok := labels[k8s.TracingNameLabel]; ok {
560-
values.Proxy.Tracing.Labels[k8s.TracingServiceName] = override
561-
}
557+
if values.Proxy.Tracing != nil {
558+
if values.Proxy.Tracing.Labels == nil {
559+
values.Proxy.Tracing.Labels = make(map[string]string)
560+
}
562561

563-
for name, value := range annotations {
564-
after, found := strings.CutPrefix(name, k8s.TracingSemanticConventionPrefix)
565-
if found {
566-
values.Proxy.Tracing.Labels[after] = value
562+
if override, ok := labels[k8s.TracingInstanceLabel]; ok {
563+
values.Proxy.Tracing.Labels[k8s.TracingServiceName] = override
564+
} else if override, ok := labels[k8s.TracingNameLabel]; ok {
565+
values.Proxy.Tracing.Labels[k8s.TracingServiceName] = override
566+
}
567+
568+
for name, value := range annotations {
569+
after, found := strings.CutPrefix(name, k8s.TracingSemanticConventionPrefix)
570+
if found {
571+
values.Proxy.Tracing.Labels[after] = value
572+
}
567573
}
568574
}
569575
}

pkg/inject/inject_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,67 @@ func TestGetOverriddenValues(t *testing.T) {
356356
}
357357
}
358358

359+
func TestApplyAnnotationOverridesMissingProxyTracing(t *testing.T) {
360+
values := &l5dcharts.Values{
361+
Proxy: &l5dcharts.Proxy{
362+
Image: &l5dcharts.Image{},
363+
Ports: &l5dcharts.Ports{},
364+
Metrics: &l5dcharts.ProxyMetrics{},
365+
},
366+
ProxyInit: &l5dcharts.ProxyInit{
367+
Image: &l5dcharts.Image{},
368+
},
369+
}
370+
371+
annotations := map[string]string{
372+
k8s.TracingSemanticConventionPrefix + "http.method": "GET",
373+
}
374+
labels := map[string]string{
375+
k8s.TracingInstanceLabel: "example",
376+
}
377+
378+
ApplyAnnotationOverrides(values, annotations, labels, nil)
379+
380+
if values.Proxy.Tracing != nil {
381+
t.Fatalf("expected tracing to remain nil, got %#v", values.Proxy.Tracing)
382+
}
383+
}
384+
385+
func TestApplyAnnotationOverridesInitializesTracingLabels(t *testing.T) {
386+
values := &l5dcharts.Values{
387+
Proxy: &l5dcharts.Proxy{
388+
Tracing: &l5dcharts.Tracing{},
389+
Image: &l5dcharts.Image{},
390+
Ports: &l5dcharts.Ports{},
391+
Metrics: &l5dcharts.ProxyMetrics{},
392+
},
393+
ProxyInit: &l5dcharts.ProxyInit{
394+
Image: &l5dcharts.Image{},
395+
},
396+
}
397+
398+
annotations := map[string]string{
399+
k8s.TracingSemanticConventionPrefix + "http.target": "/health",
400+
}
401+
labels := map[string]string{
402+
k8s.TracingNameLabel: "workload",
403+
}
404+
405+
ApplyAnnotationOverrides(values, annotations, labels, nil)
406+
407+
if values.Proxy.Tracing == nil {
408+
t.Fatal("expected tracing to be initialized")
409+
}
410+
411+
if got := values.Proxy.Tracing.Labels[k8s.TracingServiceName]; got != "workload" {
412+
t.Fatalf("expected tracing service name to be set, got %q", got)
413+
}
414+
415+
if got := values.Proxy.Tracing.Labels["http.target"]; got != "/health" {
416+
t.Fatalf("expected tracing semantic convention label to be set, got %q", got)
417+
}
418+
}
419+
359420
func TestWholeCPUCores(t *testing.T) {
360421
for _, c := range []struct {
361422
v string

0 commit comments

Comments
 (0)