-
Notifications
You must be signed in to change notification settings - Fork 787
/
names.go
140 lines (126 loc) · 3.46 KB
/
names.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package naming
import (
"bytes"
"fmt"
"math"
"strings"
"unicode"
"github.com/google/uuid"
)
// ToValidImageName converts the given string into a valid docker image name
func ToValidImageName(name string) string {
return strings.ToLower(name)
}
// ToValidImageVersion converts the given string into a valid docker image version string
func ToValidImageVersion(version string) string {
return strings.Replace(version, ":", "", -1)
}
// ToValidName converts the given string into a valid Kubernetes resource name
func ToValidName(name string) string {
return toValidName(name, false, math.MaxInt32)
}
// ToValidNameWithDots converts the given string into a valid Kubernetes resource name
func ToValidNameWithDots(name string) string {
return toValidName(name, true, math.MaxInt32)
}
// ToValidNameTruncated converts the given string into a valid Kubernetes resource name,
// truncating the result if it is more than maxLength characters.
func ToValidNameTruncated(name string, maxLength int) string {
return toValidName(name, false, maxLength)
}
// ToValidNameWithDotsTruncated converts the given string into a valid Kubernetes resource name,
// truncating the result if it is more than maxLength characters.
func ToValidNameWithDotsTruncated(name string, maxLength int) string {
return toValidName(name, true, maxLength)
}
// ToValidGCPServiceAccount converts the given string into a valid GCP service account name,
// truncating the result if it is more than 30 characters.
func ToValidGCPServiceAccount(name string) string {
if len(name) < 6 {
uuid4 := uuid.New()
name = fmt.Sprintf("%s-jx-%s", name, uuid4.String()[:1])
}
return toValidName(name, false, 30)
}
func toValidName(name string, allowDots bool, maxLength int) string {
if name == "" {
return ""
}
var buffer bytes.Buffer
first := true
lastCharDash := false
hasLetter := false
for _, ch := range name {
ch = unicode.ToLower(ch)
if ch >= 'a' && ch <= 'z' {
hasLetter = true
break
}
}
if !hasLetter {
name = fmt.Sprintf("x%s", name)
}
for _, ch := range name {
ch = unicode.ToLower(ch)
if buffer.Len()+1 > maxLength {
break
}
if first {
// strip non letters at start
if ch >= 'a' && ch <= 'z' {
buffer.WriteRune(ch)
first = false
}
} else {
if !allowDots && ch == '.' {
ch = '-'
}
if !(ch >= 'a' && ch <= 'z') && !(ch >= '0' && ch <= '9') && ch != '-' && ch != '.' {
ch = '-'
}
if ch != '-' || !lastCharDash {
buffer.WriteRune(ch)
}
lastCharDash = ch == '-'
}
}
answer := buffer.String()
for {
if strings.HasSuffix(answer, "-") {
answer = strings.TrimSuffix(answer, "-")
} else {
break
}
}
return answer
}
// ToValidValue validates a label value which can start with numbers
func ToValidValue(name string) string {
if name == "" {
return ""
}
var buffer bytes.Buffer
lastCharDash := false
for _, ch := range name {
if !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && !(ch >= '0' && ch <= '9') && ch != '-' && ch != '.' && ch != '/' {
ch = '-'
}
if ch != '-' || !lastCharDash {
buffer.WriteRune(ch)
}
lastCharDash = ch == '-'
}
answer := buffer.String()
for {
if strings.HasSuffix(answer, "-") {
answer = strings.TrimSuffix(answer, "-")
} else {
break
}
}
return answer
}
//EmailToK8sID converts the provided email address to a valid Kubernetes resource name, converting the @ to a .
func EmailToK8sID(email string) string {
return ToValidNameWithDots(strings.Replace(email, "@", ".", -1))
}