Merge pull request #996 from brendandburns/selector

Add a validation that replicaSelector matches PodTemplate.Labels
This commit is contained in:
Clayton Coleman 2014-08-22 13:46:06 -04:00
commit bd2cbdc312
3 changed files with 14 additions and 0 deletions

View File

@ -275,6 +275,11 @@ func ValidateReplicationController(controller *ReplicationController) errs.Error
if labels.Set(controller.DesiredState.ReplicaSelector).AsSelector().Empty() { if labels.Set(controller.DesiredState.ReplicaSelector).AsSelector().Empty() {
allErrs = append(allErrs, errs.NewInvalid("ReplicationController.ReplicaSelector", controller.DesiredState.ReplicaSelector)) allErrs = append(allErrs, errs.NewInvalid("ReplicationController.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.NewInvalid("ReplicaController.DesiredState.PodTemplate.Labels", controller.DesiredState.PodTemplate))
}
if controller.DesiredState.Replicas < 0 { if controller.DesiredState.Replicas < 0 {
allErrs = append(allErrs, errs.NewInvalid("ReplicationController.Replicas", controller.DesiredState.Replicas)) allErrs = append(allErrs, errs.NewInvalid("ReplicationController.Replicas", controller.DesiredState.Replicas))
} }

View File

@ -373,6 +373,7 @@ func TestValidateReplicationController(t *testing.T) {
Version: "v1beta1", Version: "v1beta1",
}, },
}, },
Labels: validSelector,
} }
successCases := []ReplicationController{ successCases := []ReplicationController{
@ -411,6 +412,13 @@ func TestValidateReplicationController(t *testing.T) {
PodTemplate: validPodTemplate, PodTemplate: validPodTemplate,
}, },
}, },
"selector_doesnt_match": {
JSONBase: JSONBase{ID: "abc"},
DesiredState: ReplicationControllerState{
ReplicaSelector: map[string]string{"foo": "bar"},
PodTemplate: validPodTemplate,
},
},
"invalid manifest": { "invalid manifest": {
JSONBase: JSONBase{ID: "abc"}, JSONBase: JSONBase{ID: "abc"},
DesiredState: ReplicationControllerState{ DesiredState: ReplicationControllerState{

View File

@ -205,6 +205,7 @@ var validPodTemplate = api.PodTemplate{
}, },
}, },
}, },
Labels: map[string]string{"a": "b"},
} }
func TestCreateController(t *testing.T) { func TestCreateController(t *testing.T) {