generate work name by hash
Signed-off-by: Garrybest <garrybest@foxmail.com>
This commit is contained in:
parent
58717e462e
commit
4924021f31
|
@ -0,0 +1,15 @@
|
|||
package hash
|
||||
|
||||
import (
|
||||
"hash"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
)
|
||||
|
||||
// DeepHashObject writes specified object to hash using the pretty library
|
||||
// which follows pointers and prints actual values of the nested objects
|
||||
// ensuring the hash does not change when a pointer changes.
|
||||
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
|
||||
hasher.Reset()
|
||||
pretty.Fprintf(hasher, "%# v", objectToWrite)
|
||||
}
|
|
@ -2,7 +2,12 @@ package names
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
|
||||
hashutil "github.com/karmada-io/karmada/pkg/util/hash"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -45,17 +50,22 @@ func GetClusterName(executionSpaceName string) (string, error) {
|
|||
return strings.TrimPrefix(executionSpaceName, executionSpacePrefix), nil
|
||||
}
|
||||
|
||||
// GenerateBindingName will generate binding name by namespace, kind and name
|
||||
// GenerateBindingName will generate binding name by kind and name
|
||||
func GenerateBindingName(kind, name string) string {
|
||||
return strings.ToLower(name + "-" + kind)
|
||||
}
|
||||
|
||||
// GenerateWorkName will generate work name by namespace, kind and name
|
||||
// GenerateWorkName will generate work name by its name and the hash of its namespace, kind and name.
|
||||
func GenerateWorkName(kind, name, namespace string) string {
|
||||
var workName string
|
||||
if len(namespace) == 0 {
|
||||
return strings.ToLower(name + "-" + kind)
|
||||
workName = strings.ToLower(name + "-" + kind)
|
||||
} else {
|
||||
workName = strings.ToLower(namespace + "-" + name + "-" + kind)
|
||||
}
|
||||
return strings.ToLower(namespace + "-" + name + "-" + kind)
|
||||
hash := fnv.New32a()
|
||||
hashutil.DeepHashObject(hash, workName)
|
||||
return fmt.Sprintf("%s-%s", name, rand.SafeEncodeString(fmt.Sprint(hash.Sum32())))
|
||||
}
|
||||
|
||||
// GenerateServiceAccountName generates the name of a ServiceAccount.
|
||||
|
|
Loading…
Reference in New Issue