mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
Expose ValidateLabels in validation.go for reuse by other components
Label validation is common to anyone building kube resources.
This commit is contained in:
parent
fbd4722094
commit
48146e01cd
@ -378,7 +378,7 @@ func ValidatePod(pod *api.Pod) errs.ValidationErrorList {
|
|||||||
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", pod.Namespace, ""))
|
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", pod.Namespace, ""))
|
||||||
}
|
}
|
||||||
allErrs = append(allErrs, ValidatePodSpec(&pod.Spec).Prefix("spec")...)
|
allErrs = append(allErrs, ValidatePodSpec(&pod.Spec).Prefix("spec")...)
|
||||||
allErrs = append(allErrs, validateLabels(pod.Labels, "labels")...)
|
allErrs = append(allErrs, ValidateLabels(pod.Labels, "labels")...)
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -394,11 +394,12 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList {
|
|||||||
allErrs = append(allErrs, validateContainers(spec.Containers, allVolumes).Prefix("containers")...)
|
allErrs = append(allErrs, validateContainers(spec.Containers, allVolumes).Prefix("containers")...)
|
||||||
allErrs = append(allErrs, validateRestartPolicy(&spec.RestartPolicy).Prefix("restartPolicy")...)
|
allErrs = append(allErrs, validateRestartPolicy(&spec.RestartPolicy).Prefix("restartPolicy")...)
|
||||||
allErrs = append(allErrs, validateDNSPolicy(&spec.DNSPolicy).Prefix("dnsPolicy")...)
|
allErrs = append(allErrs, validateDNSPolicy(&spec.DNSPolicy).Prefix("dnsPolicy")...)
|
||||||
allErrs = append(allErrs, validateLabels(spec.NodeSelector, "nodeSelector")...)
|
allErrs = append(allErrs, ValidateLabels(spec.NodeSelector, "nodeSelector")...)
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateLabels(labels map[string]string, field string) errs.ValidationErrorList {
|
// ValidateLabels validates that a set of labels are correctly defined.
|
||||||
|
func ValidateLabels(labels map[string]string, field string) errs.ValidationErrorList {
|
||||||
allErrs := errs.ValidationErrorList{}
|
allErrs := errs.ValidationErrorList{}
|
||||||
for k := range labels {
|
for k := range labels {
|
||||||
if !util.IsQualifiedName(k) {
|
if !util.IsQualifiedName(k) {
|
||||||
@ -458,9 +459,9 @@ func ValidateService(service *api.Service, lister ServiceLister, ctx api.Context
|
|||||||
}
|
}
|
||||||
|
|
||||||
if service.Spec.Selector != nil {
|
if service.Spec.Selector != nil {
|
||||||
allErrs = append(allErrs, validateLabels(service.Spec.Selector, "spec.selector")...)
|
allErrs = append(allErrs, ValidateLabels(service.Spec.Selector, "spec.selector")...)
|
||||||
}
|
}
|
||||||
allErrs = append(allErrs, validateLabels(service.Labels, "labels")...)
|
allErrs = append(allErrs, ValidateLabels(service.Labels, "labels")...)
|
||||||
|
|
||||||
if service.Spec.CreateExternalLoadBalancer {
|
if service.Spec.CreateExternalLoadBalancer {
|
||||||
services, err := lister.ListServices(ctx)
|
services, err := lister.ListServices(ctx)
|
||||||
@ -496,7 +497,7 @@ func ValidateReplicationController(controller *api.ReplicationController) errs.V
|
|||||||
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", controller.Namespace, ""))
|
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", controller.Namespace, ""))
|
||||||
}
|
}
|
||||||
allErrs = append(allErrs, ValidateReplicationControllerSpec(&controller.Spec).Prefix("spec")...)
|
allErrs = append(allErrs, ValidateReplicationControllerSpec(&controller.Spec).Prefix("spec")...)
|
||||||
allErrs = append(allErrs, validateLabels(controller.Labels, "labels")...)
|
allErrs = append(allErrs, ValidateLabels(controller.Labels, "labels")...)
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -533,7 +534,7 @@ func ValidateReplicationControllerSpec(spec *api.ReplicationControllerSpec) errs
|
|||||||
// ValidatePodTemplateSpec validates the spec of a pod template
|
// ValidatePodTemplateSpec validates the spec of a pod template
|
||||||
func ValidatePodTemplateSpec(spec *api.PodTemplateSpec) errs.ValidationErrorList {
|
func ValidatePodTemplateSpec(spec *api.PodTemplateSpec) errs.ValidationErrorList {
|
||||||
allErrs := errs.ValidationErrorList{}
|
allErrs := errs.ValidationErrorList{}
|
||||||
allErrs = append(allErrs, validateLabels(spec.Labels, "labels")...)
|
allErrs = append(allErrs, ValidateLabels(spec.Labels, "labels")...)
|
||||||
allErrs = append(allErrs, ValidatePodSpec(&spec.Spec).Prefix("spec")...)
|
allErrs = append(allErrs, ValidatePodSpec(&spec.Spec).Prefix("spec")...)
|
||||||
allErrs = append(allErrs, ValidateReadOnlyPersistentDisks(spec.Spec.Volumes).Prefix("spec.volumes")...)
|
allErrs = append(allErrs, ValidateReadOnlyPersistentDisks(spec.Spec.Volumes).Prefix("spec.volumes")...)
|
||||||
return allErrs
|
return allErrs
|
||||||
@ -577,7 +578,7 @@ func ValidateMinion(minion *api.Node) errs.ValidationErrorList {
|
|||||||
if len(minion.Name) == 0 {
|
if len(minion.Name) == 0 {
|
||||||
allErrs = append(allErrs, errs.NewFieldRequired("name", minion.Name))
|
allErrs = append(allErrs, errs.NewFieldRequired("name", minion.Name))
|
||||||
}
|
}
|
||||||
allErrs = append(allErrs, validateLabels(minion.Labels, "labels")...)
|
allErrs = append(allErrs, ValidateLabels(minion.Labels, "labels")...)
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ func TestValidateLabels(t *testing.T) {
|
|||||||
{"1.2.3.4/5678": "bar"},
|
{"1.2.3.4/5678": "bar"},
|
||||||
}
|
}
|
||||||
for i := range successCases {
|
for i := range successCases {
|
||||||
errs := validateLabels(successCases[i], "field")
|
errs := ValidateLabels(successCases[i], "field")
|
||||||
if len(errs) != 0 {
|
if len(errs) != 0 {
|
||||||
t.Errorf("case[%d] expected success, got %#v", i, errs)
|
t.Errorf("case[%d] expected success, got %#v", i, errs)
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ func TestValidateLabels(t *testing.T) {
|
|||||||
{strings.Repeat("a", 254): "bar"},
|
{strings.Repeat("a", 254): "bar"},
|
||||||
}
|
}
|
||||||
for i := range errorCases {
|
for i := range errorCases {
|
||||||
errs := validateLabels(errorCases[i], "field")
|
errs := ValidateLabels(errorCases[i], "field")
|
||||||
if len(errs) != 1 {
|
if len(errs) != 1 {
|
||||||
t.Errorf("case[%d] expected failure", i)
|
t.Errorf("case[%d] expected failure", i)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user