Ensure headless service port equal to targetPort
This commit is contained in:
@@ -1239,9 +1239,11 @@ func ValidateService(service *api.Service) validation.ErrorList {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isHeadlessService := service.Spec.ClusterIP == api.ClusterIPNone
|
||||
allPortNames := sets.String{}
|
||||
for i := range service.Spec.Ports {
|
||||
allErrs = append(allErrs, validateServicePort(&service.Spec.Ports[i], len(service.Spec.Ports) > 1, &allPortNames).PrefixIndex(i).Prefix("spec.ports")...)
|
||||
allErrs = append(allErrs, validateServicePort(&service.Spec.Ports[i], len(service.Spec.Ports) > 1, isHeadlessService, &allPortNames).PrefixIndex(i).Prefix("spec.ports")...)
|
||||
}
|
||||
|
||||
if service.Spec.Selector != nil {
|
||||
@@ -1309,7 +1311,7 @@ func ValidateService(service *api.Service) validation.ErrorList {
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func validateServicePort(sp *api.ServicePort, requireName bool, allNames *sets.String) validation.ErrorList {
|
||||
func validateServicePort(sp *api.ServicePort, requireName, isHeadlessService bool, allNames *sets.String) validation.ErrorList {
|
||||
allErrs := validation.ErrorList{}
|
||||
|
||||
if requireName && sp.Name == "" {
|
||||
@@ -1341,6 +1343,12 @@ func validateServicePort(sp *api.ServicePort, requireName bool, allNames *sets.S
|
||||
allErrs = append(allErrs, validation.NewInvalidError("targetPort", sp.TargetPort, PortNameErrorMsg))
|
||||
}
|
||||
|
||||
if isHeadlessService {
|
||||
if sp.TargetPort.Type == intstr.String || (sp.TargetPort.Type == intstr.Int && sp.Port != sp.TargetPort.IntValue()) {
|
||||
allErrs = append(allErrs, validation.NewInvalidError("port", sp.Port, "must be equal to targetPort when clusterIP = None"))
|
||||
}
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user