@@ -22,49 +22,12 @@ import (
2222 "k8s.io/apimachinery/pkg/util/validation"
2323)
2424
25- // GenerateDNS1123Label generates a valid DNS label (compliant with RFC 1123).
26- // The result is usually combined by the base and uniqueName, such as "base-uniqueName".
27- // If the generated name is too long, the suffix of base will be truncated to ensure the
28- // final name is shorter than 63 characters.
29- //
30- // Usually:
31- // - base is the name of workload, such as "deployment", "statefulset", "daemonset".
32- // - uniqueName is a random string, such as "12345" or ordinal index.
33- func GenerateDNS1123Label (base , unique string ) string {
34- return genereateDNS1123LabelByMaxLength (base , unique , validation .DNS1123LabelMaxLength )
35- }
36-
37- // GenerateDNS1123LabelByMaxLength generates a valid DNS label (compliant with RFC 1123)
38- // limited by the specified maximum length.
39- func GenereateDNS1123LabelByMaxLength (base , unique string , maxLength int ) string {
40- return genereateDNS1123LabelByMaxLength (base , unique , maxLength )
41- }
42-
43- func genereateDNS1123LabelByMaxLength (base , unique string , maxLength int ) string {
44- return genericNameGenerator (base , unique , maxLength , validation .DNS1123LabelMaxLength , fixDNS1123Label )
45- }
46-
47- // GenerateDNS1123Subdomain generates a valid DNS subdomain (compliant with RFC 1123).
48- // The result is usually combined by the base and uniqueName, such as "base-uniqueName".
49- // If the generated name is too long, the suffix of base will be truncated to ensure the
50- // final name is shorter than 253 characters.
51- //
52- // Usually:
53- // - base is the name of workload, such as "deployment", "statefulset", "daemonset".
54- // - uniqueName is a random string, such as "12345" or ordinal index.
55- func GenerateDNS1123Subdomain (base , unique string ) string {
56- return generateDNS1123SubdomainByMaxLength (base , unique , validation .DNS1123SubdomainMaxLength )
57- }
58-
59- // GenerateDNS1123SubdomainByMaxLength generates a valid DNS subdomain (compliant with RFC 1123)
60- // limited by the specified maximum length.
61- func GenerateDNS1123SubdomainByMaxLength (base , unique string , maxLength int ) string {
62- return generateDNS1123SubdomainByMaxLength (base , unique , maxLength )
63- }
64-
65- func generateDNS1123SubdomainByMaxLength (base , unique string , maxLength int ) string {
66- return genericNameGenerator (base , unique , maxLength , validation .DNS1123SubdomainMaxLength , fixDNS1123Subdomain )
67- }
25+ var (
26+ // DNS1123LabelGenerator generates a valid DNS label (compliant with RFC 1123).
27+ DNS1123LabelGenerator = newNameGenerator (validation .DNS1123LabelMaxLength , fixDNS1123Label )
28+ // DNS1123SubdomainGenerator generates a valid DNS subdomain (compliant with RFC 1123).
29+ DNS1123SubdomainGenerator = newNameGenerator (validation .DNS1123SubdomainMaxLength , fixDNS1123Subdomain )
30+ )
6831
6932func fixDNS1123Label (label string ) string {
7033 // Convert to lowercase
@@ -76,7 +39,6 @@ func fixDNS1123Label(label string) string {
7639 // Process each character in the label
7740 for i := 0 ; i < len (label ); i ++ {
7841 c := label [i ]
79-
8042 if firstChar {
8143 // First character must be alphanumeric
8244 if (c >= 'a' && c <= 'z' ) || (c >= '0' && c <= '9' ) {
@@ -86,7 +48,6 @@ func fixDNS1123Label(label string) string {
8648 // Skip non-alphanumeric characters at the beginning
8749 continue
8850 }
89-
9051 // Subsequent characters: allow alphanumeric and dash
9152 if (c >= 'a' && c <= 'z' ) || (c >= '0' && c <= '9' ) || c == '-' {
9253 builder .WriteByte (c )
@@ -97,7 +58,6 @@ func fixDNS1123Label(label string) string {
9758 }
9859
9960 result := builder .String ()
100-
10161 return strings .TrimRight (result , "-" )
10262}
10363
0 commit comments