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

Change step hashing function to derive hash from json representation of steps #108

Merged
merged 1 commit into from
Jun 14, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions utils/conditions/conditions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package conditions

import (
"encoding/json"
"fmt"
"hash/fnv"
"math"
Expand Down Expand Up @@ -236,12 +237,17 @@ func RolloutComplete(rollout *v1alpha1.Rollout, newStatus *v1alpha1.RolloutStatu
// ComputeStepHash returns a hash value calculated from the Rollout's steps. The hash will
// be safe encoded to avoid bad words.
func ComputeStepHash(rollout *v1alpha1.Rollout) string {
rolloutStepHasher := fnv.New32a()
if rollout.Spec.Strategy.BlueGreenStrategy != nil {
if rollout.Spec.Strategy.BlueGreenStrategy != nil || rollout.Spec.Strategy.CanaryStrategy == nil {
return ""
}
if rollout.Spec.Strategy.CanaryStrategy != nil {
hashutil.DeepHashObject(rolloutStepHasher, rollout.Spec.Strategy.CanaryStrategy.Steps)
rolloutStepHasher := fnv.New32a()
stepsBytes, err := json.Marshal(rollout.Spec.Strategy.CanaryStrategy.Steps)
if err != nil {
panic(err)
}
_, err = rolloutStepHasher.Write(stepsBytes)
if err != nil {
panic(err)
}
return rand.SafeEncodeString(fmt.Sprint(rolloutStepHasher.Sum32()))
}
Expand Down
8 changes: 7 additions & 1 deletion utils/conditions/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,9 @@ func TestComputeGenerationHash(t *testing.T) {
assert.NotEqual(t, baseline, roPausedHash)
}

// TestComputeStableStepHash verifies we generate different hashes for various step definitions.
// Also verifies we do not unintentionally break our ComputeStepHash function somehow (e.g. by
// modifying types or change libraries)
func TestComputeStepHash(t *testing.T) {
ro := &v1alpha1.Rollout{
Spec: v1alpha1.RolloutSpec{
Expand All @@ -788,23 +791,26 @@ func TestComputeStepHash(t *testing.T) {
},
}
roWithDiffStepsHash := ComputeStepHash(roWithDiffSteps)
assert.Equal(t, "79c9b9f6bf", roWithDiffStepsHash)

roWithSameSteps := ro.DeepCopy()
roWithSameSteps.Status.CurrentPodHash = "Test"
roWithSameSteps.Spec.Replicas = pointer.Int32Ptr(1)
roWithSameStepsHash := ComputeStepHash(roWithSameSteps)
assert.Equal(t, "6b9b86fbd5", roWithSameStepsHash)

roNoSteps := ro.DeepCopy()
roNoSteps.Spec.Strategy.CanaryStrategy.Steps = nil
roNoStepsHash := ComputeStepHash(roNoSteps)
assert.Equal(t, "5ffbfbbd64", roNoStepsHash)

roBlueGreen := ro.DeepCopy()
roBlueGreen.Spec.Strategy.CanaryStrategy = nil
roBlueGreen.Spec.Strategy.BlueGreenStrategy = &v1alpha1.BlueGreenStrategy{}
roBlueGreenHash := ComputeStepHash(roBlueGreen)
assert.Equal(t, "", roBlueGreenHash)

assert.NotEqual(t, baseline, roWithDiffStepsHash)
assert.Equal(t, baseline, roWithSameStepsHash)
assert.NotEqual(t, baseline, roNoStepsHash)
assert.Equal(t, "", roBlueGreenHash)
}