From f94198c7a3e2ea26e2c1293e61ac35ae5f6d281a Mon Sep 17 00:00:00 2001 From: Paul Morie Date: Tue, 16 Sep 2014 12:54:38 -0400 Subject: [PATCH] Expose validation method for ReplicationControllerState --- pkg/api/validation/validation.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 518178c117c..24307032faf 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -341,17 +341,24 @@ func ValidateReplicationController(controller *api.ReplicationController) errs.E if len(controller.ID) == 0 { allErrs = append(allErrs, errs.NewFieldRequired("id", controller.ID)) } - if labels.Set(controller.DesiredState.ReplicaSelector).AsSelector().Empty() { - allErrs = append(allErrs, errs.NewFieldRequired("desiredState.replicaSelector", controller.DesiredState.ReplicaSelector)) - } - selector := labels.Set(controller.DesiredState.ReplicaSelector).AsSelector() - labels := labels.Set(controller.DesiredState.PodTemplate.Labels) - if !selector.Matches(labels) { - allErrs = append(allErrs, errs.NewFieldInvalid("desiredState.podTemplate.labels", controller.DesiredState.PodTemplate)) - } - if controller.DesiredState.Replicas < 0 { - allErrs = append(allErrs, errs.NewFieldInvalid("desiredState.replicas", controller.DesiredState.Replicas)) - } - allErrs = append(allErrs, ValidateManifest(&controller.DesiredState.PodTemplate.DesiredState.Manifest).Prefix("desiredState.podTemplate.desiredState.manifest")...) + allErrs = append(allErrs, ValidateReplicationControllerState(&controller.DesiredState).Prefix("desiredState")...) + return allErrs +} + +// ValidateReplicationControllerState tests if required fields in the replication controller state are set. +func ValidateReplicationControllerState(state *api.ReplicationControllerState) errs.ErrorList { + allErrs := errs.ErrorList{} + if labels.Set(state.ReplicaSelector).AsSelector().Empty() { + allErrs = append(allErrs, errs.NewFieldRequired("replicaSelector", state.ReplicaSelector)) + } + selector := labels.Set(state.ReplicaSelector).AsSelector() + labels := labels.Set(state.PodTemplate.Labels) + if !selector.Matches(labels) { + allErrs = append(allErrs, errs.NewFieldInvalid("podTemplate.labels", state.PodTemplate)) + } + if state.Replicas < 0 { + allErrs = append(allErrs, errs.NewFieldInvalid("replicas", state.Replicas)) + } + allErrs = append(allErrs, ValidateManifest(&state.PodTemplate.DesiredState.Manifest).Prefix("podTemplate.desiredState.manifest")...) return allErrs }