mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s: fix UID creator and paths (#79769)
This commit is contained in:
@@ -38,7 +38,7 @@ func TestPlaylistConversion(t *testing.T) {
|
|||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "abc",
|
"name": "abc",
|
||||||
"namespace": "org-3",
|
"namespace": "org-3",
|
||||||
"uid": "Ik_jZSxBTV42xgQIwUsTiVx68S3RzWnzrCUVhHqvaxM",
|
"uid": "f0zxjm7ApxOafsn6DLQZ4Ezp78WRUsZqSc4taOSHq1gX",
|
||||||
"resourceVersion": "54321",
|
"resourceVersion": "54321",
|
||||||
"creationTimestamp": "1970-01-01T00:00:12Z",
|
"creationTimestamp": "1970-01-01T00:00:12Z",
|
||||||
"annotations": {
|
"annotations": {
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func GetAPIHandler(delegateHandler http.Handler, restConfig *restclient.Config,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
sub.HandleFunc(route.Path, route.Handler).
|
sub.HandleFunc("/"+route.Path, route.Handler).
|
||||||
Methods(methods...)
|
Methods(methods...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ func GetAPIHandler(delegateHandler http.Handler, restConfig *restclient.Config,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
sub.HandleFunc(route.Path, route.Handler).
|
sub.HandleFunc("/"+route.Path, route.Handler).
|
||||||
Methods(methods...)
|
Methods(methods...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,19 +4,34 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"strings"
|
"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"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create a stable UID that will be unique across a multi-tenant cluster
|
// 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 := sha256.New()
|
||||||
hasher.Write([]byte(obj.GetResourceVersion()))
|
hasher.Write([]byte(gvk.Group))
|
||||||
hasher.Write([]byte("|"))
|
hasher.Write([]byte("|"))
|
||||||
hasher.Write([]byte(obj.GetNamespace()))
|
hasher.Write([]byte(gvk.Kind))
|
||||||
hasher.Write([]byte("|"))
|
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))
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user