From 6bce72a794fbebb4bf5e953fa71ae064e76d1573 Mon Sep 17 00:00:00 2001 From: joey Date: Thu, 10 Oct 2024 11:51:34 +0800 Subject: [PATCH] fix eps named ports does not work in sidecar(initContainer with restartPolicy=Always) Signed-off-by: joey --- pkg/api/v1/pod/util.go | 11 ++++++ pkg/api/v1/pod/util_test.go | 73 ++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/pkg/api/v1/pod/util.go b/pkg/api/v1/pod/util.go index 2bd37c5b5a1..ba283df4163 100644 --- a/pkg/api/v1/pod/util.go +++ b/pkg/api/v1/pod/util.go @@ -41,6 +41,17 @@ func FindPort(pod *v1.Pod, svcPort *v1.ServicePort) (int, error) { } } } + // also support sidecar container (initContainer with restartPolicy=Always) + for _, container := range pod.Spec.InitContainers { + if container.RestartPolicy == nil || *container.RestartPolicy != v1.ContainerRestartPolicyAlways { + continue + } + for _, port := range container.Ports { + if port.Name == name && port.Protocol == svcPort.Protocol { + return int(port.ContainerPort), nil + } + } + } case intstr.Int: return portName.IntValue(), nil } diff --git a/pkg/api/v1/pod/util_test.go b/pkg/api/v1/pod/util_test.go index 225402fdaa7..a172f802742 100644 --- a/pkg/api/v1/pod/util_test.go +++ b/pkg/api/v1/pod/util_test.go @@ -32,12 +32,14 @@ import ( ) func TestFindPort(t *testing.T) { + restartAlways := v1.ContainerRestartPolicyAlways testCases := []struct { - name string - containers []v1.Container - port intstr.IntOrString - expected int - pass bool + name string + containers []v1.Container + initContainers []v1.Container + port intstr.IntOrString + expected int + pass bool }{{ name: "valid int, no ports", containers: []v1.Container{{}}, @@ -182,10 +184,69 @@ func TestFindPort(t *testing.T) { expected: 11, pass: true, }, + { + name: "Sidecar initContainer named port", + initContainers: []v1.Container{{ + RestartPolicy: &restartAlways, + Ports: []v1.ContainerPort{{ + Name: "a", + ContainerPort: 80, + HostPort: -1, + Protocol: "TCP", + }}, + }}, + port: intstr.FromString("a"), + expected: 80, + pass: true, + }, + { + name: "Invalid(restartPolicy != Always) initContainer named port", + initContainers: []v1.Container{{ + Ports: []v1.ContainerPort{{ + Name: "a", + ContainerPort: 80, + HostPort: -1, + Protocol: "TCP", + }}, + }}, + port: intstr.FromString("a"), + expected: 0, + pass: false, + }, + { + name: "App and sidecar containers have the same named port, first app container port will be used", + initContainers: []v1.Container{{ + RestartPolicy: &restartAlways, + Ports: []v1.ContainerPort{{ + Name: "a", + ContainerPort: 80, + HostPort: -1, + Protocol: "TCP", + }}, + }}, + containers: []v1.Container{{ + Ports: []v1.ContainerPort{{ + Name: "a", + ContainerPort: 81, + HostPort: -1, + Protocol: "TCP", + }}, + }, { + Ports: []v1.ContainerPort{{ + Name: "a", + ContainerPort: 82, + HostPort: -1, + Protocol: "TCP", + }}, + }}, + port: intstr.FromString("a"), + expected: 81, + pass: true, + }, } for _, tc := range testCases { - port, err := FindPort(&v1.Pod{Spec: v1.PodSpec{Containers: tc.containers}}, + port, err := FindPort(&v1.Pod{Spec: v1.PodSpec{Containers: tc.containers, InitContainers: tc.initContainers}}, &v1.ServicePort{Protocol: "TCP", TargetPort: tc.port}) if err != nil && tc.pass { t.Errorf("unexpected error for %s: %v", tc.name, err)