mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Merge pull request #7786 from thockin/ports
Service port names are required for multi-port
This commit is contained in:
		@@ -980,7 +980,7 @@ func ValidateService(service *api.Service) errs.ValidationErrorList {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	allPortNames := util.StringSet{}
 | 
						allPortNames := util.StringSet{}
 | 
				
			||||||
	for i := range service.Spec.Ports {
 | 
						for i := range service.Spec.Ports {
 | 
				
			||||||
		allErrs = append(allErrs, validateServicePort(&service.Spec.Ports[i], i, &allPortNames).PrefixIndex(i).Prefix("spec.ports")...)
 | 
							allErrs = append(allErrs, validateServicePort(&service.Spec.Ports[i], len(service.Spec.Ports) > 1, &allPortNames).PrefixIndex(i).Prefix("spec.ports")...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if service.Spec.Selector != nil {
 | 
						if service.Spec.Selector != nil {
 | 
				
			||||||
@@ -1018,19 +1018,18 @@ func ValidateService(service *api.Service) errs.ValidationErrorList {
 | 
				
			|||||||
	return allErrs
 | 
						return allErrs
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func validateServicePort(sp *api.ServicePort, index int, allNames *util.StringSet) errs.ValidationErrorList {
 | 
					func validateServicePort(sp *api.ServicePort, requireName bool, allNames *util.StringSet) errs.ValidationErrorList {
 | 
				
			||||||
	allErrs := errs.ValidationErrorList{}
 | 
						allErrs := errs.ValidationErrorList{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(sp.Name) == 0 {
 | 
						if requireName && sp.Name == "" {
 | 
				
			||||||
		// Allow empty names if they are the first port (mostly for compat).
 | 
					 | 
				
			||||||
		if index != 0 {
 | 
					 | 
				
			||||||
		allErrs = append(allErrs, errs.NewFieldRequired("name"))
 | 
							allErrs = append(allErrs, errs.NewFieldRequired("name"))
 | 
				
			||||||
		}
 | 
						} else if sp.Name != "" {
 | 
				
			||||||
	} else if !util.IsDNS1123Label(sp.Name) {
 | 
							if !util.IsDNS1123Label(sp.Name) {
 | 
				
			||||||
			allErrs = append(allErrs, errs.NewFieldInvalid("name", sp.Name, dns1123LabelErrorMsg))
 | 
								allErrs = append(allErrs, errs.NewFieldInvalid("name", sp.Name, dns1123LabelErrorMsg))
 | 
				
			||||||
		} else if allNames.Has(sp.Name) {
 | 
							} else if allNames.Has(sp.Name) {
 | 
				
			||||||
			allErrs = append(allErrs, errs.NewFieldDuplicate("name", sp.Name))
 | 
								allErrs = append(allErrs, errs.NewFieldDuplicate("name", sp.Name))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !util.IsValidPortNum(sp.Port) {
 | 
						if !util.IsValidPortNum(sp.Port) {
 | 
				
			||||||
		allErrs = append(allErrs, errs.NewFieldInvalid("port", sp.Port, portRangeErrorMsg))
 | 
							allErrs = append(allErrs, errs.NewFieldInvalid("port", sp.Port, portRangeErrorMsg))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1600,6 +1600,14 @@ func TestValidateService(t *testing.T) {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
			numErrs: 1,
 | 
								numErrs: 1,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "empty multi-port port[0] name",
 | 
				
			||||||
 | 
								tweakSvc: func(s *api.Service) {
 | 
				
			||||||
 | 
									s.Spec.Ports[0].Name = ""
 | 
				
			||||||
 | 
									s.Spec.Ports = append(s.Spec.Ports, api.ServicePort{Name: "p", Protocol: "TCP", Port: 12345})
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								numErrs: 1,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name: "invalid port name",
 | 
								name: "invalid port name",
 | 
				
			||||||
			tweakSvc: func(s *api.Service) {
 | 
								tweakSvc: func(s *api.Service) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user