func readableResourceName()

in csmnamer/namer.go [56:91]


func readableResourceName(components ...string) string {
	// clusterHash enforces uniqueness of resources of different clusters in
	// the same project.
	clusterHash := Hash(strings.Join(components, ";"), clusterUIDLen)
	prefix := csmMeshPrefix + "-" + clusterHash
	// resourceHash enforces uniqueness of resources of the same cluster.
	resourceHash := Hash(strings.Join(components, ";"), nHashLen)
	// Ideally we explicitly include all components in the GCP resource name, so
	// it's easier to be related to the corresponding k8s resource(s). However,
	// only certain characters are allowed in a GCP resource name(e.g. a common
	// character '.' in hostnames is not allowed in GCP resource name).
	var explicitComponents []string
	for _, c := range components {
		// Only explicitly include a component in GCP resource name if all
		// characters in it are allowed. Omitting a component here is okay since
		// the resourceHash already represents the full component set.
		if allCharAllowedInResourceName(c) {
			explicitComponents = append(explicitComponents, c)
		}
	}
	// The maximum total length of components is determined by subtracting length
	// of the following substring from the maximum length of resource name:
	// * prefix
	// * separators "-". There will be len(explicitComponents) + 1 of them.
	// * hash
	componentsMaxLen := resourceNameMaxLen - len(prefix) - (len(explicitComponents) + 1) - len(resourceHash)
	// Drop components from the resource name if the allowed maximum total length
	// of them is less them the total number of components. (This happens when
	// there are too many components)
	if componentsMaxLen < len(explicitComponents) {
		return fmt.Sprintf("%s-%s", prefix, resourceHash)
	}
	// Trim components to fit the allowed maximum total length.
	trimmed := TrimFieldsEvenly(componentsMaxLen, explicitComponents...)
	return fmt.Sprintf("%s-%s-%s", prefix, strings.Join(trimmed, "-"), resourceHash)
}