mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
Merge pull request #996 from brendandburns/selector
Add a validation that replicaSelector matches PodTemplate.Labels
This commit is contained in:
commit
bd2cbdc312
@ -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))
|
||||||
}
|
}
|
||||||
|
@ -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{
|
||||||
|
@ -205,6 +205,7 @@ var validPodTemplate = api.PodTemplate{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Labels: map[string]string{"a": "b"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateController(t *testing.T) {
|
func TestCreateController(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user