Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deployment helper - no assumptions on only one new ReplicaSet #41851

Merged
merged 2 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/controller/deployment/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ go_test(
"//vendor:k8s.io/apimachinery/pkg/api/equality",
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
"//vendor:k8s.io/apimachinery/pkg/runtime",
"//vendor:k8s.io/apimachinery/pkg/types",
"//vendor:k8s.io/apimachinery/pkg/util/intstr",
"//vendor:k8s.io/client-go/testing",
],
Expand Down
19 changes: 13 additions & 6 deletions pkg/controller/deployment/util/deployment_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,13 @@ func EqualIgnoreHash(template1, template2 v1.PodTemplateSpec) bool {
// FindNewReplicaSet returns the new RS this given deployment targets (the one with the same pod template).
func FindNewReplicaSet(deployment *extensions.Deployment, rsList []*extensions.ReplicaSet) (*extensions.ReplicaSet, error) {
newRSTemplate := GetNewReplicaSetTemplate(deployment)
sort.Sort(controller.ReplicaSetsByCreationTimestamp(rsList))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smarterclayton @mfojtik we are going to choose the oldest replica set out of two identical replica sets for now - seems like the sanest thing to do when we end up in such a state (seems possible after cluster upgrades: #40415).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janetkuo can you add a comment explaining this here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for i := range rsList {
if EqualIgnoreHash(rsList[i].Spec.Template, newRSTemplate) {
// This is the new ReplicaSet.
// In rare cases, such as after cluster upgrades, Deployment may end up with
// having more than one new ReplicaSets that have the same template as its template,
// see https://github.com/kubernetes/kubernetes/issues/40415
// We deterministically choose the oldest new ReplicaSet.
return rsList[i], nil
}
}
Expand All @@ -629,16 +633,21 @@ func FindOldReplicaSets(deployment *extensions.Deployment, rsList []*extensions.
// All pods and replica sets are labeled with pod-template-hash to prevent overlapping
oldRSs := map[string]*extensions.ReplicaSet{}
allOldRSs := map[string]*extensions.ReplicaSet{}
newRSTemplate := GetNewReplicaSetTemplate(deployment)
requiredRSs := []*extensions.ReplicaSet{}
allRSs := []*extensions.ReplicaSet{}
newRS, err := FindNewReplicaSet(deployment, rsList)
if err != nil {
return requiredRSs, allRSs, err
}
for _, pod := range podList.Items {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have PR that reworks FindOldReplicaSets - the podList is not needed at all. We should simply look at rs.spec.replicas for filtering RSs with and without pods.

podLabelsSelector := labels.Set(pod.ObjectMeta.Labels)
for _, rs := range rsList {
rsLabelsSelector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
if err != nil {
return nil, nil, fmt.Errorf("invalid label selector: %v", err)
}
// Filter out replica set that has the same pod template spec as the deployment - that is the new replica set.
if EqualIgnoreHash(rs.Spec.Template, newRSTemplate) {
// Filter out new replica set
if newRS != nil && rs.UID == newRS.UID {
continue
}
allOldRSs[rs.ObjectMeta.Name] = rs
Expand All @@ -647,12 +656,10 @@ func FindOldReplicaSets(deployment *extensions.Deployment, rsList []*extensions.
}
}
}
requiredRSs := []*extensions.ReplicaSet{}
for key := range oldRSs {
value := oldRSs[key]
requiredRSs = append(requiredRSs, value)
}
allRSs := []*extensions.ReplicaSet{}
for key := range allOldRSs {
value := allOldRSs[key]
allRSs = append(allRSs, value)
Expand Down
51 changes: 48 additions & 3 deletions pkg/controller/deployment/util/deployment_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package util

import (
"fmt"
"math/rand"
"reflect"
"strconv"
"testing"
"time"

Expand All @@ -27,6 +29,7 @@ import (
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
core "k8s.io/client-go/testing"
"k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -161,6 +164,7 @@ func generateRS(deployment extensions.Deployment) extensions.ReplicaSet {
template := GetNewReplicaSetTemplate(&deployment)
return extensions.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
UID: randomUID(),
Name: v1.SimpleNameGenerator.GenerateName("replicaset"),
Labels: template.Labels,
},
Expand All @@ -172,6 +176,10 @@ func generateRS(deployment extensions.Deployment) extensions.ReplicaSet {
}
}

func randomUID() types.UID {
return types.UID(strconv.FormatInt(rand.Int63(), 10))
}

// generateDeployment creates a deployment, with the input image as its template
func generateDeployment(image string) extensions.Deployment {
podLabels := map[string]string{"name": image}
Expand Down Expand Up @@ -466,9 +474,18 @@ func TestEqualIgnoreHash(t *testing.T) {
}

func TestFindNewReplicaSet(t *testing.T) {
now := metav1.Now()
later := metav1.Time{Time: now.Add(time.Minute)}

deployment := generateDeployment("nginx")
newRS := generateRS(deployment)
newRS.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "different-hash"
newRS.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "hash"
newRS.CreationTimestamp = later

newRSDup := generateRS(deployment)
newRSDup.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "different-hash"
newRSDup.CreationTimestamp = now

oldDeployment := generateDeployment("nginx")
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
oldRS := generateRS(oldDeployment)
Expand All @@ -481,11 +498,17 @@ func TestFindNewReplicaSet(t *testing.T) {
expected *extensions.ReplicaSet
}{
{
test: "Get new ReplicaSet with the same spec but different pod-template-hash value",
test: "Get new ReplicaSet with the same template as Deployment spec but different pod-template-hash value",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS, &oldRS},
expected: &newRS,
},
{
test: "Get the oldest new ReplicaSet when there are more than one ReplicaSet with the same template",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS, &oldRS, &newRSDup},
expected: &newRSDup,
},
{
test: "Get nil new ReplicaSet",
deployment: deployment,
Expand All @@ -502,13 +525,23 @@ func TestFindNewReplicaSet(t *testing.T) {
}

func TestFindOldReplicaSets(t *testing.T) {
now := metav1.Now()
later := metav1.Time{Time: now.Add(time.Minute)}

deployment := generateDeployment("nginx")
newRS := generateRS(deployment)
newRS.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "different-hash"
newRS.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "hash"
newRS.CreationTimestamp = later

newRSDup := generateRS(deployment)
newRSDup.Labels[extensions.DefaultDeploymentUniqueLabelKey] = "different-hash"
newRSDup.CreationTimestamp = now

oldDeployment := generateDeployment("nginx")
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
oldRS := generateRS(oldDeployment)
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)

newPod := generatePodFromRS(newRS)
oldPod := generatePodFromRS(oldRS)

Expand Down Expand Up @@ -542,6 +575,18 @@ func TestFindOldReplicaSets(t *testing.T) {
},
expected: []*extensions.ReplicaSet{&oldRS},
},
{
test: "Get old ReplicaSets with two new ReplicaSets, only the oldest new ReplicaSet is seen as new ReplicaSet",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&oldRS, &newRS, &newRSDup},
podList: &v1.PodList{
Items: []v1.Pod{
newPod,
oldPod,
},
},
expected: []*extensions.ReplicaSet{&oldRS, &newRS},
},
{
test: "Get empty old ReplicaSets",
deployment: deployment,
Expand Down
4 changes: 2 additions & 2 deletions staging/src/k8s.io/apimachinery/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions staging/src/k8s.io/apiserver/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.