-
Notifications
You must be signed in to change notification settings - Fork 684
/
funcs.go
51 lines (46 loc) · 1.18 KB
/
funcs.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
package main
import (
"fmt"
"strings"
"time"
)
const (
compromised = "compromised"
)
// ScheduleJobWithTrigger creates a long-running loop that runs a job after an initialDelay
// and then after each period duration.
// It returns a trigger function that runs the job early when called.
func ScheduleJobWithTrigger(initialDelay, period time.Duration, job func()) func() {
trigger := make(chan struct{})
go func() {
for {
<-trigger
job()
}
}()
go func() {
time.Sleep(initialDelay)
for {
trigger <- struct{}{}
time.Sleep(period)
}
}()
return func() {
trigger <- struct{}{}
}
}
const (
kubeChars = "abcdefghijklmnopqrstuvwxyz0123456789-" // Acceptable characters in k8s resource name
maxNameLength = 245 // Max resource name length is 253, leave some room for a suffix
)
func validateKeyPrefix(name string) (string, error) {
if len(name) > maxNameLength {
return "", fmt.Errorf("name is too long, must be shorter than %d, got %d", maxNameLength, len(name))
}
for _, char := range name {
if !strings.ContainsRune(kubeChars, char) {
return "", fmt.Errorf("name contains illegal character %c", char)
}
}
return name, nil
}