Skip to content

Commit 0db1d12

Browse files
committed
hash: update hash encoding
the rand.SafeEncodeString doesn't add any value for a hash, and just makes the encoded version longer
1 parent 1f7573b commit 0db1d12

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

component/ensure_component_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package component
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -78,7 +79,7 @@ func TestEnsureServiceHandler(t *testing.T) {
7879
"example.com/component": "the-main-service-component",
7980
},
8081
Annotations: map[string]string{
81-
hashKey: "n55dh7ch6bh5c4h75h68bh644hcfq",
82+
hashKey: "76251aaa1ff6c84f",
8283
},
8384
}},
8485
},
@@ -93,7 +94,7 @@ func TestEnsureServiceHandler(t *testing.T) {
9394
"example.com/component": "the-main-service-component",
9495
},
9596
Annotations: map[string]string{
96-
hashKey: "n55dh7ch6bh5c4h75h68bh644hcfq",
97+
hashKey: "76251aaa1ff6c84f",
9798
},
9899
},
99100
}, &corev1.Service{ObjectMeta: metav1.ObjectMeta{
@@ -145,8 +146,9 @@ func TestEnsureServiceHandler(t *testing.T) {
145146
hash.NewObjectHash(), hashKey),
146147
ctxOwner,
147148
queueOps,
148-
func(_ context.Context, _ *applycorev1.ServiceApplyConfiguration) (*corev1.Service, error) {
149+
func(_ context.Context, sac *applycorev1.ServiceApplyConfiguration) (*corev1.Service, error) {
149150
applyCalled = true
151+
fmt.Print(sac.Annotations)
150152
return nil, nil
151153
},
152154
func(_ context.Context, _ types.NamespacedName) error {

fileinformer/file_informer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestFileInformer(t *testing.T) {
4646
require.Len(t, eventHandlers.Calls, 1)
4747
eventHandlers.Unlock()
4848
eventHandlers2.Lock()
49-
require.Len(t, len(eventHandlers2.Calls), 1)
49+
require.Len(t, eventHandlers2.Calls, 1)
5050
eventHandlers2.Unlock()
5151

5252
// expect an OnAdd when the file is written

hash/hash.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/cespare/xxhash/v2"
1111
"github.com/davecgh/go-spew/spew"
12-
"k8s.io/apimachinery/pkg/util/rand"
1312
)
1413

1514
type (
@@ -71,7 +70,7 @@ func SecureObject(obj interface{}) string {
7170
// xxhash's hasher.Write never returns an error, so we can safely ignore
7271
// the error here tpp
7372
_, _ = xxhasher.Write(hasher.Sum(nil))
74-
return rand.SafeEncodeString(fmt.Sprint(string(xxhasher.Sum(nil))))
73+
return fmt.Sprintf("%x", xxhasher.Sum64())
7574
}
7675

7776
// SecureEqual compares hashes safely
@@ -92,7 +91,7 @@ func Object(obj interface{}) string {
9291
// xxhash's hasher.Write never returns an error, and Fprintf just passes up
9392
// the underlying Write call's error, so we can safely ignore the error here
9493
_, _ = printer.Fprintf(hasher, "%#v", obj)
95-
return rand.SafeEncodeString(fmt.Sprint(string(hasher.Sum(nil))))
94+
return fmt.Sprintf("%x", hasher.Sum64())
9695
}
9796

9897
// Equal compares hashes safely

hash/hash_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func ExampleObject() {
1313
},
1414
}
1515
hash := Object(configmap)
16-
fmt.Println(Equal(hash, "n688h54h56ch64bh677h55fh648hddq"))
16+
fmt.Println(Equal(hash, "f40a7fcee977cc58"))
1717
// Output: true
1818
}
1919

@@ -24,6 +24,6 @@ func ExampleSecureObject() {
2424
},
2525
}
2626
hash := SecureObject(secret)
27-
fmt.Println(SecureEqual(hash, "n665hb8h667h68hfbhffh669h54dq"))
27+
fmt.Println(SecureEqual(hash, "dd40df186063e16c"))
2828
// Output: true
2929
}

0 commit comments

Comments
 (0)