Skip to content

Commit 6dd198f

Browse files
committed
Fix hash code
1 parent a0c0a89 commit 6dd198f

File tree

7 files changed

+63
-46
lines changed

7 files changed

+63
-46
lines changed

api/v1/coherence_webhook.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -54,6 +54,12 @@ func (in *Coherence) Default() {
5454
spec.SetReplicas(spec.GetReplicas())
5555
}
5656
SetCommonDefaults(in)
57+
58+
// apply a label with the hash of the spec - ths must be the last action here to make sure that
59+
// any modifications to the spec field are included in the hash
60+
if hash, applied := EnsureCoherenceHashLabel(in); applied {
61+
webhookLogger.Info(fmt.Sprintf("Applied %s label", LabelCoherenceHash), "hash", hash)
62+
}
5763
}
5864

5965
// SetCommonDefaults sets defaults common to both a Job and StatefulSet
@@ -119,12 +125,6 @@ func SetCommonDefaults(in CoherenceResource) {
119125

120126
// apply the Operator version annotation
121127
in.AddAnnotation(AnnotationOperatorVersion, operator.GetVersion())
122-
123-
// apply a label with the hash of the spec - ths must be the last action here to make sure that
124-
// any modifications to the spec field are included in the hash
125-
if hash, applied := EnsureHashLabel(in); applied {
126-
logger.Info(fmt.Sprintf("Applied %s label", LabelCoherenceHash), "hash", hash)
127-
}
128128
}
129129

130130
// The path in this annotation MUST match the const below

api/v1/coherence_webhook_job.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -67,6 +67,12 @@ func (in *CoherenceJob) Default() {
6767
}
6868

6969
SetCommonDefaults(in)
70+
71+
// apply a label with the hash of the spec - ths must be the last action here to make sure that
72+
// any modifications to the spec field are included in the hash
73+
if hash, applied := EnsureJobHashLabel(in); applied {
74+
webhookLogger.Info(fmt.Sprintf("Applied %s label", LabelCoherenceHash), "hash", hash)
75+
}
7076
}
7177

7278
// The path in this annotation MUST match the const below

api/v1/hasher.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -8,19 +8,34 @@ package v1
88

99
import (
1010
"encoding/binary"
11+
"encoding/json"
1112
"fmt"
12-
"github.com/davecgh/go-spew/spew"
13-
"hash"
1413
"hash/fnv"
1514
"k8s.io/apimachinery/pkg/util/rand"
1615
)
1716

18-
func EnsureHashLabel(c CoherenceResource) (string, bool) {
17+
func EnsureCoherenceHashLabel(c *Coherence) (string, bool) {
1918
labels := c.GetLabels()
2019
if labels == nil {
2120
labels = make(map[string]string)
2221
}
23-
spec := c.GetSpec()
22+
spec := c.Spec
23+
hashNew := ComputeHash(&spec, nil)
24+
hashCurrent, found := labels[LabelCoherenceHash]
25+
if !found || hashCurrent != hashNew {
26+
labels[LabelCoherenceHash] = hashNew
27+
c.SetLabels(labels)
28+
return hashNew, true
29+
}
30+
return hashCurrent, false
31+
}
32+
33+
func EnsureJobHashLabel(c *CoherenceJob) (string, bool) {
34+
labels := c.GetLabels()
35+
if labels == nil {
36+
labels = make(map[string]string)
37+
}
38+
spec := c.Spec
2439
hashNew := ComputeHash(&spec, nil)
2540
hashCurrent, found := labels[LabelCoherenceHash]
2641
if !found || hashCurrent != hashNew {
@@ -33,30 +48,25 @@ func EnsureHashLabel(c CoherenceResource) (string, bool) {
3348

3449
// ComputeHash returns a hash value calculated from Coherence spec and
3550
// The hash will be safe encoded to avoid bad words.
36-
func ComputeHash(template interface{}, collisionCount *int32) string {
37-
podTemplateSpecHasher := fnv.New32a()
38-
DeepHashObject(podTemplateSpecHasher, template)
51+
func ComputeHash(in interface{}, collisionCount *int32) string {
52+
hasher := fnv.New32a()
53+
b, _ := json.Marshal(in)
54+
_, _ = hasher.Write(b)
3955

4056
// Add collisionCount in the hash if it exists.
4157
if collisionCount != nil {
4258
collisionCountBytes := make([]byte, 8)
4359
binary.LittleEndian.PutUint32(collisionCountBytes, uint32(*collisionCount))
44-
_, _ = podTemplateSpecHasher.Write(collisionCountBytes)
60+
_, _ = hasher.Write(collisionCountBytes)
4561
}
4662

47-
return rand.SafeEncodeString(fmt.Sprint(podTemplateSpecHasher.Sum32()))
63+
return rand.SafeEncodeString(fmt.Sprint(hasher.Sum32()))
4864
}
4965

50-
// DeepHashObject writes specified object to hash using the spew library
51-
// which follows pointers and prints actual values of the nested objects
52-
// ensuring the hash does not change when a pointer changes.
53-
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
54-
hasher.Reset()
55-
printer := spew.ConfigState{
56-
Indent: " ",
57-
SortKeys: true,
58-
DisableMethods: true,
59-
SpewKeys: true,
60-
}
61-
_, _ = printer.Fprintf(hasher, "%#v", objectToWrite)
62-
}
66+
//// DeepHashObject writes specified object to hash using the spew library
67+
//// which follows pointers and prints actual values of the nested objects
68+
//// ensuring the hash does not change when a pointer changes.
69+
//func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
70+
// b, _ := json.Marshal(objectToWrite)
71+
// hasher.Write(b)
72+
//}

api/v1/hasher_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -26,10 +26,11 @@ func TestHash(t *testing.T) {
2626
Spec: spec,
2727
}
2828

29-
coh.EnsureHashLabel(deployment)
29+
coh.EnsureCoherenceHashLabel(deployment)
3030

3131
// If this test fails you have probably added a new field to CoherenceResourceSpec
3232
// This will break backwards compatibility. This field needs to be added to
3333
// both CoherenceStatefulSetResourceSpec and CoherenceJobResourceSpec instead
34-
g.Expect(deployment.GetLabels()["coherence-hash"]).To(Equal("5cb9fd9f96"))
34+
//g.Expect(deployment.GetLabels()["coherence-hash"]).To(Equal("5cb9fd9f96"))
35+
g.Expect(deployment.GetLabels()["coherence-hash"]).To(Equal("5859f96865"))
3536
}

controllers/coherence_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -349,14 +349,14 @@ func (in *CoherenceReconciler) ensureHashApplied(ctx context.Context, c *coh.Coh
349349

350350
// Re-fetch the Coherence resource to ensure we have the most recent copy
351351
latest := c.DeepCopy()
352-
hash, _ := coh.EnsureHashLabel(latest)
352+
hash, _ := coh.EnsureCoherenceHashLabel(latest)
353353

354354
if currentHash != hash {
355-
if c.IsBeforeVersion("3.3.0") {
356-
// Before 3.3.0 there was a bug calculating the has in the defaulting web-hook
355+
if c.IsBeforeVersion("3.4.2") {
356+
// Before 3.4.2 there was a bug calculating the hash in the defaulting web-hook
357357
// This would cause the hashes to be different here, when in fact they should not be
358358
// If the Coherence resource being processes has no version annotation, or a version
359-
// prior to 3.3.0 then we return as if the hashes matched
359+
// prior to 3.4.2 then we return as if the hashes matched
360360
if labels == nil {
361361
labels = make(map[string]string)
362362
}

controllers/coherencejob_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,23 @@ func (in *CoherenceJobReconciler) SetupWithManager(mgr ctrl.Manager, cs clients.
271271
func (in *CoherenceJobReconciler) GetReconciler() reconcile.Reconciler { return in }
272272

273273
// ensureHashApplied ensures that the hash label is present in the CoherenceJob resource, patching it if required
274-
func (in *CoherenceJobReconciler) ensureHashApplied(ctx context.Context, c coh.CoherenceResource) (bool, error) {
274+
func (in *CoherenceJobReconciler) ensureHashApplied(ctx context.Context, c *coh.CoherenceJob) (bool, error) {
275275
currentHash := ""
276276
labels := c.GetLabels()
277277
if len(labels) > 0 {
278278
currentHash = labels[coh.LabelCoherenceHash]
279279
}
280280

281281
// Re-fetch the CoherenceJob resource to ensure we have the most recent copy
282-
latest := c.DeepCopyResource()
283-
hash, _ := coh.EnsureHashLabel(latest)
282+
latest := c.DeepCopy()
283+
hash, _ := coh.EnsureJobHashLabel(latest)
284284

285285
if currentHash != hash {
286-
if c.IsBeforeVersion("3.2.8") {
287-
// Before 3.2.8 there was a bug calculating the has in the defaulting web-hook
286+
if c.IsBeforeVersion("3.4.2") {
287+
// Before 3.4.2 there was a bug calculating the has in the defaulting web-hook
288288
// This would cause the hashes to be different here, when in fact they should not be
289289
// If the CoherenceJob resource being processes has no version annotation, or a version
290-
// prior to 3.2.8 then we return as if the hashes matched
290+
// prior to 3.4.2 then we return as if the hashes matched
291291
if labels == nil {
292292
labels = make(map[string]string)
293293
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.23.0
55
toolchain go1.23.4
66

77
require (
8-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
98
github.com/distribution/reference v0.6.0
109
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1110
github.com/go-logr/logr v1.4.2
@@ -31,6 +30,7 @@ require (
3130
require (
3231
github.com/beorn7/perks v1.0.1 // indirect
3332
github.com/cespare/xxhash/v2 v2.3.0 // indirect
33+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3434
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
3535
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
3636
github.com/evanphx/json-patch/v5 v5.9.0 // indirect

0 commit comments

Comments
 (0)