diff --git a/pkg/api/validation.go b/pkg/api/validation.go index cd54e00f7d1..b1847c99b78 100644 --- a/pkg/api/validation.go +++ b/pkg/api/validation.go @@ -275,6 +275,11 @@ func ValidateReplicationController(controller *ReplicationController) errs.Error if labels.Set(controller.DesiredState.ReplicaSelector).AsSelector().Empty() { 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 { allErrs = append(allErrs, errs.NewInvalid("ReplicationController.Replicas", controller.DesiredState.Replicas)) } diff --git a/pkg/api/validation_test.go b/pkg/api/validation_test.go index 093adc14bbd..2d13d246862 100644 --- a/pkg/api/validation_test.go +++ b/pkg/api/validation_test.go @@ -373,6 +373,7 @@ func TestValidateReplicationController(t *testing.T) { Version: "v1beta1", }, }, + Labels: validSelector, } successCases := []ReplicationController{ @@ -411,6 +412,13 @@ func TestValidateReplicationController(t *testing.T) { PodTemplate: validPodTemplate, }, }, + "selector_doesnt_match": { + JSONBase: JSONBase{ID: "abc"}, + DesiredState: ReplicationControllerState{ + ReplicaSelector: map[string]string{"foo": "bar"}, + PodTemplate: validPodTemplate, + }, + }, "invalid manifest": { JSONBase: JSONBase{ID: "abc"}, DesiredState: ReplicationControllerState{ diff --git a/pkg/registry/controller/storage_test.go b/pkg/registry/controller/storage_test.go index cc060177744..92c2b9eb905 100644 --- a/pkg/registry/controller/storage_test.go +++ b/pkg/registry/controller/storage_test.go @@ -205,6 +205,7 @@ var validPodTemplate = api.PodTemplate{ }, }, }, + Labels: map[string]string{"a": "b"}, } func TestCreateController(t *testing.T) {