Skip to content

Instantly share code, notes, and snippets.

@uhthomas
Created October 9, 2023 16:37
Show Gist options
  • Save uhthomas/dc7cd51a1f5ec6b5ae14cc8a82916118 to your computer and use it in GitHub Desktop.
Save uhthomas/dc7cd51a1f5ec6b5ae14cc8a82916118 to your computer and use it in GitHub Desktop.
// You can edit this code!
// Click here and start typing.
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
)
const (
V1ApplySetIdFormat = "applyset-%s-v1"
applySetIDPartDelimiter = "."
)
type ApplySet struct {
parentRefName,
parentRefNamespace,
parentRefKind,
parentRefGroup string
}
func (a ApplySet) ID() string {
unencoded := strings.Join([]string{a.parentRefName, a.parentRefNamespace, a.parentRefKind, a.parentRefGroup}, applySetIDPartDelimiter)
hashed := sha256.Sum256([]byte(unencoded))
b64 := base64.RawURLEncoding.EncodeToString(hashed[:])
// Label values must start and end with alphanumeric values, so add a known-safe prefix and suffix.
return fmt.Sprintf(V1ApplySetIdFormat, b64)
}
func main() {
fmt.Println(ApplySet{
parentRefName: "automata",
parentRefNamespace: "",
parentRefKind: "ApplySet",
parentRefGroup: "starjunk.net",
}.ID())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment