Skip to content

Commit a9287e5

Browse files
committed
hash: breaking api change
the hash interfaces returned errors before, but the underlying implementations picked for the hash package never return errors on calls to Write. bubbling the errors in the interface like this leads to a lot of unnecessary error handling, so it's worth breaking the api contract here (and we're pre-1.0.0 anyway).
1 parent 3b0fa4d commit a9287e5

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

component/ensure_component.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ func (e *EnsureComponentByHash[K, A]) Handle(ctx context.Context) {
5555
ownedObjs := e.List(ctx, e.nn.MustValue(ctx))
5656

5757
newObj := e.newObj(ctx)
58-
hash, err := e.Hash(newObj)
59-
if err != nil {
60-
e.ctrls.RequeueErr(ctx, err)
61-
return
62-
}
58+
hash := e.Hash(newObj)
6359
newObj = newObj.WithAnnotations(map[string]string{e.HashAnnotationKey: hash})
6460

6561
matchingObjs := make([]K, 0)
@@ -78,7 +74,7 @@ func (e *EnsureComponentByHash[K, A]) Handle(ctx context.Context) {
7874

7975
if len(matchingObjs) == 0 {
8076
// apply if no matching KubeObject in cluster
81-
_, err = e.applyObject(ctx, newObj)
77+
_, err := e.applyObject(ctx, newObj)
8278
if err != nil {
8379
e.ctrls.RequeueErr(ctx, err)
8480
return

hash/hash.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
)
1414

1515
type (
16-
ObjectHashFunc func(obj any) (string, error)
16+
ObjectHashFunc func(obj any) string
1717
EqualFunc func(a, b string) bool
1818
)
1919

2020
// ObjectHasher hashes and object and can compare hashes for equality
2121
type ObjectHasher interface {
22-
Hash(obj any) (string, error)
22+
Hash(obj any) string
2323
Equal(a, b string) bool
2424
}
2525

@@ -28,7 +28,7 @@ type hasher struct {
2828
EqualFunc
2929
}
3030

31-
func (h *hasher) Hash(obj interface{}) (string, error) {
31+
func (h *hasher) Hash(obj interface{}) string {
3232
return h.ObjectHashFunc(obj)
3333
}
3434

@@ -54,25 +54,24 @@ func NewObjectHash() ObjectHasher {
5454

5555
// SecureObject canonicalizes the object before hashing with sha512 and then
5656
// with xxhash
57-
func SecureObject(obj interface{}) (string, error) {
57+
func SecureObject(obj interface{}) string {
5858
hasher := sha512.New512_256()
5959
printer := spew.ConfigState{
6060
Indent: " ",
6161
SortKeys: true,
6262
DisableMethods: true,
6363
SpewKeys: true,
6464
}
65-
_, err := printer.Fprintf(hasher, "%#v", obj)
66-
if err != nil {
67-
return "", err
68-
}
65+
// sha512's hasher.Write never returns an error, and Fprintf just passes up
66+
// the underlying Write call's error, so we can safely ignore the error here
67+
_, _ = printer.Fprintf(hasher, "%#v", obj)
6968
// xxhash the sha512 hash to get a shorter value
7069
xxhasher := xxhash.New()
71-
_, err = xxhasher.Write(hasher.Sum(nil))
72-
if err != nil {
73-
return "", err
74-
}
75-
return rand.SafeEncodeString(fmt.Sprint(xxhasher.Sum(nil))), nil
70+
71+
// xxhash's hasher.Write never returns an error, so we can safely ignore
72+
// the error here tpp
73+
_, _ = xxhasher.Write(hasher.Sum(nil))
74+
return rand.SafeEncodeString(fmt.Sprint(xxhasher.Sum(nil)))
7675
}
7776

7877
// SecureEqual compares hashes safely
@@ -81,19 +80,19 @@ func SecureEqual(a, b string) bool {
8180
}
8281

8382
// Object canonicalizes the object before hashing with xxhash
84-
func Object(obj interface{}) (string, error) {
83+
func Object(obj interface{}) string {
8584
hasher := xxhash.New()
8685
printer := spew.ConfigState{
8786
Indent: " ",
8887
SortKeys: true,
8988
DisableMethods: true,
9089
SpewKeys: true,
9190
}
92-
_, err := printer.Fprintf(hasher, "%#v", obj)
93-
if err != nil {
94-
return "", err
95-
}
96-
return rand.SafeEncodeString(fmt.Sprint(hasher.Sum(nil))), nil
91+
92+
// xxhash's hasher.Write never returns an error, and Fprintf just passes up
93+
// the underlying Write call's error, so we can safely ignore the error here
94+
_, _ = printer.Fprintf(hasher, "%#v", obj)
95+
return rand.SafeEncodeString(fmt.Sprint(hasher.Sum(nil)))
9796
}
9897

9998
// Equal compares hashes safely

hash/hash_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func ExampleObject() {
1212
"some": "data",
1313
},
1414
}
15-
hash, _ := Object(configmap)
15+
hash := Object(configmap)
1616
fmt.Println(Equal(hash, "n688h54h56ch64bh677h55fh648hddq"))
1717
// Output: true
1818
}
@@ -23,7 +23,7 @@ func ExampleSecureObject() {
2323
"some": "data",
2424
},
2525
}
26-
hash, _ := SecureObject(secret)
26+
hash := SecureObject(secret)
2727
fmt.Println(SecureEqual(hash, "n665hb8h667h68hfbhffh669h54dq"))
2828
// Output: true
2929
}

0 commit comments

Comments
 (0)