diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index f5f61eb290e..7f22a3238f7 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -1909,6 +1909,16 @@ func validateHostNetwork(hostNetwork bool, containers []api.Container, fldPath * return allErrors } +func validateHostNetworkNoHostAliases(hostNetwork bool, hostAliases []api.HostAlias, fldPath *field.Path) field.ErrorList { + allErrors := field.ErrorList{} + if hostNetwork { + if len(hostAliases) > 0 { + allErrors = append(allErrors, field.Forbidden(fldPath, "may not be set when `hostNetwork` is true")) + } + } + return allErrors +} + // validateImagePullSecrets checks to make sure the pull secrets are well // formed. Right now, we only expect name to be set (it's the only field). If // this ever changes and someone decides to set those fields, we'd like to @@ -2401,6 +2411,7 @@ func ValidatePodSecurityContext(securityContext *api.PodSecurityContext, spec *a if securityContext != nil { allErrs = append(allErrs, validateHostNetwork(securityContext.HostNetwork, spec.Containers, specPath.Child("containers"))...) + allErrs = append(allErrs, validateHostNetworkNoHostAliases(securityContext.HostNetwork, spec.HostAliases, specPath)...) if securityContext.FSGroup != nil { for _, msg := range validation.IsValidGroupId(*securityContext.FSGroup) { allErrs = append(allErrs, field.Invalid(fldPath.Child("fsGroup"), *(securityContext.FSGroup), msg)) diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index 375ef36fda6..8a732dbe3ea 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -3262,6 +3262,12 @@ func TestValidatePodSpec(t *testing.T) { RestartPolicy: api.RestartPolicyAlways, DNSPolicy: api.DNSClusterFirst, }, + "with hostNetwork and hostAliases": { + SecurityContext: &api.PodSecurityContext{ + HostNetwork: true, + }, + HostAliases: []api.HostAlias{{IP: "12.34.56.78", Hostnames: []string{"host1", "host2"}}}, + }, "bad supplementalGroups large than math.MaxInt32": { Containers: []api.Container{{Name: "ctr", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: "File"}}, SecurityContext: &api.PodSecurityContext{