/
extra_annotations_and_labels_post_renderer.go
387 lines (319 loc) · 11.6 KB
/
extra_annotations_and_labels_post_renderer.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package helm
import (
"bytes"
"context"
"fmt"
"os"
"reflect"
"sort"
"strings"
yaml_v3 "gopkg.in/yaml.v3"
"helm.sh/helm/v3/pkg/releaseutil"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/werf"
"github.com/werf/werf/pkg/werf/global_warnings"
)
var WerfRuntimeAnnotations = map[string]string{
"werf.io/version": werf.Version,
}
var WerfRuntimeLabels = map[string]string{}
func NewExtraAnnotationsAndLabelsPostRenderer(extraAnnotations, extraLabels map[string]string, ignoreInvalidAnnotationsAndLabels bool) *ExtraAnnotationsAndLabelsPostRenderer {
return &ExtraAnnotationsAndLabelsPostRenderer{
ExtraAnnotations: extraAnnotations,
ExtraLabels: extraLabels,
IgnoreInvalidAnnotationsAndLabels: ignoreInvalidAnnotationsAndLabels,
globalWarnings: &defaultGlobalWarnings{},
}
}
type ExtraAnnotationsAndLabelsPostRenderer struct {
ExtraAnnotations map[string]string
ExtraLabels map[string]string
IgnoreInvalidAnnotationsAndLabels bool
globalWarnings globalWarnings
}
type defaultGlobalWarnings struct{}
func (gw *defaultGlobalWarnings) GlobalWarningLn(ctx context.Context, msg string) {
global_warnings.GlobalWarningLn(ctx, msg)
}
type globalWarnings interface {
GlobalWarningLn(ctx context.Context, msg string)
}
func replaceNodeByKey(node *yaml_v3.Node, key string, value *yaml_v3.Node) {
content := node.Content
end := len(content)
for pos := 0; pos < end; pos += 2 {
keyNode := content[pos]
if keyNode.Tag != "!!str" {
continue
}
var k string
if err := keyNode.Decode(&k); err != nil {
continue
}
if k == key {
content[pos+1] = value
return
}
}
}
func findNodeByKey(node *yaml_v3.Node, key string) *yaml_v3.Node {
content := node.Content
end := len(content)
for pos := 0; pos < end; pos += 2 {
keyNode := content[pos]
valueNode := content[pos+1]
if keyNode.Tag != "!!str" {
continue
}
var k string
if err := keyNode.Decode(&k); err != nil {
continue
}
if k == key {
return valueNode
}
}
return nil
}
func dereferenceAliasNode(node *yaml_v3.Node) (*yaml_v3.Node, error) {
dereferencedNode, err := createNode(map[string]interface{}{})
if err != nil {
return nil, err
}
dereferencedNode.Content = append(dereferencedNode.Content, node.Alias.Content...)
return dereferencedNode, nil
}
func getMapNode(docNode *yaml_v3.Node) *yaml_v3.Node {
if docNode.Kind == yaml_v3.DocumentNode {
if len(docNode.Content) > 0 {
n := docNode.Content[0]
if n.Tag == "!!map" {
return n
}
}
}
return nil
}
func createNode(v interface{}) (*yaml_v3.Node, error) {
newNode := &yaml_v3.Node{}
if err := newNode.Encode(v); err != nil {
return nil, fmt.Errorf("unable to encode value %#v into yaml node: %w", v, err)
}
return newNode, nil
}
func appendToNode(node *yaml_v3.Node, data interface{}) (*yaml_v3.Node, error) {
newNode, err := createNode(data)
if err != nil {
return nil, err
}
node.Content = append(node.Content, newNode.Content...)
return node, nil
}
func validateStringNode(node *yaml_v3.Node) error {
var v interface{}
if err := node.Decode(&v); err != nil {
return fmt.Errorf("unable to decode value %q: %w", node.Value, err)
}
if _, ok := v.(string); !ok {
typeOf := reflect.TypeOf(v)
if typeOf != nil {
return fmt.Errorf("invalid node %q: expected string, got %s", node.Value, reflect.TypeOf(v).String())
} else {
return fmt.Errorf("invalid node %q: expected string, got null value", node.Value)
}
}
return nil
}
func validateKeyAndValue(keyNode, valueNode *yaml_v3.Node) (errors []error) {
keyErr := validateStringNode(keyNode)
if keyErr != nil {
errors = append(errors, keyErr)
}
var valueErr error
if valueNode.Kind == yaml_v3.AliasNode {
valueErr = validateStringNode(valueNode.Alias)
} else {
valueErr = validateStringNode(valueNode)
}
if valueErr != nil {
errors = append(errors, valueErr)
}
return
}
func validateMapStringStringNode(node *yaml_v3.Node) ([]*yaml_v3.Node, []error) {
content := node.Content
end := len(content)
var validValues []*yaml_v3.Node
var errors []error
for pos := 0; pos < end; pos += 2 {
keyNode := content[pos]
valueNode := content[pos+1]
if keyNode.Tag == "!!merge" && valueNode.Kind == yaml_v3.AliasNode {
for pos := 0; pos < len(valueNode.Alias.Content); pos += 2 {
keyNode := valueNode.Alias.Content[pos]
valueNode := valueNode.Alias.Content[pos+1]
newErrs := validateKeyAndValue(keyNode, valueNode)
if len(newErrs) > 0 {
errors = append(errors, newErrs...)
} else {
validValues = append(validValues, keyNode, valueNode)
}
}
continue
}
newErrs := validateKeyAndValue(keyNode, valueNode)
if len(newErrs) > 0 {
errors = append(errors, newErrs...)
} else {
validValues = append(validValues, keyNode, valueNode)
}
}
return validValues, errors
}
func appendExtraData(node *yaml_v3.Node, key string, data interface{}) error {
if targetNode := findNodeByKey(node, key); targetNode != nil {
if targetNode.Kind == yaml_v3.AliasNode {
dereferencedTargetNode, err := dereferenceAliasNode(targetNode)
if err != nil {
return err
}
appendToNode(dereferencedTargetNode, data)
replaceNodeByKey(node, key, dereferencedTargetNode)
} else {
appendToNode(targetNode, data)
}
if targetNode.Kind != yaml_v3.AliasNode && targetNode.Kind != yaml_v3.MappingNode {
targetNode.Kind = yaml_v3.MappingNode
}
targetNode.Tag = "!!map"
} else {
appendToNode(node, map[string]interface{}{key: data})
}
return nil
}
func (pr *ExtraAnnotationsAndLabelsPostRenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
extraAnnotations := map[string]string{}
for k, v := range WerfRuntimeAnnotations {
extraAnnotations[k] = v
}
for k, v := range pr.ExtraAnnotations {
extraAnnotations[k] = v
}
extraLabels := map[string]string{}
for k, v := range WerfRuntimeLabels {
extraLabels[k] = v
}
for k, v := range pr.ExtraLabels {
extraLabels[k] = v
}
splitManifestsByKeys := releaseutil.SplitManifests(renderedManifests.String())
manifestsKeys := make([]string, 0, len(splitManifestsByKeys))
for k := range splitManifestsByKeys {
manifestsKeys = append(manifestsKeys, k)
}
sort.Sort(releaseutil.BySplitManifestsOrder(manifestsKeys))
splitModifiedManifests := make([]string, 0)
for _, manifestKey := range manifestsKeys {
manifestContent := splitManifestsByKeys[manifestKey]
if os.Getenv("WERF_HELM_V3_EXTRA_ANNOTATIONS_AND_LABELS_DEBUG") == "1" {
fmt.Printf("ExtraAnnotationsAndLabelsPostRenderer -- original manifest BEGIN\n")
fmt.Printf("%s\n", manifestContent)
fmt.Printf("ExtraAnnotationsAndLabelsPostRenderer -- original manifest END\n")
}
var obj unstructured.Unstructured
if err := yaml.Unmarshal([]byte(manifestContent), &obj); err != nil {
logboek.Warn().LogF("Unable to decode yaml manifest as unstructured object: %s: will not add extra annotations and labels to this object:\n%s\n---\n", err, manifestContent)
splitModifiedManifests = append(splitModifiedManifests, manifestContent)
continue
}
if obj.GetKind() == "" {
logboek.Debug().LogF("Skipping empty object\n")
continue
}
var objNode yaml_v3.Node
if err := yaml_v3.Unmarshal([]byte(manifestContent), &objNode); err != nil {
logboek.Warn().LogF("Unable to decode yaml manifest as map slice: %s: will not add extra annotations and labels to this object:\n%s\n---\n", err, manifestContent)
splitModifiedManifests = append(splitModifiedManifests, manifestContent)
continue
}
if os.Getenv("WERF_HELM_V3_EXTRA_ANNOTATIONS_AND_LABELS_DEBUG") == "1" {
fmt.Printf("Unpacket obj annotations: %#v\n", obj.GetAnnotations())
}
if objMapNode := getMapNode(&objNode); objMapNode != nil {
if metadataNode := findNodeByKey(objMapNode, "metadata"); metadataNode != nil {
if obj.IsList() && len(extraAnnotations) > 0 {
pr.globalWarnings.GlobalWarningLn(context.Background(), fmt.Sprintf("werf annotations won't be applied to *List resource Kinds, including %s. We advise to replace *List resources with multiple separate resources of the same Kind", obj.GetKind()))
} else if len(extraAnnotations) > 0 {
if err := appendExtraData(metadataNode, "annotations", extraAnnotations); err != nil {
pr.globalWarnings.GlobalWarningLn(context.Background(), fmt.Sprintf("werf annotations won't be applied to the %s/%s: an error have occurred during annotations injection: %s\n", strings.ToLower(obj.GetKind()), obj.GetName(), err))
}
}
if obj.IsList() && len(extraLabels) > 0 {
pr.globalWarnings.GlobalWarningLn(context.Background(), fmt.Sprintf("werf labels won't be applied to *List resource Kinds, including %s. We advise to replace *List resources with multiple separate resources of the same Kind", obj.GetKind()))
} else if len(extraLabels) > 0 {
if err := appendExtraData(metadataNode, "labels", extraLabels); err != nil {
pr.globalWarnings.GlobalWarningLn(context.Background(), fmt.Sprintf("werf labels won't be applied to the %s/%s: an error have occurred during labels injection: %s\n", strings.ToLower(obj.GetKind()), obj.GetName(), err))
}
}
if annotationsNode := findNodeByKey(metadataNode, "annotations"); annotationsNode != nil {
validNodes, errors := validateMapStringStringNode(annotationsNode)
for _, err := range errors {
pr.globalWarnings.GlobalWarningLn(context.Background(), fmt.Sprintf("%s/%s annotations validation: %s", strings.ToLower(obj.GetKind()), obj.GetName(), err.Error()))
}
if pr.IgnoreInvalidAnnotationsAndLabels {
annotationsNode.Content = validNodes
}
}
if labelsNode := findNodeByKey(metadataNode, "labels"); labelsNode != nil {
validNodes, errors := validateMapStringStringNode(labelsNode)
for _, err := range errors {
pr.globalWarnings.GlobalWarningLn(context.Background(), fmt.Sprintf("%s/%s labels validation: %s\n", strings.ToLower(obj.GetKind()), obj.GetName(), err.Error()))
}
if pr.IgnoreInvalidAnnotationsAndLabels {
labelsNode.Content = validNodes
}
}
}
}
var modifiedManifestContent bytes.Buffer
yamlEncoder := yaml_v3.NewEncoder(&modifiedManifestContent)
yamlEncoder.SetIndent(2)
if err := yamlEncoder.Encode(&objNode); err != nil {
return nil, fmt.Errorf("unable to modify manifest: %w\n%s\n---\n", err, manifestContent)
} else {
splitModifiedManifests = append(splitModifiedManifests, modifiedManifestContent.String())
if os.Getenv("WERF_HELM_V3_EXTRA_ANNOTATIONS_AND_LABELS_DEBUG") == "1" {
fmt.Printf("ExtraAnnotationsAndLabelsPostRenderer -- modified manifest BEGIN\n")
fmt.Printf("%s", modifiedManifestContent.String())
fmt.Printf("ExtraAnnotationsAndLabelsPostRenderer -- modified manifest END\n")
}
}
}
modifiedManifests := bytes.NewBufferString(strings.Join(splitModifiedManifests, "---\n"))
if os.Getenv("WERF_HELM_V3_EXTRA_ANNOTATIONS_AND_LABELS_DEBUG") == "1" {
fmt.Printf("ExtraAnnotationsAndLabelsPostRenderer -- modified manifests RESULT BEGIN\n")
fmt.Printf("%s\n", modifiedManifests.String())
fmt.Printf("ExtraAnnotationsAndLabelsPostRenderer -- modified manifests RESULT END\n")
}
return modifiedManifests, nil
}
func (pr *ExtraAnnotationsAndLabelsPostRenderer) Add(extraAnnotations, extraLabels map[string]string) {
if len(extraAnnotations) > 0 {
if pr.ExtraAnnotations == nil {
pr.ExtraAnnotations = make(map[string]string)
}
for k, v := range extraAnnotations {
pr.ExtraAnnotations[k] = v
}
}
if len(extraLabels) > 0 {
if pr.ExtraLabels == nil {
pr.ExtraLabels = make(map[string]string)
}
for k, v := range extraLabels {
pr.ExtraLabels[k] = v
}
}
}