/
names.go
43 lines (35 loc) · 1.17 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
package backup
import (
api "github.com/percona/percona-xtradb-cluster-operator/pkg/apis/pxc/v1"
)
// GenName63 generates legit name for backup resources.
// k8s sets the `job-name` label for the created by job pod.
// So we have to be sure that job name won't be longer than 63 symbols.
// Yet the job name has to have some meaningful name which won't be conflicting with other jobs' names.
func GenName63(cr *api.PerconaXtraDBClusterBackup) string {
postfix := cr.Name
maxNameLen := 16
typ, ok := cr.GetLabels()["type"]
// in case it's not a cron-job we're not sure if the name fits rules
// but there is more room for names
if !ok || typ != "cron" {
maxNameLen = 29
postfix = trimNameRight(postfix, maxNameLen)
}
name := "xb-" + postfix
return name
}
// trimNameRight if needed cut off symbol by symbol from the name right side
// until it satisfy requirements to end with an alphanumeric character and have a length no more than ln
func trimNameRight(name string, ln int) string {
if len(name) <= ln {
ln = len(name)
}
for ; ln > 0; ln-- {
if name[ln-1] >= 'a' && name[ln-1] <= 'z' ||
name[ln-1] >= '0' && name[ln-1] <= '9' {
break
}
}
return name[:ln]
}