-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
44 lines (33 loc) · 890 Bytes
/
instance.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
package util
import (
"fmt"
"strings"
)
const (
podvmNamePrefix = "podvm"
)
func sanitize(input string) string {
var output string
for _, c := range strings.ToLower(input) {
if !(('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '-') {
c = '-'
}
output += string(c)
}
return output
}
func GenerateInstanceName(podName, sandboxID string, podvmNameMax int) string {
podName = sanitize(podName)
sandboxID = sanitize(sandboxID)
prefixLen := len(podvmNamePrefix)
podNameLen := len(podName)
if podvmNameMax > 0 && prefixLen+podNameLen+10 > podvmNameMax {
podNameLen = podvmNameMax - prefixLen - 10
if podNameLen < 0 {
panic(fmt.Errorf("podvmNameMax is too small: %d", podvmNameMax))
}
fmt.Printf("podNameLen: %d", podNameLen)
}
instanceName := fmt.Sprintf("%s-%.*s-%.8s", podvmNamePrefix, podNameLen, podName, sandboxID)
return instanceName
}