in pkg/common/uniquename/uniquename.go [59:119]
func ClusterScopedUniqueName(format Format, namespace, name string) (string, error) {
reservedSlots := 2 + uuidLength // 2 dashes + 5 character UUID string
switch format {
case DNS1123Subdomain:
availableSlots := validation.DNS1123SubdomainMaxLength // 253 characters
slotsPerSeg := (availableSlots - reservedSlots) / 2
uniqueName := fmt.Sprintf("%s-%s-%s",
namespace[:minInt(slotsPerSeg, len(namespace))],
name[:minInt(slotsPerSeg, len(name))],
uuid.NewUUID()[:uuidLength],
)
if errs := validation.IsDNS1123Subdomain(uniqueName); len(errs) != 0 {
return "", fmt.Errorf("failed to format a unique RFC 1123 DNS subdomain name with namespace %s, name %s: %v", namespace, name, errs)
}
return uniqueName, nil
case DNS1123Label:
availableSlots := validation.DNS1123LabelMaxLength // 63 characters
slotsPerSeg := (availableSlots - reservedSlots) / 2
// If the object name is a RFC 1123 DNS subdomain, it may include dot characters, which is not allowed in
// RFC 1123 DNS labels.
name = removeDots(name)
uniqueName := fmt.Sprintf("%s-%s-%s",
namespace[:minInt(slotsPerSeg, len(namespace))],
name[:minInt(slotsPerSeg, len(name))],
uuid.NewUUID()[:uuidLength],
)
if errs := validation.IsDNS1123Label(uniqueName); len(errs) != 0 {
return "", fmt.Errorf("failed to format a unique RFC 1123 DNS label name with namespace %s, name %s: %v", namespace, name, errs)
}
return uniqueName, nil
case DNS1035Label:
availableSlots := validation.DNS1035LabelMaxLength // 63 characters
slotsPerSeg := (availableSlots - reservedSlots) / 2
// Namespace names are RFC 1123 DNS labels, which may start with an alphanumeric character; RFC 1035 DNS
// labels, on the other hand, does not allow numeric characters at the beginning of the string.
if startsWithNumericCharacter(namespace) {
namespace = "ns" + namespace
}
// If the object name is a RFC 1123 DNS subdomain, it may include dot characters, which is not allowed in
// RFC 1035 DNS labels.
name = removeDots(name)
uniqueName := fmt.Sprintf("%s-%s-%s",
namespace[:minInt(slotsPerSeg, len(namespace))],
name[:minInt(slotsPerSeg, len(name))],
uuid.NewUUID()[:uuidLength],
)
if errs := validation.IsDNS1035Label(uniqueName); len(errs) != 0 {
return "", fmt.Errorf("failed to format a unique RFC 1035 DNS label name with namespace %s, name %s: %v", namespace, name, errs)
}
return uniqueName, nil
}
return "", fmt.Errorf("not a valid name format: %d", format)
}