forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
258 lines (234 loc) · 7.6 KB
/
template.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
package template
import (
"fmt"
"regexp"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/openshift/origin/pkg/template/api"
. "github.com/openshift/origin/pkg/template/generator"
"github.com/openshift/origin/pkg/util"
"github.com/openshift/origin/pkg/util/stringreplace"
)
var parameterExp = regexp.MustCompile(`\$\{([a-zA-Z0-9\_]+)\}`)
// Processor process the Template into the List with substituted parameters
type Processor struct {
Generators map[string]Generator
}
// NewProcessor creates new Processor and initializes its set of generators.
func NewProcessor(generators map[string]Generator) *Processor {
return &Processor{Generators: generators}
}
// Process transforms Template object into List object. It generates
// Parameter values using the defined set of generators first, and then it
// substitutes all Parameter expression occurrences with their corresponding
// values (currently in the containers' Environment variables only).
func (p *Processor) Process(template *api.Template) fielderrors.ValidationErrorList {
templateErrors := fielderrors.ValidationErrorList{}
if err := p.GenerateParameterValues(template); err != nil {
return append(templateErrors.Prefix("Template"), fielderrors.NewFieldInvalid("parameters", err, "failure to generate parameter value"))
}
for i, item := range template.Objects {
if obj, ok := item.(*runtime.Unknown); ok {
// TODO: use runtime.DecodeList when it returns ValidationErrorList
obj, err := runtime.UnstructuredJSONScheme.Decode(obj.RawJSON)
if err != nil {
util.ReportError(&templateErrors, i, *fielderrors.NewFieldInvalid("objects", err, "unable to handle object"))
continue
}
item = obj
}
newItem, err := p.SubstituteParameters(template.Parameters, item)
if err != nil {
util.ReportError(&templateErrors, i, *fielderrors.NewFieldNotSupported("parameters", err))
}
stripNamespace(newItem)
if err := addLabels(newItem, template.ObjectLabels); err != nil {
util.ReportError(&templateErrors, i, *fielderrors.NewFieldInvalid("labels", err, "label could not be applied"))
}
template.Objects[i] = newItem
}
return templateErrors
}
func stripNamespace(obj runtime.Object) {
// Remove namespace from the item
if itemMeta, err := meta.Accessor(obj); err == nil {
itemMeta.SetNamespace("")
return
}
// TODO: allow meta.Accessor to handle runtime.Unstructured
if unstruct, ok := obj.(*runtime.Unstructured); ok && unstruct.Object != nil {
if obj, ok := unstruct.Object["metadata"]; ok {
if m, ok := obj.(map[string]interface{}); ok {
if _, ok := m["namespace"]; ok {
m["namespace"] = ""
}
}
return
}
if _, ok := unstruct.Object["namespace"]; ok {
unstruct.Object["namespace"] = ""
return
}
}
}
func addLabels(obj runtime.Object, labels map[string]string) error {
if labels == nil {
return nil
}
// Remove namespace from the item
if meta, err := meta.Accessor(obj); err == nil {
existing := meta.Labels()
if existing == nil {
existing = make(map[string]string)
}
if err := util.MergeInto(existing, labels, util.OverwriteExistingDstKey); err != nil {
return err
}
meta.SetLabels(existing)
return nil
}
// TODO: allow meta.Accessor to handle runtime.Unstructured
if unstruct, ok := obj.(*runtime.Unstructured); ok && unstruct.Object != nil {
// the presence of "metadata" is sufficient for us to apply the rules for Kube-like
// objects.
// TODO: add swagger detection to allow this to happen more effectively
if obj, ok := unstruct.Object["metadata"]; ok {
if m, ok := obj.(map[string]interface{}); ok {
existing := make(map[string]string)
if l, ok := m["labels"]; ok {
if found, ok := extractLabels(l); ok {
existing = found
}
}
if err := util.MergeInto(existing, labels, util.OverwriteExistingDstKey); err != nil {
return err
}
m["labels"] = mapToGeneric(existing)
}
return nil
}
// only attempt to set root labels if a root object called labels exists
// TODO: add swagger detection to allow this to happen more effectively
if obj, ok := unstruct.Object["labels"]; ok {
existing := make(map[string]string)
if found, ok := extractLabels(obj); ok {
existing = found
}
if err := util.MergeInto(existing, labels, util.OverwriteExistingDstKey); err != nil {
return err
}
unstruct.Object["labels"] = mapToGeneric(existing)
return nil
}
}
return nil
}
// extractLabels extracts a map[string]string from a map[string]interface{}
func extractLabels(obj interface{}) (map[string]string, bool) {
lm, ok := obj.(map[string]interface{})
if !ok {
return nil, false
}
existing := make(map[string]string)
for k, v := range lm {
switch t := v.(type) {
case string:
existing[k] = t
}
}
return existing, true
}
// mapToGeneric converts a map[string]string into a map[string]interface{}
func mapToGeneric(obj map[string]string) map[string]interface{} {
if obj == nil {
return nil
}
res := make(map[string]interface{})
for k, v := range obj {
res[k] = v
}
return res
}
// AddParameter adds new custom parameter to the Template. It overrides
// the existing parameter, if already defined.
func AddParameter(t *api.Template, param api.Parameter) {
if existing := GetParameterByName(t, param.Name); existing != nil {
*existing = param
} else {
t.Parameters = append(t.Parameters, param)
}
}
// GetParameterByName searches for a Parameter in the Template
// based on its name.
func GetParameterByName(t *api.Template, name string) *api.Parameter {
for i, param := range t.Parameters {
if param.Name == name {
return &(t.Parameters[i])
}
}
return nil
}
// SubstituteParameters loops over all values defined in structured
// and unstructured types that are children of item.
//
// Example of Parameter expression:
// - ${PARAMETER_NAME}
//
func (p *Processor) SubstituteParameters(params []api.Parameter, item runtime.Object) (runtime.Object, error) {
// Make searching for given parameter name/value more effective
paramMap := make(map[string]string, len(params))
for _, param := range params {
paramMap[param.Name] = param.Value
}
stringreplace.VisitObjectStrings(item, func(in string) string {
for _, match := range parameterExp.FindAllStringSubmatch(in, -1) {
if len(match) > 1 {
if paramValue, found := paramMap[match[1]]; found {
in = strings.Replace(in, match[0], paramValue, 1)
}
}
}
return in
})
return item, nil
}
// GenerateParameterValues generates Value for each Parameter of the given
// Template that has Generate field specified where Value is not already
// supplied.
//
// Examples:
//
// from | value
// -----------------------------
// "test[0-9]{1}x" | "test7x"
// "[0-1]{8}" | "01001100"
// "0x[A-F0-9]{4}" | "0xB3AF"
// "[a-zA-Z0-9]{8}" | "hW4yQU5i"
func (p *Processor) GenerateParameterValues(t *api.Template) error {
for i := range t.Parameters {
param := &t.Parameters[i]
if len(param.Value) > 0 {
continue
}
if param.Generate != "" {
generator, ok := p.Generators[param.Generate]
if !ok {
return fmt.Errorf("template.parameters[%v]: Unable to find the '%v' generator", i, param.Generate)
}
if generator == nil {
return fmt.Errorf("template.parameters[%v]: Invalid '%v' generator", i, param.Generate)
}
value, err := generator.GenerateValue(param.From)
if err != nil {
return err
}
param.Value, ok = value.(string)
if !ok {
return fmt.Errorf("template.parameters[%v]: Unable to convert the generated value '%#v' to string", i, value)
}
}
}
return nil
}