Merge pull request #57460 from dixudx/validate_initcontainer_hostport
Automatic merge from submit-queue (batch tested with PRs 62951, 57460, 63118). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. fix hostport checking for initContainers since they run in sequential order **What this PR does / why we need it**: Fix hostport checking for initContainers since they run in sequential order **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: partial Fixes #57443 **Special notes for your reviewer**: /assign @CaoShuFeng @dims **Release note**: ```release-note None ```
This commit is contained in:
@@ -5163,7 +5163,7 @@ func TestValidateContainers(t *testing.T) {
|
||||
},
|
||||
{Name: "abc-1234", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: "File", SecurityContext: fakeValidSecurityContext(true)},
|
||||
}
|
||||
if errs := validateContainers(successCase, volumeDevices, field.NewPath("field")); len(errs) != 0 {
|
||||
if errs := validateContainers(successCase, false, volumeDevices, field.NewPath("field")); len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
|
||||
@@ -5390,7 +5390,67 @@ func TestValidateContainers(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
if errs := validateContainers(v, volumeDevices, field.NewPath("field")); len(errs) == 0 {
|
||||
if errs := validateContainers(v, false, volumeDevices, field.NewPath("field")); len(errs) == 0 {
|
||||
t.Errorf("expected failure for %s", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateInitContainers(t *testing.T) {
|
||||
volumeDevices := make(map[string]core.VolumeSource)
|
||||
capabilities.SetForTests(capabilities.Capabilities{
|
||||
AllowPrivileged: true,
|
||||
})
|
||||
|
||||
successCase := []core.Container{
|
||||
{
|
||||
Name: "container-1-same-host-port-different-protocol",
|
||||
Image: "image",
|
||||
Ports: []core.ContainerPort{
|
||||
{ContainerPort: 80, HostPort: 80, Protocol: "TCP"},
|
||||
{ContainerPort: 80, HostPort: 80, Protocol: "UDP"},
|
||||
},
|
||||
ImagePullPolicy: "IfNotPresent",
|
||||
TerminationMessagePolicy: "File",
|
||||
},
|
||||
{
|
||||
Name: "container-2-same-host-port-different-protocol",
|
||||
Image: "image",
|
||||
Ports: []core.ContainerPort{
|
||||
{ContainerPort: 80, HostPort: 80, Protocol: "TCP"},
|
||||
{ContainerPort: 80, HostPort: 80, Protocol: "UDP"},
|
||||
},
|
||||
ImagePullPolicy: "IfNotPresent",
|
||||
TerminationMessagePolicy: "File",
|
||||
},
|
||||
}
|
||||
if errs := validateContainers(successCase, true, volumeDevices, field.NewPath("field")); len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
|
||||
capabilities.SetForTests(capabilities.Capabilities{
|
||||
AllowPrivileged: false,
|
||||
})
|
||||
errorCases := map[string][]core.Container{
|
||||
"duplicate ports": {
|
||||
{
|
||||
Name: "abc",
|
||||
Image: "image",
|
||||
Ports: []core.ContainerPort{
|
||||
{
|
||||
ContainerPort: 8080, HostPort: 8080, Protocol: "TCP",
|
||||
},
|
||||
{
|
||||
ContainerPort: 8080, HostPort: 8080, Protocol: "TCP",
|
||||
},
|
||||
},
|
||||
ImagePullPolicy: "IfNotPresent",
|
||||
TerminationMessagePolicy: "File",
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
if errs := validateContainers(v, true, volumeDevices, field.NewPath("field")); len(errs) == 0 {
|
||||
t.Errorf("expected failure for %s", k)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user