forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workload_store.go
329 lines (296 loc) · 10.4 KB
/
workload_store.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package workload
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/docker/distribution/reference"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/api/customization/workload"
"github.com/rancher/rancher/pkg/clustermanager"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/apis/project.cattle.io/v3/schema"
projectschema "github.com/rancher/types/apis/project.cattle.io/v3/schema"
managementv3 "github.com/rancher/types/client/management/v3"
projectclient "github.com/rancher/types/client/project/v3"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)
func NewWorkloadAggregateStore(schemas *types.Schemas, manager *clustermanager.Manager) {
workloadSchema := schemas.Schema(&schema.Version, "workload")
store := NewAggregateStore(schemas.Schema(&schema.Version, "deployment"),
schemas.Schema(&schema.Version, "replicaSet"),
schemas.Schema(&schema.Version, "replicationController"),
schemas.Schema(&schema.Version, "daemonSet"),
schemas.Schema(&schema.Version, "statefulSet"),
schemas.Schema(&schema.Version, "job"),
schemas.Schema(&schema.Version, "cronJob"))
for _, s := range store.Schemas {
if s.ID == "deployment" {
s.Formatter = workload.DeploymentFormatter
} else {
s.Formatter = workload.Formatter
}
}
workloadSchema.Store = store
actionWrapper := workload.ActionWrapper{
ClusterManager: manager,
}
workloadSchema.ActionHandler = actionWrapper.ActionHandler
workloadSchema.LinkHandler = workload.Handler{}.LinkHandler
}
func NewCustomizeStore(store types.Store) types.Store {
return &CustomizeStore{
Store: NewTransformStore(store),
}
}
type CustomizeStore struct {
types.Store
}
func (s *CustomizeStore) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
setSelector(schema.ID, data)
setWorkloadSpecificDefaults(schema.ID, data)
setSecrets(apiContext, data)
if err := setPorts(convert.ToString(data["name"]), data); err != nil {
return nil, err
}
setScheduling(apiContext, data)
setStrategy(data)
return s.Store.Create(apiContext, schema, data)
}
func (s *CustomizeStore) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
splitted := strings.Split(id, ":")
if err := setPorts(splitted[1], data); err != nil {
return nil, err
}
setScheduling(apiContext, data)
setStrategy(data)
return s.Store.Update(apiContext, schema, data, id)
}
func (s *CustomizeStore) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
shortID := id
if strings.Count(id, ":") > 1 {
_, shortID = splitTypeAndID(id)
}
return s.Store.ByID(apiContext, schema, shortID)
}
func setScheduling(apiContext *types.APIContext, data map[string]interface{}) {
if nodeID := convert.ToString(values.GetValueN(data, "scheduling", "node", "nodeId")); nodeID != "" {
nodeName := getNodeName(apiContext, nodeID)
values.PutValue(data, nodeName, "scheduling", "node", "nodeId")
state := getState(data)
state[getKey(nodeName)] = nodeID
setState(data, state)
} else {
values.PutValue(data, "", "nodeId")
}
}
func setStrategy(data map[string]interface{}) {
strategy, ok := values.GetValue(data, "deploymentConfig", "strategy")
if ok && convert.ToString(strategy) == "Recreate" {
values.RemoveValue(data, "deploymentConfig", "maxSurge")
values.RemoveValue(data, "deploymentConfig", "maxUnavailable")
}
}
func setSelector(schemaID string, data map[string]interface{}) {
setSelector := false
isJob := strings.EqualFold(schemaID, "job") || strings.EqualFold(schemaID, "cronJob")
if convert.IsEmpty(data["selector"]) && !isJob {
setSelector = true
}
if setSelector {
workloadID := resolveWorkloadID(schemaID, data)
// set selector
data["selector"] = map[string]interface{}{
"matchLabels": map[string]interface{}{
SelectorLabel: workloadID,
},
}
// set workload labels
workloadLabels := convert.ToMapInterface(data["workloadLabels"])
if workloadLabels == nil {
workloadLabels = make(map[string]interface{})
}
workloadLabels[SelectorLabel] = workloadID
data["workloadLabels"] = workloadLabels
// set labels
labels := convert.ToMapInterface(data["labels"])
if labels == nil {
labels = make(map[string]interface{})
}
labels[SelectorLabel] = workloadID
data["labels"] = labels
}
}
func setSecrets(apiContext *types.APIContext, data map[string]interface{}) {
if val, _ := values.GetValue(data, "imagePullSecrets"); val != nil {
return
}
if containers, _ := values.GetSlice(data, "containers"); len(containers) > 0 {
imagePullSecrets, _ := data["imagePullSecrets"].([]corev1.LocalObjectReference)
domainToCreds := getCreds(apiContext, convert.ToString(data["namespaceId"]))
for _, container := range containers {
if image := convert.ToString(container["image"]); image != "" {
domain := getDomain(image)
if secrets, ok := domainToCreds[domain]; ok {
imagePullSecrets = append(imagePullSecrets, secrets...)
}
}
}
if imagePullSecrets != nil {
values.PutValue(data, imagePullSecrets, "imagePullSecrets")
}
}
}
func setWorkloadSpecificDefaults(schemaID string, data map[string]interface{}) {
if strings.EqualFold(schemaID, "job") || strings.EqualFold(schemaID, "cronJob") {
// job has different defaults
if _, ok := data["restartPolicy"]; !ok {
data["restartPolicy"] = "OnFailure"
}
}
}
func setPorts(workloadName string, data map[string]interface{}) error {
containers, ok := values.GetValue(data, "containers")
if !ok {
return nil
}
for _, c := range convert.ToInterfaceSlice(containers) {
cMap, err := convert.EncodeToMap(c)
if err != nil {
logrus.Warnf("Failed to transform container to map: %v", err)
continue
}
v, ok := values.GetValue(cMap, "ports")
if ok {
ports := convert.ToInterfaceSlice(v)
usedNames := map[string]bool{}
for _, p := range ports {
port, err := convert.EncodeToMap(p)
if err != nil {
logrus.Warnf("Failed to transform port to map %v", err)
continue
}
containerPort, err := convert.ToNumber(port["containerPort"])
if err != nil {
return httperror.NewAPIError(httperror.InvalidOption, fmt.Sprintf("failed to transform container port [%v] "+
"to number: %v", port["containerPort"], err))
}
containerPortStr := strconv.Itoa(int(containerPort))
protocol := strings.ToLower(convert.ToString(port["protocol"]))
sourcePort := strings.ToLower(convert.ToString(port["sourcePort"]))
portName := convert.ToString(port["name"])
if portName == "" || isRancherGeneratedPort(portName, containerPortStr, protocol) {
// port name is of format containerPortProtoSourcePortKind
// len limit is 15, therefore a) no separator b) kind is numerated
numKind := "0"
switch kind := convert.ToString(port["kind"]); kind {
case "NodePort":
numKind = "1"
case "ClusterIP":
numKind = "2"
case "LoadBalancer":
numKind = "3"
}
portName = fmt.Sprintf("%s%s%s%s",
containerPortStr,
protocol,
sourcePort,
numKind)
}
//validate port name
if _, ok := usedNames[portName]; ok {
return httperror.NewAPIError(httperror.InvalidOption, fmt.Sprintf("Duplicated port kind=%v,"+
" conainerPort=%v, protcol=%v", port["kind"], port["containerPort"], port["protocol"]))
}
usedNames[portName] = true
port["name"] = portName
if generateDNSName(workloadName, convert.ToString(port["dnsName"])) {
if port["kind"] == "ClusterIP" {
// use workload name for clusterIP service as it will be used by dns resolution
port["dnsName"] = strings.ToLower(convert.ToString(workloadName))
} else {
port["dnsName"] = fmt.Sprintf("%s-%s", strings.ToLower(convert.ToString(workloadName)),
strings.ToLower(convert.ToString(port["kind"])))
}
}
}
}
}
return nil
}
func isRancherGeneratedPort(portName, containerPort, protocol string) bool {
if strings.HasPrefix(portName, fmt.Sprintf("%s%s", containerPort, protocol)) {
return true
}
return false
}
func generateDNSName(workloadName, dnsName string) bool {
if dnsName == "" {
return true
}
// regenerate the name in case port type got changed
if strings.EqualFold(dnsName, workloadName) || strings.HasPrefix(dnsName, fmt.Sprintf("%s-", workloadName)) {
return true
}
return false
}
func getCreds(apiContext *types.APIContext, namespaceID string) map[string][]corev1.LocalObjectReference {
domainToCreds := make(map[string][]corev1.LocalObjectReference)
var namespacedCreds []projectclient.NamespacedDockerCredential
if err := access.List(apiContext, &projectschema.Version, "namespacedDockerCredential", &types.QueryOptions{}, &namespacedCreds); err == nil {
for _, cred := range namespacedCreds {
if cred.NamespaceId == namespaceID {
store(cred.Registries, domainToCreds, cred.Name)
}
}
}
var creds []projectclient.DockerCredential
if err := access.List(apiContext, &projectschema.Version, "dockerCredential", &types.QueryOptions{}, &creds); err == nil {
for _, cred := range creds {
store(cred.Registries, domainToCreds, cred.Name)
}
}
return domainToCreds
}
func getNodeName(apiContext *types.APIContext, nodeID string) string {
var node managementv3.Node
var nodeName string
if err := access.ByID(apiContext, &managementschema.Version, managementv3.NodeType, nodeID, &node); err == nil {
nodeName = node.NodeName
}
return nodeName
}
func setState(data map[string]interface{}, stateMap map[string]string) {
content, err := json.Marshal(stateMap)
if err != nil {
logrus.Errorf("failed to save state on workload: %v", data["id"])
return
}
values.PutValue(data, string(content), "annotations", "workload.cattle.io/state")
}
func getState(data map[string]interface{}) map[string]string {
state := map[string]string{}
v, ok := values.GetValue(data, "annotations", "workload.cattle.io/state")
if ok {
json.Unmarshal([]byte(convert.ToString(v)), &state)
}
return state
}
func getDomain(image string) string {
var repo string
named, err := reference.ParseNormalizedNamed(image)
if err != nil {
logrus.Debug(err)
return repo
}
domain := reference.Domain(named)
if domain == "docker.io" {
return "index.docker.io"
}
return domain
}