Skip to content

Commit c7a35ee

Browse files
authored
fix(cli): improved support for native sidecar servers in linkerd check --proxy (#14779)
(Extracted from #14566) The "opaque ports are properly annotated" check had a bug where it only validated regular containers, missing ports in init containers (native sidecars). This meant mismatched annotations between pods and services could go undetected when the port belonged to an init container. When a service had the opaque-ports annotation but the corresponding pod did not, the check would incorrectly pass if the port was defined in an init container instead of a regular container. This commit extends the check to validate ports in both regular containers and init containers, ensuring consistent opaque-ports annotations across pods and services regardless of where the port is defined.
1 parent aa193ab commit c7a35ee

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

pkg/healthcheck/healthcheck.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ func misconfiguredOpaqueAnnotation(service *corev1.Service, pod *corev1.Pod) err
24922492
func checkPodPorts(service *corev1.Service, pod *corev1.Pod, podPorts []string, port int) error {
24932493
for _, sp := range service.Spec.Ports {
24942494
if int(sp.Port) == port {
2495-
for _, c := range pod.Spec.Containers {
2495+
for _, c := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
24962496
for _, cp := range c.Ports {
24972497
if cp.ContainerPort == sp.TargetPort.IntVal || cp.Name == sp.TargetPort.StrVal {
24982498
// The pod exposes a container port that would be
@@ -2544,7 +2544,7 @@ func checkServiceNamePorts(service *corev1.Service, pod *corev1.Pod, port int, s
25442544
// port to check.
25452545
continue
25462546
}
2547-
for _, c := range pod.Spec.Containers {
2547+
for _, c := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
25482548
for _, cp := range c.Ports {
25492549
if int(cp.ContainerPort) == port {
25502550
// This is the containerPort that maps to the opaque port

pkg/healthcheck/healthcheck_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,6 +2973,62 @@ subsets:
29732973
},
29742974
expected: nil,
29752975
},
2976+
{
2977+
resources: []string{`
2978+
apiVersion: v1
2979+
kind: Service
2980+
metadata:
2981+
name: svc
2982+
namespace: test-ns
2983+
annotations:
2984+
config.linkerd.io/opaque-ports: "9200"
2985+
spec:
2986+
selector:
2987+
app: test
2988+
ports:
2989+
- name: test
2990+
port: 9200
2991+
targetPort: 9200
2992+
`,
2993+
`
2994+
apiVersion: v1
2995+
kind: Pod
2996+
metadata:
2997+
name: pod
2998+
namespace: test-ns
2999+
labels:
3000+
app: test
3001+
spec:
3002+
initContainers:
3003+
- name: test
3004+
image: test
3005+
restartPolicy: Always
3006+
ports:
3007+
- name: test
3008+
containerPort: 9200
3009+
`,
3010+
`
3011+
apiVersion: v1
3012+
kind: Endpoints
3013+
metadata:
3014+
name: svc
3015+
namespace: test-ns
3016+
subsets:
3017+
- addresses:
3018+
- ip: 10.244.3.12
3019+
nodeName: nod
3020+
targetRef:
3021+
kind: Pod
3022+
name: pod
3023+
namespace: test-ns
3024+
ports:
3025+
- name: test
3026+
port: 9200
3027+
protocol: TCP
3028+
`,
3029+
},
3030+
expected: fmt.Errorf("\t* service svc expects target port 9200 to be opaque; add it to pod pod config.linkerd.io/opaque-ports annotation"),
3031+
},
29763032
}
29773033

29783034
for i, tc := range testCases {

0 commit comments

Comments
 (0)