Skip to content

Commit

Permalink
Remove obsolete deployment helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Michail Kargakis <[email protected]>
  • Loading branch information
0xmichalis committed May 25, 2017
1 parent 4aa8b1a commit fcf68ba
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 85 deletions.
25 changes: 0 additions & 25 deletions pkg/controller/deployment/util/deployment_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,31 +746,6 @@ func LabelPodsWithHash(podList *v1.PodList, c clientset.Interface, podLister cor
return nil
}

// GetNewReplicaSetTemplate returns the desired PodTemplateSpec for the new ReplicaSet corresponding to the given ReplicaSet.
// Callers of this helper need to set the DefaultDeploymentUniqueLabelKey k/v pair.
func GetNewReplicaSetTemplate(deployment *extensions.Deployment) v1.PodTemplateSpec {
// newRS will have the same template as in deployment spec.
return v1.PodTemplateSpec{
ObjectMeta: deployment.Spec.Template.ObjectMeta,
Spec: deployment.Spec.Template.Spec,
}
}

// TODO: remove the duplicate
// GetNewReplicaSetTemplateInternal returns the desired PodTemplateSpec for the new ReplicaSet corresponding to the given ReplicaSet.
func GetNewReplicaSetTemplateInternal(deployment *internalextensions.Deployment) api.PodTemplateSpec {
// newRS will have the same template as in deployment spec, plus a unique label in some cases.
newRSTemplate := api.PodTemplateSpec{
ObjectMeta: deployment.Spec.Template.ObjectMeta,
Spec: deployment.Spec.Template.Spec,
}
newRSTemplate.ObjectMeta.Labels = labelsutil.CloneAndAddLabel(
deployment.Spec.Template.ObjectMeta.Labels,
internalextensions.DefaultDeploymentUniqueLabelKey,
fmt.Sprintf("%d", GetInternalPodTemplateSpecHash(newRSTemplate)))
return newRSTemplate
}

// SetFromReplicaSetTemplate sets the desired PodTemplateSpec from a replica set template to the given deployment.
func SetFromReplicaSetTemplate(deployment *extensions.Deployment, template v1.PodTemplateSpec) *extensions.Deployment {
deployment.Spec.Template.ObjectMeta = template.ObjectMeta
Expand Down
34 changes: 10 additions & 24 deletions pkg/controller/deployment/util/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package util

import (
"encoding/json"
"hash/adler32"
"strconv"
"strings"
"testing"

"k8s.io/kubernetes/pkg/api/v1"
hashutil "k8s.io/kubernetes/pkg/util/hash"
)

var podSpec string = `
Expand Down Expand Up @@ -103,34 +105,12 @@ var podSpec string = `

func TestPodTemplateSpecHash(t *testing.T) {
seenHashes := make(map[uint32]int)
broken := false

for i := 0; i < 1000; i++ {
specJson := strings.Replace(podSpec, "@@VERSION@@", strconv.Itoa(i), 1)
spec := v1.PodTemplateSpec{}
json.Unmarshal([]byte(specJson), &spec)
hash := GetPodTemplateSpecHash(spec)
if v, ok := seenHashes[hash]; ok {
broken = true
t.Logf("Hash collision, old: %d new: %d", v, i)
break
}
seenHashes[hash] = i
}

if !broken {
t.Errorf("expected adler to break but it didn't")
}
}

func TestPodTemplateSpecHashFnv(t *testing.T) {
seenHashes := make(map[uint32]int)

for i := 0; i < 1000; i++ {
specJson := strings.Replace(podSpec, "@@VERSION@@", strconv.Itoa(i), 1)
spec := v1.PodTemplateSpec{}
json.Unmarshal([]byte(specJson), &spec)
hash := GetPodTemplateSpecHashFnv(spec)
if v, ok := seenHashes[hash]; ok {
t.Errorf("Hash collision, old: %d new: %d", v, i)
break
Expand All @@ -144,15 +124,21 @@ func BenchmarkAdler(b *testing.B) {
json.Unmarshal([]byte(podSpec), &spec)

for i := 0; i < b.N; i++ {
GetPodTemplateSpecHash(spec)
getPodTemplateSpecOldHash(spec)
}
}

func getPodTemplateSpecOldHash(template v1.PodTemplateSpec) uint32 {
podTemplateSpecHasher := adler32.New()
hashutil.DeepHashObject(podTemplateSpecHasher, template)
return podTemplateSpecHasher.Sum32()
}

func BenchmarkFnv(b *testing.B) {
spec := v1.PodTemplateSpec{}
json.Unmarshal([]byte(podSpec), &spec)

for i := 0; i < b.N; i++ {
GetPodTemplateSpecHashFnv(spec)
GetPodTemplateSpecHash(spec)
}
}
14 changes: 0 additions & 14 deletions pkg/controller/deployment/util/pod_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package util

import (
"hash/adler32"
"hash/fnv"

"github.com/golang/glog"
Expand All @@ -32,19 +31,6 @@ import (
)

func GetPodTemplateSpecHash(template v1.PodTemplateSpec) uint32 {
podTemplateSpecHasher := adler32.New()
hashutil.DeepHashObject(podTemplateSpecHasher, template)
return podTemplateSpecHasher.Sum32()
}

// TODO: remove the duplicate
func GetInternalPodTemplateSpecHash(template api.PodTemplateSpec) uint32 {
podTemplateSpecHasher := adler32.New()
hashutil.DeepHashObject(podTemplateSpecHasher, template)
return podTemplateSpecHasher.Sum32()
}

func GetPodTemplateSpecHashFnv(template v1.PodTemplateSpec) uint32 {
podTemplateSpecHasher := fnv.New32a()
hashutil.DeepHashObject(podTemplateSpecHasher, template)
return podTemplateSpecHasher.Sum32()
Expand Down
10 changes: 0 additions & 10 deletions pkg/controller/deployment/util/replicaset_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,3 @@ func GetReplicaSetHash(rs *extensions.ReplicaSet) string {
Spec: rs.Spec.Template.Spec,
}))
}

// GetReplicaSetHashFnv returns the pod template hash of a ReplicaSet's pod template spec.
func GetReplicaSetHashFnv(rs *extensions.ReplicaSet) string {
meta := rs.Spec.Template.ObjectMeta
meta.Labels = labelsutil.CloneAndRemoveLabel(meta.Labels, extensions.DefaultDeploymentUniqueLabelKey)
return fmt.Sprintf("%d", GetPodTemplateSpecHashFnv(v1.PodTemplateSpec{
ObjectMeta: meta,
Spec: rs.Spec.Template.Spec,
}))
}
15 changes: 3 additions & 12 deletions pkg/kubectl/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
)

func TestReplicationControllerStop(t *testing.T) {
Expand Down Expand Up @@ -441,7 +440,6 @@ func TestDeploymentStop(t *testing.T) {
Replicas: 0,
},
}
template := deploymentutil.GetNewReplicaSetTemplateInternal(&deployment)
trueVar := true
tests := []struct {
Name string
Expand Down Expand Up @@ -492,9 +490,7 @@ func TestDeploymentStop(t *testing.T) {
},
},
},
Spec: extensions.ReplicaSetSpec{
Template: template,
},
Spec: extensions.ReplicaSetSpec{},
},
// ReplicaSet owned by something else (should be ignored).
{
Expand All @@ -512,9 +508,7 @@ func TestDeploymentStop(t *testing.T) {
},
},
},
Spec: extensions.ReplicaSetSpec{
Template: template,
},
Spec: extensions.ReplicaSetSpec{},
},
},
},
Expand Down Expand Up @@ -709,7 +703,6 @@ func TestDeploymentNotFoundError(t *testing.T) {
Replicas: 0,
},
}
template := deploymentutil.GetNewReplicaSetTemplateInternal(deployment)

fake := fake.NewSimpleClientset(
deployment,
Expand All @@ -719,9 +712,7 @@ func TestDeploymentNotFoundError(t *testing.T) {
Name: name,
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{
Template: template,
},
Spec: extensions.ReplicaSetSpec{},
},
},
},
Expand Down

0 comments on commit fcf68ba

Please sign in to comment.