forked from kubernetes-sigs/kubebuilder-declarative-pattern
/
objects.go
413 lines (342 loc) · 9.75 KB
/
objects.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package manifest
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// Objects holds a collection of objects, so that we can filter / sequence them
type Objects struct {
Items []*Object
Blobs [][]byte
Path string
}
// GetItems is a nil-safe wrapper around
func (o *Objects) GetItems() []*Object {
if o == nil {
return nil
}
return o.Items
}
type Object struct {
object *unstructured.Unstructured
Group string
Version string
Kind string
name string
namespace string
json []byte
}
func ParseJSONToObject(json []byte) (*Object, error) {
o, gvk, err := unstructured.UnstructuredJSONScheme.Decode(json, nil, nil)
if err != nil {
return nil, fmt.Errorf("error parsing json into unstructured object: %v", err)
}
u, ok := o.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("parsed unexpected type %T", o)
}
return &Object{
object: u,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
name: u.GetName(),
namespace: u.GetNamespace(),
json: json,
}, nil
}
func (o *Object) AddLabels(labels map[string]string) {
merged := make(map[string]string)
for k, v := range o.object.GetLabels() {
merged[k] = v
}
for k, v := range labels {
merged[k] = v
}
o.object.SetLabels(merged)
// Invalidate cached json
o.json = nil
}
func (o *Object) AddAnnotations(annotations map[string]string) {
merged := make(map[string]string)
for k, v := range o.object.GetAnnotations() {
merged[k] = v
}
for k, v := range annotations {
merged[k] = v
}
o.object.SetAnnotations(merged)
// Invalidate cached json
o.json = nil
}
func (o *Object) GetNamespace() string {
return o.namespace
}
func (o *Object) SetNamespace(ns string) error {
if err := o.SetNestedField(ns, "metadata", "namespace"); err != nil {
return fmt.Errorf("failed to set namespace: %w", err)
}
o.namespace = ns
return nil
}
func (o *Object) GetName() string {
return o.name
}
func (o *Object) SetName(name string) error {
if err := o.SetNestedField(name, "metadata", "name"); err != nil {
return fmt.Errorf("failed to set name: %w", err)
}
o.name = name
return nil
}
func (o *Object) SetNestedStringMap(value map[string]string, fields ...string) error {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
err := unstructured.SetNestedStringMap(o.object.Object, value, fields...)
// Invalidate cached json
o.json = nil
return err
}
func nestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
var val interface{} = obj
for i, field := range fields {
if m, ok := val.(map[string]interface{}); ok {
val, ok = m[field]
if !ok {
return nil, false, nil
}
} else {
return nil, false, fmt.Errorf("%v accessor error: %v is of the type %T, expected map[string]interface{}", strings.Join(fields[:i+1], "."), val, val)
}
}
return val, true, nil
}
func (o *Object) MutateContainers(fn func(map[string]interface{}) error) error {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
containers, found, err := nestedFieldNoCopy(o.object.Object, "spec", "template", "spec", "containers")
if err != nil {
return fmt.Errorf("error reading containers: %v", err)
}
if !found {
return fmt.Errorf("containers not found")
}
containerList, ok := containers.([]interface{})
if !ok {
return fmt.Errorf("containers was not a list")
}
initContainers, found, err := nestedFieldNoCopy(o.object.Object, "spec", "template", "spec", "initContainers")
if err != nil {
return fmt.Errorf("error reading init containers: %v", err)
}
if found {
initContainerList, ok := initContainers.([]interface{})
if !ok {
return fmt.Errorf("init containers was not a list")
}
containerList = append(containerList, initContainerList...)
}
for _, co := range containerList {
container, ok := co.(map[string]interface{})
if !ok {
return fmt.Errorf("container was not an object")
}
if err := fn(container); err != nil {
return err
}
}
// Invalidate cached json
o.json = nil
return err
}
func (o *Object) MutatePodSpec(fn func(map[string]interface{}) error) error {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
sp, found, err := nestedFieldNoCopy(o.object.Object, "spec", "template", "spec")
if err != nil {
return fmt.Errorf("error reading containers: %v", err)
}
if !found {
return fmt.Errorf("pod spec not found")
}
podSpec, ok := sp.(map[string]interface{})
if !ok {
return fmt.Errorf("pod spec was not an object")
}
if err := fn(podSpec); err != nil {
return err
}
// Invalidate cached json
o.json = nil
return err
}
func (o *Object) NestedStringMap(fields ...string) (map[string]string, bool, error) {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
return unstructured.NestedStringMap(o.object.Object, fields...)
}
func (o *Object) SetNestedField(value interface{}, fields ...string) error {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
err := unstructured.SetNestedField(o.object.Object, value, fields...)
// Invalidate cached json
o.json = nil
return err
}
func (o *Object) SetNestedSlice(value []interface{}, fields ...string) error {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
err := unstructured.SetNestedSlice(o.object.Object, value, fields...)
// Invalidate cached json
o.json = nil
return err
}
func (o *Object) SetNestedFieldNoCopy(value interface{}, fields ...string) error {
if o.object.Object == nil {
o.object.Object = make(map[string]interface{})
}
// err := unstructured.SetNestedField(o.object.Object, value, fields...)
m := o.object.Object
for i, field := range fields[:len(fields)-1] {
if val, ok := m[field]; ok {
if valMap, ok := val.(map[string]interface{}); ok {
m = valMap
} else {
return fmt.Errorf("value cannot be set because %v is not a map[string]interface{}", fields[:i+1])
}
} else {
newVal := make(map[string]interface{})
m[field] = newVal
m = newVal
}
}
m[fields[len(fields)-1]] = value
// Invalidate cached json
o.json = nil
return nil
}
func (o *Object) JSON() ([]byte, error) {
if o.json != nil {
return o.json, nil
}
b, err := o.object.MarshalJSON()
if err != nil {
return nil, err
}
o.json = b
return b, nil
}
// UnstructuredContent exposes the raw object, primarily for testing
func (o *Object) UnstructuredObject() *unstructured.Unstructured {
return o.object
}
func (o *Object) GroupKind() schema.GroupKind {
return o.object.GroupVersionKind().GroupKind()
}
func (o *Object) GroupVersionKind() schema.GroupVersionKind {
return o.object.GroupVersionKind()
}
func (o *Objects) JSONManifest() (string, error) {
var b bytes.Buffer
for i, item := range o.Items {
if i != 0 {
b.WriteString("\n\n")
}
// We build a JSON manifest because conversion to yaml is harder
// (and we've lost line numbers anyway if we applied any transforms)
json, err := item.JSON()
if err != nil {
return "", fmt.Errorf("error building json: %v", err)
}
b.Write(json)
}
return b.String(), nil
}
// Sort will order the items in Objects in order of score, group, kind, name. The intent is to
// have a deterministic ordering in which Objects are applied.
func (o *Objects) Sort(score func(o *Object) int) {
sort.Slice(o.Items, func(i, j int) bool {
iScore := score(o.Items[i])
jScore := score(o.Items[j])
return iScore < jScore ||
(iScore == jScore &&
o.Items[i].Group < o.Items[j].Group) ||
(iScore == jScore &&
o.Items[i].Group == o.Items[j].Group &&
o.Items[i].Kind < o.Items[j].Kind) ||
(iScore == jScore &&
o.Items[i].Group == o.Items[j].Group &&
o.Items[i].Kind == o.Items[j].Kind &&
o.Items[i].name < o.Items[j].name)
})
}
func ParseObjects(ctx context.Context, manifest string) (*Objects, error) {
log := log.FromContext(ctx)
objects := &Objects{}
reader := k8syaml.NewYAMLReader(bufio.NewReader(strings.NewReader(manifest)))
for {
raw, err := reader.Read()
if err != nil {
if err == io.EOF {
return objects, nil
}
return nil, fmt.Errorf("invalid YAML doc: %w", err)
}
raw = bytes.TrimSpace(raw)
out := unstructured.Unstructured{}
if err := k8syaml.Unmarshal(raw, &out); err != nil {
log.WithValues("error", err).WithValues("yaml", raw).V(2).Info("Unable to parse into Unstructured, storing as blob")
objects.Blobs = append(objects.Blobs, append(bytes.TrimPrefix(raw, []byte("---\n")), '\n'))
}
if len(raw) == 0 || bytes.Equal(raw, []byte("null")) || len(out.Object) == 0 {
continue
}
o, err := NewObject(&out)
if err != nil {
return nil, err
}
objects.Items = append(objects.Items, o)
}
}
func newObject(u *unstructured.Unstructured, json []byte) (*Object, error) {
o := &Object{
object: u,
json: json,
}
gvk := u.GetObjectKind().GroupVersionKind()
o.Group = gvk.Group
o.Kind = gvk.Kind
o.name = u.GetName()
o.namespace = u.GetNamespace()
return o, nil
}
func NewObject(u *unstructured.Unstructured) (*Object, error) {
var json []byte
return newObject(u, json)
}