-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinject.go
146 lines (109 loc) · 4.18 KB
/
inject.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
package main
import (
"crypto/md5"
"encoding/hex"
)
const DEFAULT_REVISION_HISTORY_LIMIT = 3
type InjectContext struct {
Objects map[string]map[string]RenderContextEnvAwareObject
Env string
Branch string
Namespace string
TagVersion string
}
func InjectMetadata(injectContext InjectContext, objects []map[string]interface{}) []map[string]interface{} {
for _, object := range objects {
kind := object["kind"].(string)
var metadata = object["metadata"].(map[interface{}]interface{})
name := metadata["name"].(string)
envAwareObjectName := injectContext.Objects[kind][name].Name
switch kind {
case K8S_SERVICE:
injectMetaDataIntoService(envAwareObjectName, injectContext, object)
break
case K8S_DEPLOYMENT:
injectMetaDataIntoDeployment(envAwareObjectName, injectContext, object)
break
default:
injectMetaDataIntoObject(envAwareObjectName, injectContext, object)
break
}
}
return objects
}
func injectMetaDataIntoService(envAwareObjectName string, injectContext InjectContext, object map[string]interface{}) map[string]interface{} {
object = inject(envAwareObjectName, injectContext, object)
var spec = object["spec"].(map[interface{}]interface{})
var specSelector = spec["selector"].(map[interface{}]interface{})
specSelector["env"] = injectContext.Env
spec["selector"] = specSelector
object["spec"] = spec
return object
}
func injectMetaDataIntoDeployment(envAwareObjectName string, injectContext InjectContext, object map[string]interface{}) map[string]interface{} {
object = inject(envAwareObjectName, injectContext, object)
/*
* .spec.selector is an optional field that specifies a label selector for
* the Pods targeted by this deployment.
*
* If specified, .spec.selector must match .spec.template.metadata.labels,
* or it will be rejected by the API. If .spec.selector is unspecified,
* .spec.selector.matchLabels will be defaulted to .spec.template.metadata.labels.
*
* http://kubernetes.io/docs/user-guide/deployments/#selector
*/
spec := object["spec"].(map[interface{}]interface{})
specTemplate := spec["template"].(map[interface{}]interface{})
specTemplateMetadata := specTemplate["metadata"].(map[interface{}]interface{})
var specTemplateMetadataLabels map[interface{}]interface{}
if specTemplateMetadata["labels"].(map[interface{}]interface{}) == nil {
specTemplateMetadataLabels = map[interface{}]interface{}{
"env": injectContext.Env,
}
} else {
specTemplateMetadataLabels = specTemplateMetadata["labels"].(map[interface{}]interface{})
specTemplateMetadataLabels["env"] = injectContext.Env
}
if spec["selector"] == nil {
spec["selector"] = map[interface{}]interface{}{
"matchLabels": specTemplateMetadataLabels,
}
}
specSelector := spec["selector"].(map[interface{}]interface{})
/*
* Limit replica set history if not set.
*/
if spec["revisionHistoryLimit"] == nil {
spec["revisionHistoryLimit"] = DEFAULT_REVISION_HISTORY_LIMIT
}
specSelectorMatchLabels := specSelector["matchLabels"].(map[interface{}]interface{})
specSelectorMatchLabels["env"] = injectContext.Env
specSelector["matchLabels"] = specSelectorMatchLabels
spec["selector"] = specSelector
object["spec"] = spec
return object
}
func injectMetaDataIntoObject(envAwareObjectName string, injectContext InjectContext, object map[string]interface{}) map[string]interface{} {
return inject(envAwareObjectName, injectContext, object)
}
func inject(envAwareObjectName string, injectContext InjectContext, object map[string]interface{}) map[string]interface{} {
var metadata = object["metadata"].(map[interface{}]interface{})
metadata["name"] = envAwareObjectName
metadata["namespace"] = injectContext.Namespace
if metadata["labels"] == nil {
metadata["labels"] = map[interface{}]interface{}{}
}
var labels = metadata["labels"].(map[interface{}]interface{})
labels["env"] = injectContext.Env
labels["branch"] = injectContext.Branch
labels["branch_hash"] = MD5(injectContext.Branch)
labels["version"] = injectContext.TagVersion
metadata["labels"] = labels
object["metadata"] = metadata
return object
}
func MD5(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}