From d5a6a5439189fc5aee9445145836fe5d46c397b3 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 20 Nov 2014 13:55:45 +0800 Subject: [PATCH] clean up error message on labels validation errs --- pkg/api/validation/validation.go | 18 +++++++++--------- pkg/api/validation/validation_test.go | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 8a1b65d259a..00512de07d6 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -363,7 +363,7 @@ func ValidatePod(pod *api.Pod) errs.ValidationErrorList { allErrs = append(allErrs, errs.NewFieldInvalid("namespace", pod.Namespace, "")) } allErrs = append(allErrs, ValidatePodSpec(&pod.Spec).Prefix("spec")...) - allErrs = append(allErrs, validateLabels(pod.Labels)...) + allErrs = append(allErrs, validateLabels(pod.Labels, "labels")...) return allErrs } @@ -378,15 +378,15 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList { allErrs = append(allErrs, vErrs.Prefix("volumes")...) allErrs = append(allErrs, validateContainers(spec.Containers, allVolumes).Prefix("containers")...) allErrs = append(allErrs, validateRestartPolicy(&spec.RestartPolicy).Prefix("restartPolicy")...) - allErrs = append(allErrs, validateLabels(spec.NodeSelector).Prefix("nodeSelector")...) + allErrs = append(allErrs, validateLabels(spec.NodeSelector, "nodeSelector")...) return allErrs } -func validateLabels(labels map[string]string) errs.ValidationErrorList { +func validateLabels(labels map[string]string, field string) errs.ValidationErrorList { allErrs := errs.ValidationErrorList{} for k := range labels { if !util.IsDNS952Label(k) { - allErrs = append(allErrs, errs.NewFieldNotSupported("label", k)) + allErrs = append(allErrs, errs.NewFieldNotSupported(field, k)) } } return allErrs @@ -456,8 +456,8 @@ func ValidateService(service *api.Service, lister ServiceLister, ctx api.Context } } } - allErrs = append(allErrs, validateLabels(service.Labels)...) - allErrs = append(allErrs, validateLabels(service.Spec.Selector)...) + allErrs = append(allErrs, validateLabels(service.Labels, "labels")...) + allErrs = append(allErrs, validateLabels(service.Spec.Selector, "selector")...) return allErrs } @@ -471,7 +471,7 @@ func ValidateReplicationController(controller *api.ReplicationController) errs.V allErrs = append(allErrs, errs.NewFieldInvalid("namespace", controller.Namespace, "")) } allErrs = append(allErrs, ValidateReplicationControllerSpec(&controller.Spec).Prefix("spec")...) - allErrs = append(allErrs, validateLabels(controller.Labels)...) + allErrs = append(allErrs, validateLabels(controller.Labels, "labels")...) return allErrs } @@ -494,7 +494,6 @@ func ValidateReplicationControllerSpec(spec *api.ReplicationControllerSpec) errs if !selector.Matches(labels) { allErrs = append(allErrs, errs.NewFieldInvalid("template.labels", spec.Template.Labels, "selector does not match template")) } - allErrs = append(allErrs, validateLabels(spec.Template.Labels).Prefix("template.labels")...) allErrs = append(allErrs, ValidatePodTemplateSpec(spec.Template).Prefix("template")...) // RestartPolicy has already been first-order validated as per ValidatePodTemplateSpec(). if spec.Template.Spec.RestartPolicy.Always == nil { @@ -509,6 +508,7 @@ func ValidateReplicationControllerSpec(spec *api.ReplicationControllerSpec) errs // ValidatePodTemplateSpec validates the spec of a pod template func ValidatePodTemplateSpec(spec *api.PodTemplateSpec) errs.ValidationErrorList { allErrs := errs.ValidationErrorList{} + allErrs = append(allErrs, validateLabels(spec.Labels, "labels")...) allErrs = append(allErrs, ValidatePodSpec(&spec.Spec).Prefix("spec")...) allErrs = append(allErrs, ValidateReadOnlyPersistentDisks(spec.Spec.Volumes).Prefix("spec.volumes")...) return allErrs @@ -557,7 +557,7 @@ func ValidateMinion(minion *api.Minion) errs.ValidationErrorList { if len(minion.Name) == 0 { allErrs = append(allErrs, errs.NewFieldRequired("name", minion.Name)) } - allErrs = append(allErrs, validateLabels(minion.Labels)...) + allErrs = append(allErrs, validateLabels(minion.Labels, "labels")...) return allErrs } diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index b2a3ee107d3..05a500d917f 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -1003,8 +1003,8 @@ func TestValidateReplicationController(t *testing.T) { field != "spec.template" && field != "GCEPersistentDisk.ReadOnly" && field != "spec.replicas" && - field != "spec.template.label" && - field != "label" { + field != "spec.template.labels" && + field != "labels" { t.Errorf("%s: missing prefix for: %v", k, errs[i]) } } @@ -1080,7 +1080,7 @@ func TestValidateMinion(t *testing.T) { for i := range errs { field := errs[i].(*errors.ValidationError).Field if field != "name" && - field != "label" { + field != "labels" { t.Errorf("%s: missing prefix for: %v", k, errs[i]) } }