in controllers/utils.go [45:79]
func generateServiceName(namespace string, name string, portStr string, serviceNameTemplate string) string {
// The delimeter must be a character prohibited in kubernetes object names!
nameString := strings.Join([]string{namespace, name, portStr}, ";")
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(nameString)))[:8]
serviceTemplateTags := strings.Split(serviceNameTemplate, "-")
var fieldsToTruncate []string
for _, field := range serviceTemplateTags {
switch field {
case "{namespace}":
fieldsToTruncate = append(fieldsToTruncate, namespace)
case "{name}":
fieldsToTruncate = append(fieldsToTruncate, name)
case "{port}":
fieldsToTruncate = append(fieldsToTruncate, portStr)
}
}
// maxNameLength is the max length of service name without hyphens and hash (8). 63 - hashlength - numberOfHyphens
maxNegLengthWoHash := maxNameLength - strings.Count(serviceNameTemplate, "{hash}")*8 - strings.Count(serviceNameTemplate, "-")
truncFields := TrimFieldsEvenly(maxNegLengthWoHash, fieldsToTruncate...)
// form the final neg name string with hash
var fields []string
indexTruncFields := 0
for _, field := range serviceTemplateTags {
if field == "{hash}" {
fields = append(fields, hash)
} else {
fields = append(fields, truncFields[indexTruncFields])
indexTruncFields++
}
}
return strings.Join(fields, "-")
}