@@ -32,12 +32,14 @@ import (
32
32
)
33
33
34
34
func TestFindPort (t * testing.T ) {
35
+ restartAlways := v1 .ContainerRestartPolicyAlways
35
36
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
41
43
}{{
42
44
name : "valid int, no ports" ,
43
45
containers : []v1.Container {{}},
@@ -182,10 +184,69 @@ func TestFindPort(t *testing.T) {
182
184
expected : 11 ,
183
185
pass : true ,
184
186
},
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
+ },
185
246
}
186
247
187
248
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 }},
189
250
& v1.ServicePort {Protocol : "TCP" , TargetPort : tc .port })
190
251
if err != nil && tc .pass {
191
252
t .Errorf ("unexpected error for %s: %v" , tc .name , err )
0 commit comments