fix eps named ports does not work in sidecar(initContainer with restartPolicy=Always)

Signed-off-by: joey <zchengjoey@gmail.com>
This commit is contained in:
joey 2024-10-10 11:51:34 +08:00
parent 582dcc2aca
commit 6bce72a794
No known key found for this signature in database
GPG Key ID: B5AE69C27B241A78
2 changed files with 78 additions and 6 deletions

View File

@ -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: case intstr.Int:
return portName.IntValue(), nil return portName.IntValue(), nil
} }

View File

@ -32,12 +32,14 @@ import (
) )
func TestFindPort(t *testing.T) { func TestFindPort(t *testing.T) {
restartAlways := v1.ContainerRestartPolicyAlways
testCases := []struct { testCases := []struct {
name string name string
containers []v1.Container containers []v1.Container
port intstr.IntOrString initContainers []v1.Container
expected int port intstr.IntOrString
pass bool expected int
pass bool
}{{ }{{
name: "valid int, no ports", name: "valid int, no ports",
containers: []v1.Container{{}}, containers: []v1.Container{{}},
@ -182,10 +184,69 @@ func TestFindPort(t *testing.T) {
expected: 11, expected: 11,
pass: true, 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 { 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}) &v1.ServicePort{Protocol: "TCP", TargetPort: tc.port})
if err != nil && tc.pass { if err != nil && tc.pass {
t.Errorf("unexpected error for %s: %v", tc.name, err) t.Errorf("unexpected error for %s: %v", tc.name, err)