Skip to content

Commit c45f3ab

Browse files
authored
Merge pull request kubernetes#127976 from chengjoey/fix/named-ports
fix eps named ports does not work in sidecar(initContainer with restartPolicy=Always)
2 parents 2f7df33 + 6bce72a commit c45f3ab

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

pkg/api/v1/pod/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ func FindPort(pod *v1.Pod, svcPort *v1.ServicePort) (int, error) {
4141
}
4242
}
4343
}
44+
// also support sidecar container (initContainer with restartPolicy=Always)
45+
for _, container := range pod.Spec.InitContainers {
46+
if container.RestartPolicy == nil || *container.RestartPolicy != v1.ContainerRestartPolicyAlways {
47+
continue
48+
}
49+
for _, port := range container.Ports {
50+
if port.Name == name && port.Protocol == svcPort.Protocol {
51+
return int(port.ContainerPort), nil
52+
}
53+
}
54+
}
4455
case intstr.Int:
4556
return portName.IntValue(), nil
4657
}

pkg/api/v1/pod/util_test.go

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ import (
3232
)
3333

3434
func TestFindPort(t *testing.T) {
35+
restartAlways := v1.ContainerRestartPolicyAlways
3536
testCases := []struct {
36-
name string
37-
containers []v1.Container
38-
port intstr.IntOrString
39-
expected int
40-
pass bool
37+
name string
38+
containers []v1.Container
39+
initContainers []v1.Container
40+
port intstr.IntOrString
41+
expected int
42+
pass bool
4143
}{{
4244
name: "valid int, no ports",
4345
containers: []v1.Container{{}},
@@ -182,10 +184,69 @@ func TestFindPort(t *testing.T) {
182184
expected: 11,
183185
pass: true,
184186
},
187+
{
188+
name: "Sidecar initContainer named port",
189+
initContainers: []v1.Container{{
190+
RestartPolicy: &restartAlways,
191+
Ports: []v1.ContainerPort{{
192+
Name: "a",
193+
ContainerPort: 80,
194+
HostPort: -1,
195+
Protocol: "TCP",
196+
}},
197+
}},
198+
port: intstr.FromString("a"),
199+
expected: 80,
200+
pass: true,
201+
},
202+
{
203+
name: "Invalid(restartPolicy != Always) initContainer named port",
204+
initContainers: []v1.Container{{
205+
Ports: []v1.ContainerPort{{
206+
Name: "a",
207+
ContainerPort: 80,
208+
HostPort: -1,
209+
Protocol: "TCP",
210+
}},
211+
}},
212+
port: intstr.FromString("a"),
213+
expected: 0,
214+
pass: false,
215+
},
216+
{
217+
name: "App and sidecar containers have the same named port, first app container port will be used",
218+
initContainers: []v1.Container{{
219+
RestartPolicy: &restartAlways,
220+
Ports: []v1.ContainerPort{{
221+
Name: "a",
222+
ContainerPort: 80,
223+
HostPort: -1,
224+
Protocol: "TCP",
225+
}},
226+
}},
227+
containers: []v1.Container{{
228+
Ports: []v1.ContainerPort{{
229+
Name: "a",
230+
ContainerPort: 81,
231+
HostPort: -1,
232+
Protocol: "TCP",
233+
}},
234+
}, {
235+
Ports: []v1.ContainerPort{{
236+
Name: "a",
237+
ContainerPort: 82,
238+
HostPort: -1,
239+
Protocol: "TCP",
240+
}},
241+
}},
242+
port: intstr.FromString("a"),
243+
expected: 81,
244+
pass: true,
245+
},
185246
}
186247

187248
for _, tc := range testCases {
188-
port, err := FindPort(&v1.Pod{Spec: v1.PodSpec{Containers: tc.containers}},
249+
port, err := FindPort(&v1.Pod{Spec: v1.PodSpec{Containers: tc.containers, InitContainers: tc.initContainers}},
189250
&v1.ServicePort{Protocol: "TCP", TargetPort: tc.port})
190251
if err != nil && tc.pass {
191252
t.Errorf("unexpected error for %s: %v", tc.name, err)

0 commit comments

Comments
 (0)