package provider import ( "crypto/sha256" "encoding/base32" "strings" "k8s.io/apimachinery/pkg/types" ) const namePrefix = "proxy-" // NameFromUID derives a deterministic instance name from a Proxy CR's UID. // Provider.Create implementations key idempotency on this name: a repeat // call after a crash must find the existing instance by this name rather // than create a duplicate, and the reconciler relies on that to recover // providerID after a crash even if status was wiped. // // SHA-256 of the UID, truncated to the first 10 bytes, RFC 4648 base32 // (no padding), lowercased: exactly 16 characters, for 22 total with the // "proxy-" prefix. That gives 80 bits of collision resistance — a birthday // collision only becomes likely around 2^40 objects, against a fleet of // tens — while satisfying GCP's instance name rules // (^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$, <=63 chars) with headroom, since GCP // auto-names some resources (e.g. the boot disk) after the instance name. // base32's alphabet (A-Z2-7) is entirely legal once lowercased; base64 is // not (has '+', '/', and uppercase), and hex would need 20 characters for // the same 80 bits. func NameFromUID(uid types.UID) string { sum := sha256.Sum256([]byte(uid)) enc := base32.StdEncoding.WithPadding(base32.NoPadding) return namePrefix + strings.ToLower(enc.EncodeToString(sum[:10])) }