Move Deployments to ReplicaSets and switch the Deployment selector to the new LabelSelector.
Update the Deployments' API types, defaulting code, conversions, helpers and validation to use ReplicaSets instead of ReplicationControllers and LabelSelector instead of map[string]string for selectors. Also update the Deployment controller, registry, kubectl subcommands, client listers package and e2e tests to use ReplicaSets and LabelSelector for Deployments.
This commit is contained in:
@@ -82,13 +82,13 @@ func TestGetReadyPodsCount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// generatePodFromRC creates a pod, with the input rc's selector and its template
|
||||
func generatePodFromRC(rc api.ReplicationController) api.Pod {
|
||||
// generatePodFromRS creates a pod, with the input ReplicaSet's selector and its template
|
||||
func generatePodFromRS(rs extensions.ReplicaSet) api.Pod {
|
||||
return api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: rc.Spec.Selector,
|
||||
Labels: rs.Labels,
|
||||
},
|
||||
Spec: rc.Spec.Template.Spec,
|
||||
Spec: rs.Spec.Template.Spec,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,15 +110,15 @@ func generatePod(labels map[string]string, image string) api.Pod {
|
||||
}
|
||||
}
|
||||
|
||||
func generateRCWithLabel(labels map[string]string, image string) api.ReplicationController {
|
||||
return api.ReplicationController{
|
||||
func generateRSWithLabel(labels map[string]string, image string) extensions.ReplicaSet {
|
||||
return extensions.ReplicaSet{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: api.SimpleNameGenerator.GenerateName("rc"),
|
||||
Name: api.SimpleNameGenerator.GenerateName("replicaset"),
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: api.ReplicationControllerSpec{
|
||||
Spec: extensions.ReplicaSetSpec{
|
||||
Replicas: 1,
|
||||
Selector: labels,
|
||||
Selector: &extensions.LabelSelector{MatchLabels: labels},
|
||||
Template: &api.PodTemplateSpec{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
@@ -135,17 +135,17 @@ func generateRCWithLabel(labels map[string]string, image string) api.Replication
|
||||
}
|
||||
}
|
||||
|
||||
// generateRC creates a replication controller, with the input deployment's template as its template
|
||||
func generateRC(deployment extensions.Deployment) api.ReplicationController {
|
||||
template := GetNewRCTemplate(deployment)
|
||||
return api.ReplicationController{
|
||||
// generateRS creates a replica set, with the input deployment's template as its template
|
||||
func generateRS(deployment extensions.Deployment) extensions.ReplicaSet {
|
||||
template := GetNewReplicaSetTemplate(deployment)
|
||||
return extensions.ReplicaSet{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: api.SimpleNameGenerator.GenerateName("rc"),
|
||||
Name: api.SimpleNameGenerator.GenerateName("replicaset"),
|
||||
Labels: template.Labels,
|
||||
},
|
||||
Spec: api.ReplicationControllerSpec{
|
||||
Spec: extensions.ReplicaSetSpec{
|
||||
Template: &template,
|
||||
Selector: template.Labels,
|
||||
Selector: &extensions.LabelSelector{MatchLabels: template.Labels},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func generateDeployment(image string) extensions.Deployment {
|
||||
},
|
||||
Spec: extensions.DeploymentSpec{
|
||||
Replicas: 1,
|
||||
Selector: podLabels,
|
||||
Selector: &extensions.LabelSelector{MatchLabels: podLabels},
|
||||
Template: api.PodTemplateSpec{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: podLabels,
|
||||
@@ -186,32 +186,32 @@ func generateDeployment(image string) extensions.Deployment {
|
||||
|
||||
func TestGetNewRC(t *testing.T) {
|
||||
newDeployment := generateDeployment("nginx")
|
||||
newRC := generateRC(newDeployment)
|
||||
newRC := generateRS(newDeployment)
|
||||
|
||||
tests := []struct {
|
||||
test string
|
||||
rcList api.ReplicationControllerList
|
||||
expected *api.ReplicationController
|
||||
rsList extensions.ReplicaSetList
|
||||
expected *extensions.ReplicaSet
|
||||
}{
|
||||
{
|
||||
"No new RC",
|
||||
api.ReplicationControllerList{
|
||||
Items: []api.ReplicationController{
|
||||
generateRC(generateDeployment("foo")),
|
||||
generateRC(generateDeployment("bar")),
|
||||
"No new ReplicaSet",
|
||||
extensions.ReplicaSetList{
|
||||
Items: []extensions.ReplicaSet{
|
||||
generateRS(generateDeployment("foo")),
|
||||
generateRS(generateDeployment("bar")),
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"Has new RC",
|
||||
api.ReplicationControllerList{
|
||||
Items: []api.ReplicationController{
|
||||
generateRC(generateDeployment("foo")),
|
||||
generateRC(generateDeployment("bar")),
|
||||
generateRC(generateDeployment("abc")),
|
||||
"Has new ReplicaSet",
|
||||
extensions.ReplicaSetList{
|
||||
Items: []extensions.ReplicaSet{
|
||||
generateRS(generateDeployment("foo")),
|
||||
generateRS(generateDeployment("bar")),
|
||||
generateRS(generateDeployment("abc")),
|
||||
newRC,
|
||||
generateRC(generateDeployment("xyz")),
|
||||
generateRS(generateDeployment("xyz")),
|
||||
},
|
||||
},
|
||||
&newRC,
|
||||
@@ -223,69 +223,69 @@ func TestGetNewRC(t *testing.T) {
|
||||
c := &simple.Client{
|
||||
Request: simple.Request{
|
||||
Method: "GET",
|
||||
Path: testapi.Default.ResourcePath("replicationControllers", ns, ""),
|
||||
Path: testapi.Default.ResourcePath("replicaSets", ns, ""),
|
||||
},
|
||||
Response: simple.Response{
|
||||
StatusCode: 200,
|
||||
Body: &test.rcList,
|
||||
Body: &test.rsList,
|
||||
},
|
||||
}
|
||||
rc, err := GetNewRC(newDeployment, c.Setup(t).Clientset)
|
||||
rs, err := GetNewReplicaSet(newDeployment, c.Setup(t).Clientset)
|
||||
if err != nil {
|
||||
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
|
||||
}
|
||||
if !api.Semantic.DeepEqual(rc, test.expected) {
|
||||
t.Errorf("In test case %s, expected %+v, got %+v", test.test, test.expected, rc)
|
||||
if !api.Semantic.DeepEqual(rs, test.expected) {
|
||||
t.Errorf("In test case %s, expected %+v, got %+v", test.test, test.expected, rs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOldRCs(t *testing.T) {
|
||||
newDeployment := generateDeployment("nginx")
|
||||
newRC := generateRC(newDeployment)
|
||||
newPod := generatePodFromRC(newRC)
|
||||
newRS := generateRS(newDeployment)
|
||||
newPod := generatePodFromRS(newRS)
|
||||
|
||||
// create 2 old deployments and related rcs/pods, with the same labels but different template
|
||||
// create 2 old deployments and related replica sets/pods, with the same labels but different template
|
||||
oldDeployment := generateDeployment("nginx")
|
||||
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
|
||||
oldRC := generateRC(oldDeployment)
|
||||
oldPod := generatePodFromRC(oldRC)
|
||||
oldRS := generateRS(oldDeployment)
|
||||
oldPod := generatePodFromRS(oldRS)
|
||||
oldDeployment2 := generateDeployment("nginx")
|
||||
oldDeployment2.Spec.Template.Spec.Containers[0].Name = "nginx-old-2"
|
||||
oldRC2 := generateRC(oldDeployment2)
|
||||
oldPod2 := generatePodFromRC(oldRC2)
|
||||
oldRS2 := generateRS(oldDeployment2)
|
||||
oldPod2 := generatePodFromRS(oldRS2)
|
||||
|
||||
// create 1 rc that existed before the deployment, with the same labels as the deployment
|
||||
existedPod := generatePod(newDeployment.Spec.Selector, "foo")
|
||||
existedRC := generateRCWithLabel(newDeployment.Spec.Selector, "foo")
|
||||
// create 1 ReplicaSet that existed before the deployment, with the same labels as the deployment
|
||||
existedPod := generatePod(newDeployment.Spec.Template.Labels, "foo")
|
||||
existedRS := generateRSWithLabel(newDeployment.Spec.Template.Labels, "foo")
|
||||
|
||||
tests := []struct {
|
||||
test string
|
||||
objs []runtime.Object
|
||||
expected []*api.ReplicationController
|
||||
expected []*extensions.ReplicaSet
|
||||
}{
|
||||
{
|
||||
"No old RCs",
|
||||
"No old ReplicaSets",
|
||||
[]runtime.Object{
|
||||
&api.PodList{
|
||||
Items: []api.Pod{
|
||||
generatePod(newDeployment.Spec.Selector, "foo"),
|
||||
generatePod(newDeployment.Spec.Selector, "bar"),
|
||||
generatePod(newDeployment.Spec.Template.Labels, "foo"),
|
||||
generatePod(newDeployment.Spec.Template.Labels, "bar"),
|
||||
newPod,
|
||||
},
|
||||
},
|
||||
&api.ReplicationControllerList{
|
||||
Items: []api.ReplicationController{
|
||||
generateRC(generateDeployment("foo")),
|
||||
newRC,
|
||||
generateRC(generateDeployment("bar")),
|
||||
&extensions.ReplicaSetList{
|
||||
Items: []extensions.ReplicaSet{
|
||||
generateRS(generateDeployment("foo")),
|
||||
newRS,
|
||||
generateRS(generateDeployment("bar")),
|
||||
},
|
||||
},
|
||||
},
|
||||
[]*api.ReplicationController{},
|
||||
[]*extensions.ReplicaSet{},
|
||||
},
|
||||
{
|
||||
"Has old RC",
|
||||
"Has old ReplicaSet",
|
||||
[]runtime.Object{
|
||||
&api.PodList{
|
||||
Items: []api.Pod{
|
||||
@@ -294,51 +294,51 @@ func TestGetOldRCs(t *testing.T) {
|
||||
generatePod(map[string]string{"name": "bar"}, "bar"),
|
||||
generatePod(map[string]string{"name": "xyz"}, "xyz"),
|
||||
existedPod,
|
||||
generatePod(newDeployment.Spec.Selector, "abc"),
|
||||
generatePod(newDeployment.Spec.Template.Labels, "abc"),
|
||||
},
|
||||
},
|
||||
&api.ReplicationControllerList{
|
||||
Items: []api.ReplicationController{
|
||||
oldRC2,
|
||||
oldRC,
|
||||
existedRC,
|
||||
newRC,
|
||||
generateRCWithLabel(map[string]string{"name": "xyz"}, "xyz"),
|
||||
generateRCWithLabel(map[string]string{"name": "bar"}, "bar"),
|
||||
&extensions.ReplicaSetList{
|
||||
Items: []extensions.ReplicaSet{
|
||||
oldRS2,
|
||||
oldRS,
|
||||
existedRS,
|
||||
newRS,
|
||||
generateRSWithLabel(map[string]string{"name": "xyz"}, "xyz"),
|
||||
generateRSWithLabel(map[string]string{"name": "bar"}, "bar"),
|
||||
},
|
||||
},
|
||||
},
|
||||
[]*api.ReplicationController{&oldRC, &oldRC2, &existedRC},
|
||||
[]*extensions.ReplicaSet{&oldRS, &oldRS2, &existedRS},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
rcs, _, err := GetOldRCs(newDeployment, fake.NewSimpleClientset(test.objs...))
|
||||
rss, _, err := GetOldReplicaSets(newDeployment, fake.NewSimpleClientset(test.objs...))
|
||||
if err != nil {
|
||||
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
|
||||
}
|
||||
if !equal(rcs, test.expected) {
|
||||
t.Errorf("In test case %q, expected %v, got %v", test.test, test.expected, rcs)
|
||||
if !equal(rss, test.expected) {
|
||||
t.Errorf("In test case %q, expected %v, got %v", test.test, test.expected, rss)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// equal compares the equality of two rc slices regardless of their ordering
|
||||
func equal(rcs1, rcs2 []*api.ReplicationController) bool {
|
||||
if reflect.DeepEqual(rcs1, rcs2) {
|
||||
// equal compares the equality of two ReplicaSet slices regardless of their ordering
|
||||
func equal(rss1, rss2 []*extensions.ReplicaSet) bool {
|
||||
if reflect.DeepEqual(rss1, rss2) {
|
||||
return true
|
||||
}
|
||||
if rcs1 == nil || rcs2 == nil || len(rcs1) != len(rcs2) {
|
||||
if rss1 == nil || rss2 == nil || len(rss1) != len(rss2) {
|
||||
return false
|
||||
}
|
||||
count := 0
|
||||
for _, rc1 := range rcs1 {
|
||||
for _, rc2 := range rcs2 {
|
||||
if reflect.DeepEqual(rc1, rc2) {
|
||||
for _, rs1 := range rss1 {
|
||||
for _, rs2 := range rss2 {
|
||||
if reflect.DeepEqual(rs1, rs2) {
|
||||
count++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return count == len(rcs1)
|
||||
return count == len(rss1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user