K8s: fix UID creator and paths (#79769)

This commit is contained in:
Ryan McKinley 2023-12-20 14:42:38 -08:00 committed by GitHub
parent f05cbe589a
commit 10bb02e026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 9 deletions

View File

@ -38,7 +38,7 @@ func TestPlaylistConversion(t *testing.T) {
"metadata": {
"name": "abc",
"namespace": "org-3",
"uid": "Ik_jZSxBTV42xgQIwUsTiVx68S3RzWnzrCUVhHqvaxM",
"uid": "f0zxjm7ApxOafsn6DLQZ4Ezp78WRUsZqSc4taOSHq1gX",
"resourceVersion": "54321",
"creationTimestamp": "1970-01-01T00:00:12Z",
"annotations": {

View File

@ -42,7 +42,7 @@ func GetAPIHandler(delegateHandler http.Handler, restConfig *restclient.Config,
if err != nil {
return nil, err
}
sub.HandleFunc(route.Path, route.Handler).
sub.HandleFunc("/"+route.Path, route.Handler).
Methods(methods...)
}
@ -60,7 +60,7 @@ func GetAPIHandler(delegateHandler http.Handler, restConfig *restclient.Config,
if err != nil {
return nil, err
}
sub.HandleFunc(route.Path, route.Handler).
sub.HandleFunc("/"+route.Path, route.Handler).
Methods(methods...)
}
}

View File

@ -4,19 +4,34 @@ import (
"crypto/sha256"
"encoding/base64"
"strings"
"unicode"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
// Create a stable UID that will be unique across a multi-tenant cluster
func CalculateClusterWideUID(obj metav1.Object) types.UID {
// This is useful while we migrate from SQL storage to something where the UID (GUID)
// is actually baked into the storage engine itself.
func CalculateClusterWideUID(obj runtime.Object) types.UID {
gvk := obj.GetObjectKind().GroupVersionKind()
hasher := sha256.New()
hasher.Write([]byte(obj.GetResourceVersion()))
hasher.Write([]byte(gvk.Group))
hasher.Write([]byte("|"))
hasher.Write([]byte(obj.GetNamespace()))
hasher.Write([]byte(gvk.Kind))
hasher.Write([]byte("|"))
hasher.Write([]byte(obj.GetName()))
meta, err := meta.Accessor(obj)
if err == nil {
hasher.Write([]byte(meta.GetNamespace()))
hasher.Write([]byte("|"))
hasher.Write([]byte(meta.GetName()))
}
v := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
return types.UID(strings.ReplaceAll(v, "=", ""))
return types.UID(strings.Map(func(r rune) rune {
if !(unicode.IsLetter(r) || unicode.IsDigit(r)) {
return 'X'
}
return r
}, v))
}