-
Notifications
You must be signed in to change notification settings - Fork 88
/
roles.go
140 lines (115 loc) · 3.41 KB
/
roles.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 embeddedcluster
import (
"context"
"fmt"
"regexp"
"sort"
"strings"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
)
const DEFAULT_CONTROLLER_ROLE_NAME = "controller"
var labelValueRegex = regexp.MustCompile(`[^a-zA-Z0-9-_.]+`)
// GetRoles will get a list of role names
func GetRoles(ctx context.Context) ([]string, error) {
config, err := ClusterConfig(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get cluster config: %w", err)
}
if config == nil {
// use the default nil spec
config = &embeddedclusterv1beta1.ConfigSpec{}
}
// determine role names
roles := []string{}
if config.Roles.Controller.Name != "" {
roles = append(roles, config.Roles.Controller.Name)
} else {
roles = append(roles, DEFAULT_CONTROLLER_ROLE_NAME)
}
for _, role := range config.Roles.Custom {
if role.Name != "" {
roles = append(roles, role.Name)
}
}
return roles, nil
}
// ControllerRoleName determines the name for the 'controller' role
// this might be part of the config, or it might be the default
func ControllerRoleName(ctx context.Context) (string, error) {
conf, err := ClusterConfig(ctx)
if err != nil {
return "", fmt.Errorf("failed to get cluster config: %w", err)
}
if conf != nil && conf.Roles.Controller.Name != "" {
return conf.Roles.Controller.Name, nil
}
return DEFAULT_CONTROLLER_ROLE_NAME, nil
}
// sort roles by name, but put controller first
func SortRoles(controllerRole string, inputRoles []string) []string {
roles := inputRoles
// determine if the controller role is present
hasController := false
controllerIdx := 0
for idx, role := range roles {
if role == controllerRole {
hasController = true
controllerIdx = idx
break
}
}
// if the controller role is present, remove it
if hasController {
roles = append(roles[:controllerIdx], roles[controllerIdx+1:]...)
}
// sort the roles
sort.Strings(roles)
// if the controller role was present, add it back to the front
if hasController {
roles = append([]string{controllerRole}, roles...)
}
return roles
}
// getRoleNodeLabels looks up roles in the cluster config and determines the additional labels to be applied from that
func getRoleNodeLabels(ctx context.Context, roles []string) ([]string, error) {
config, err := ClusterConfig(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get cluster config: %w", err)
}
return getRoleLabelsImpl(config, roles), nil
}
func labelify(s string) string {
// remove illegal characters
removechars := labelValueRegex.ReplaceAllString(s, "-")
// remove leading dashes
trimmed := strings.TrimPrefix(removechars, "-")
// restrict it to 63 characters
if len(trimmed) > 63 {
trimmed = trimmed[:63]
}
// remove trailing dashes
trimmed = strings.TrimSuffix(trimmed, "-")
return trimmed
}
func getRoleLabelsImpl(config *embeddedclusterv1beta1.ConfigSpec, roles []string) []string {
toReturn := []string{}
if config == nil {
return toReturn
}
for _, role := range roles {
if role == config.Roles.Controller.Name {
for k, v := range config.Roles.Controller.Labels {
toReturn = append(toReturn, fmt.Sprintf("%s=%s", labelify(k), labelify(v)))
}
}
for _, customRole := range config.Roles.Custom {
if role == customRole.Name {
for k, v := range customRole.Labels {
toReturn = append(toReturn, fmt.Sprintf("%s=%s", labelify(k), labelify(v)))
}
}
}
}
sort.Strings(toReturn)
return toReturn
}