1
1
/*
2
- * Copyright (c) 2021, 2023 , Oracle and/or its affiliates.
2
+ * Copyright (c) 2020, 2025 , Oracle and/or its affiliates.
3
3
* Licensed under the Universal Permissive License v 1.0 as shown at
4
4
* http://oss.oracle.com/licenses/upl.
5
5
*/
@@ -8,19 +8,34 @@ package v1
8
8
9
9
import (
10
10
"encoding/binary"
11
+ "encoding/json"
11
12
"fmt"
12
- "github.com/davecgh/go-spew/spew"
13
- "hash"
14
13
"hash/fnv"
15
14
"k8s.io/apimachinery/pkg/util/rand"
16
15
)
17
16
18
- func EnsureHashLabel (c CoherenceResource ) (string , bool ) {
17
+ func EnsureCoherenceHashLabel (c * Coherence ) (string , bool ) {
19
18
labels := c .GetLabels ()
20
19
if labels == nil {
21
20
labels = make (map [string ]string )
22
21
}
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
24
39
hashNew := ComputeHash (& spec , nil )
25
40
hashCurrent , found := labels [LabelCoherenceHash ]
26
41
if ! found || hashCurrent != hashNew {
@@ -33,30 +48,25 @@ func EnsureHashLabel(c CoherenceResource) (string, bool) {
33
48
34
49
// ComputeHash returns a hash value calculated from Coherence spec and
35
50
// 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 )
39
55
40
56
// Add collisionCount in the hash if it exists.
41
57
if collisionCount != nil {
42
58
collisionCountBytes := make ([]byte , 8 )
43
59
binary .LittleEndian .PutUint32 (collisionCountBytes , uint32 (* collisionCount ))
44
- _ , _ = podTemplateSpecHasher .Write (collisionCountBytes )
60
+ _ , _ = hasher .Write (collisionCountBytes )
45
61
}
46
62
47
- return rand .SafeEncodeString (fmt .Sprint (podTemplateSpecHasher .Sum32 ()))
63
+ return rand .SafeEncodeString (fmt .Sprint (hasher .Sum32 ()))
48
64
}
49
65
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
+ //}
0 commit comments