The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/services/apiserver/utils/uids.go

37 lines
1015 B

package utils
import (
"crypto/sha256"
"encoding/base64"
"strings"
"unicode"
"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
// 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(gvk.Group))
hasher.Write([]byte("|"))
hasher.Write([]byte(gvk.Kind))
hasher.Write([]byte("|"))
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.Map(func(r rune) rune {
if !(unicode.IsLetter(r) || unicode.IsDigit(r)) {
return 'X'
}
return r
}, v))
}