-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.go
129 lines (106 loc) · 3 KB
/
context.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
package kubeconfig
import (
"fmt"
"regexp"
"strings"
)
const (
ContextPrefix = "gs-"
ClientCertSuffix = "-clientcert"
AWSIAMSuffix = "-awsiam"
)
type ContextType int
const (
ContextTypeNone ContextType = iota
ContextTypeMC
ContextTypeWC
)
var contextRegexp = regexp.MustCompile(fmt.Sprintf(`^%s([a-z0-9]+)(-[a-z0-9-]+)?$`, ContextPrefix))
// GenerateKubeContextName creates a context name,
// from an installation's code name.
func GenerateKubeContextName(installationCodeName string) string {
return fmt.Sprintf("%s%s", ContextPrefix, installationCodeName)
}
func GenerateWCKubeContextName(mcKubeContextName string, wcName string) string {
return fmt.Sprintf("%s-%s", mcKubeContextName, wcName)
}
func GenerateWCClientCertKubeContextName(mcKubeContextName string, wcName string) string {
return fmt.Sprintf("%s-%s%s", mcKubeContextName, wcName, ClientCertSuffix)
}
func GenerateWCAWSIAMKubeContextName(mcKubeContextName string, wcName string) string {
return fmt.Sprintf("%s-%s%s", mcKubeContextName, wcName, AWSIAMSuffix)
}
// IsKubeContext checks whether the name provided,
// matches our pattern for naming kubernetes contexts.
func IsKubeContext(s string) (bool, ContextType) {
contextType := GetKubeContextType(s)
return contextType != ContextTypeNone, contextType
}
// GetClientCertContextName returns the name of the client cert context name for an identifier
func GetClientCertContextName(identifier string) string {
if !strings.HasPrefix(identifier, ContextPrefix) {
identifier = ContextPrefix + identifier
}
if !strings.HasSuffix(identifier, ClientCertSuffix) {
identifier = identifier + ClientCertSuffix
}
return identifier
}
// GetCodeNameFromKubeContext gets an installation's
// code name, by knowing the context used to reference it.
func GetCodeNameFromKubeContext(c string) string {
parts := getKubeContextParts(c)
if len(parts) < 1 {
return c
}
return parts[0]
}
func GetClusterNameFromKubeContext(c string) string {
parts := getKubeContextParts(c)
if len(parts) < 2 {
return ""
}
return parts[1]
}
// IsCodeName checks whether a provided name is
// an installation's code name.
func IsCodeName(s string) bool {
codeNameRegExp, _ := regexp.Compile("^[a-z]+$")
return codeNameRegExp.MatchString(s)
}
// IsWCCodeName checks whether a provided name is
// a WC id with installation code name prefix.
func IsWCCodeName(s string) bool {
parts := strings.Split(s, "-")
if len(parts) != 2 {
return false
}
if !IsCodeName(parts[0]) {
return false
}
return true
}
func GetKubeContextType(s string) ContextType {
contextParts := getKubeContextParts(s)
switch len(contextParts) {
case 2:
return ContextTypeWC
case 1:
return ContextTypeMC
default:
return ContextTypeNone
}
}
func getKubeContextParts(s string) []string {
submatches := contextRegexp.FindStringSubmatch(s)
if len(submatches) < 1 {
return nil
}
var parts []string
for _, match := range submatches[1:] {
if len(match) > 0 {
parts = append(parts, strings.TrimPrefix(match, "-"))
}
}
return parts
}