Skip to content

Commit a3f578d

Browse files
committed
add pod containers[*].port duplicate warning
1 parent f3ae27f commit a3f578d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pkg/api/pod/warnings.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta
243243
}
244244
}
245245
}
246+
// duplicate containers[*].ports
247+
if len(c.Ports) > 1 {
248+
ports := sets.NewInt64()
249+
for i, port := range c.Ports {
250+
if ports.Has(int64(port.ContainerPort)) {
251+
warnings = append(warnings, fmt.Sprintf("%s: duplicate container port %d", p.Child("ports").Index(i).Child("containerPort"), port.ContainerPort))
252+
} else {
253+
ports.Insert(int64(port.ContainerPort))
254+
}
255+
}
256+
}
246257
return true
247258
})
248259

pkg/api/pod/warnings_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,58 @@ func TestWarnings(t *testing.T) {
459459
},
460460
expected: []string{},
461461
},
462+
{
463+
name: "no duplicate container ports",
464+
template: &api.PodTemplateSpec{Spec: api.PodSpec{
465+
Containers: []api.Container{{
466+
Name: "foo",
467+
Ports: []api.ContainerPort{
468+
{ContainerPort: 80, HostPort: 80},
469+
},
470+
}},
471+
}},
472+
expected: []string{},
473+
},
474+
{
475+
name: "duplicate container ports",
476+
template: &api.PodTemplateSpec{Spec: api.PodSpec{
477+
Containers: []api.Container{{
478+
Name: "foo",
479+
Ports: []api.ContainerPort{
480+
{ContainerPort: 80, HostPort: 80},
481+
{ContainerPort: 80, HostPort: 80},
482+
},
483+
}},
484+
}},
485+
expected: []string{
486+
`spec.containers[0].ports[1].containerPort: duplicate container port 80`,
487+
},
488+
},
489+
{
490+
name: "duplicate container ports in two containers",
491+
template: &api.PodTemplateSpec{Spec: api.PodSpec{
492+
Containers: []api.Container{
493+
{
494+
Name: "foo1",
495+
Ports: []api.ContainerPort{
496+
{ContainerPort: 80, HostPort: 80},
497+
{ContainerPort: 180, HostPort: 80},
498+
{ContainerPort: 80, HostPort: 80},
499+
},
500+
},
501+
{
502+
Name: "foo",
503+
Ports: []api.ContainerPort{
504+
{ContainerPort: 80, HostPort: 80},
505+
{ContainerPort: 80, HostPort: 80},
506+
},
507+
}},
508+
}},
509+
expected: []string{
510+
`spec.containers[0].ports[2].containerPort: duplicate container port 80`,
511+
`spec.containers[1].ports[1].containerPort: duplicate container port 80`,
512+
},
513+
},
462514
}
463515

464516
for _, tc := range testcases {

0 commit comments

Comments
 (0)