This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use stable hash for labels and name suffixes (aligning with OLMv0 cha…
…nges) Signed-off-by: Joe Lanford <[email protected]>
- Loading branch information
1 parent
42e713e
commit b4e9976
Showing
6 changed files
with
108 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,39 @@ | ||
package util | ||
|
||
import ( | ||
"hash" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"crypto/sha256" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
) | ||
|
||
// DeepHashObject writes specified object to hash using the spew library | ||
// which follows pointers and prints actual values of the nested objects | ||
// ensuring the hash does not change when a pointer changes. | ||
// From https://github.com/operator-framework/operator-lifecycle-manager/blob/master/pkg/lib/kubernetes/pkg/util/hash/hash.go | ||
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) { | ||
func DeepHashObject(obj interface{}) (string, error) { | ||
// While the most accurate encoding we could do for Kubernetes objects (runtime.Object) | ||
// would use the API machinery serializers, those operate over entire objects - and | ||
// we often need to operate on snippets. Checking with the experts and the implementation, | ||
// we can see that the serializers are a thin wrapper over json.Marshal for encoding: | ||
// https://github.com/kubernetes/kubernetes/blob/8509ab82b96caa2365552efa08c8ba8baf11c5ec/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go#L216-L247 | ||
// Therefore, we can be confident that using json.Marshal() here will: | ||
// 1. be stable & idempotent - the library sorts keys, etc. | ||
// 2. be germane to our needs - only fields that serialize and are sent to the server | ||
// will be encoded | ||
|
||
hasher := sha256.New224() | ||
hasher.Reset() | ||
printer := spew.ConfigState{ | ||
Indent: " ", | ||
SortKeys: true, | ||
DisableMethods: true, | ||
SpewKeys: true, | ||
encoder := json.NewEncoder(hasher) | ||
if err := encoder.Encode(obj); err != nil { | ||
return "", fmt.Errorf("couldn't encode object: %w", err) | ||
} | ||
printer.Fprintf(hasher, "%#v", objectToWrite) | ||
|
||
// base62(sha224(bytes)) is a useful hash and encoding for adding the contents of this | ||
// to a Kubernetes identifier or other field which has length and character set requirements | ||
var hash []byte | ||
hash = hasher.Sum(hash) | ||
|
||
var i big.Int | ||
i.SetBytes(hash[:]) | ||
return i.Text(36), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters