/
unstructured.go
567 lines (513 loc) · 17 KB
/
unstructured.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
Copyright 2018 The OpenEBS 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.
*/
// TODO
// Move this to pkg/unstruct/v1alpha1/unstruct.go
// TODO
// Create a new struct called unstruct that wraps unstructured.Unstructured
// An unstructured instance is a JSON compatible structure and is compatible
// to any kubernetes resource (native as well as custom). This instance can be
// used during following kubernetes API invocations:
//
// - Create
// - Update
// - UpdateStatus
// - Delete
// - DeleteCollection
// - Get
// - List
// - Watch
// - Patch
package v1alpha1
import (
"strings"
"github.com/ghodss/yaml"
"github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
"github.com/openebs/maya/pkg/util"
"github.com/openebs/maya/pkg/version"
kubever "github.com/openebs/maya/pkg/version/kubernetes"
errors "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// kind represents the name of the kubernetes kind
type kind string
// resource converts the name of the kubernetes kind to corresponding kubernetes
// resource's name
//
// NOTE:
// This may not be the best of approaches to get name of a resource. However,
// this fits the current requirement. This might need a revisit depending on
// future requirements.
func (k kind) resource() (resource string) {
resource = strings.ToLower(string(k))
switch resource {
case "":
return
case "storageclass":
return "storageclasses"
default:
return resource + "s"
}
}
// isNamespaced flags if the kind is namespaced or not
//
// NOTE:
// This may not be the best of approaches to flag a resource as namespaced or
// not. However, this fits the current requirement. This might need a revisit
// depending on future requirements.
func (k kind) isNamespaced() (no bool) {
ks := strings.ToLower(string(k))
switch ks {
case "customresourcedefinition":
return no
case "storageclass":
return no
case "persistentvolume":
return no
case "castemplate":
return no
case "storagepoolclaim":
return no
case "cstorpool":
return no
case "storagepool":
return no
default:
return !no
}
}
// GroupVersionResourceFromGVK returns the GroupVersionResource of the provided
// unstructured instance by making use of this instance's GroupVersionKind info
//
// NOTE:
// Resource is assumed as plural of Kind
func GroupVersionResourceFromGVK(unstructured *unstructured.Unstructured) (gvr schema.GroupVersionResource) {
if unstructured == nil {
return
}
gvk := unstructured.GroupVersionKind()
gvr.Group = strings.ToLower(gvk.Group)
gvr.Version = strings.ToLower(gvk.Version)
gvr.Resource = kind(gvk.Kind).resource()
return
}
// WithBytesUnstructuredCreator abstracts creation of unstructured instance
// from the provided bytes
type WithBytesUnstructuredCreator func(raw []byte) (*unstructured.Unstructured, error)
// CreateUnstructuredFromYamlBytes creates an unstructured instance from the
// provided YAML document in bytes
//
// NOTE:
// This is an implementation of WithBytesUnstructuredCreator
func CreateUnstructuredFromYamlBytes(raw []byte) (*unstructured.Unstructured, error) {
m := map[string]interface{}{}
err := yaml.Unmarshal(raw, &m)
if err != nil {
return nil, errors.Wrapf(err, "failed to create unstructured instance from yaml: %s", raw)
}
return &unstructured.Unstructured{
Object: m,
}, nil
}
// UnstructuredCreator abstracts creation of unstructured instance from the
// provided document
type UnstructuredCreator func(document string) (*unstructured.Unstructured, error)
// CreateUnstructuredFromYaml creates an unstructured instance from the
// provided YAML document
func CreateUnstructuredFromYaml(document string) (*unstructured.Unstructured, error) {
return CreateUnstructuredFromYamlBytes([]byte(document))
}
// CreateUnstructuredFromJson creates an unstructured instance from the
// provided JSON document
func CreateUnstructuredFromJson(document string) (*unstructured.Unstructured, error) {
uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, []byte(document))
if err != nil {
return nil, errors.Wrap(err, "failed to create unstructured instance from json document")
}
return uncastObj.(*unstructured.Unstructured), nil
}
// UnstructuredMiddleware abstracts updating given unstructured instance
type UnstructuredMiddleware func(given *unstructured.Unstructured) (updated *unstructured.Unstructured)
// UnstructuredPredicate abstracts evaluating a condition against the provided
// unstructured instance
type UnstructuredPredicate func(given *unstructured.Unstructured) bool
// UnstructuredPredicateList represents a list of unstructured predicates
type UnstructuredPredicateList []UnstructuredPredicate
// All evaluates if all the predicates succeed
func (l UnstructuredPredicateList) All(given *unstructured.Unstructured) bool {
for _, p := range l {
if !p(given) {
return false
}
}
return true
}
// Any evaluates if at least one of the predicates succeed
func (l UnstructuredPredicateList) Any(given *unstructured.Unstructured) bool {
if len(l) == 0 {
return true
}
for _, p := range l {
if p(given) {
return true
}
}
return false
}
// IsNamespaceScoped flags if the given unstructured instance is namespace
// scoped
//
// NOTE:
// This is a UnstructuredPredicate implementation
func IsNamespaceScoped(given *unstructured.Unstructured) bool {
if given == nil {
return false
}
return kind(given.GetKind()).isNamespaced()
}
// IsCASTemplate flags if the given unstructured instance is a CASTemplate
// scoped
//
// NOTE:
// This is a UnstructuredPredicate implementation
func IsCASTemplate(given *unstructured.Unstructured) bool {
if given == nil {
return false
}
return strings.ToLower(given.GetKind()) == "castemplate"
}
// IsNameUnVersioned flags if the given unstructured instance name has
// version as its suffix
//
// NOTE:
// This is a UnstructuredPredicate implementation
func IsNameUnVersioned(given *unstructured.Unstructured) bool {
if given == nil {
return false
}
return !IsNameVersioned(given)
}
// IsNameVersioned flags if the given unstructured instance name does not have
// version as its suffix
//
// NOTE:
// This is a UnstructuredPredicate implementation
func IsNameVersioned(given *unstructured.Unstructured) bool {
if given == nil {
return false
}
return version.IsVersioned(given.GetName())
}
// IsRunTask flags if the given unstructured instance is a RunTask
// scoped
//
// NOTE:
// This is a UnstructuredPredicate implementation
func IsRunTask(given *unstructured.Unstructured) bool {
if given == nil {
return false
}
return strings.ToLower(given.GetKind()) == "runtask"
}
// UpdateNamespace updates the given unstructured instance's namespace with
// a valid namespace i.e. non-empty namespace
func UpdateNamespace(n string) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
namespace := strings.TrimSpace(n)
if given == nil || len(namespace) == 0 {
return given
}
given.SetNamespace(namespace)
return given
}
}
// AddKubeServerVersionToLabels adds kubernetes server version to instance's
// labels
//
// TODO
// Move this override flag to a Predicate. This will follow idiomatic Maya
// convention to separate conditional(s) logic from core business logic. This
// in turn helps in readability & maintainability of the codebase.
//
// e.g.
// func IsLabelSet(key string) UnstructuredPredicate {...}
// func IsLabelUnSet(key string) UnstructuredPredicate {...}
func AddKubeServerVersionToLabels(override bool) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil {
return given
}
l := given.GetLabels()
if l == nil {
l = map[string]string{}
}
// TODO
// Nested if else clause is bad. This will get handled once IsLabelUnSet
// predicate is used.
if len(l[string(v1alpha1.KubeServerVersionPlainKey)]) == 0 || override {
// TODO
// error should not be ignored here
// Need to design a custom struct that wraps unstructured.Unstructured
// instance and has fields to accomodate error(s), warning(s), etc
//
// NOTE:
// This will be done along with idiomatic maya refactorings
vInfo, err := GetServerVersion()
if err != nil {
return given
}
l[string(v1alpha1.KubeServerVersionPlainKey)] = kubever.AsLabelValue(vInfo.GitVersion)
}
given.SetLabels(l)
return given
}
}
// AddNameToLabels extracts the instance's name & adds it to the same instance's
// labels mapped by the provided label key
//
// TODO
// Move this override flag to a Predicate. This will follow idiomatic Maya
// convention to separate conditional(s) logic from core business logic. This
// in turn helps in readability & maintainability of the codebase.
//
// e.g.
// func IsLabelSet(key string) UnstructuredPredicate {...}
// func IsLabelUnSet(key string) UnstructuredPredicate {...}
func AddNameToLabels(key string, override bool) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil || len(key) == 0 {
return given
}
l := given.GetLabels()
if l == nil {
l = map[string]string{}
}
if override {
l[key] = given.GetName()
}
if len(l[key]) == 0 {
l[key] = given.GetName()
}
given.SetLabels(l)
return given
}
}
// SuffixNameWithVersion suffixes the given unstructured instance's name with
// current version
// Converting to lowercase is required as the version can have custom tags having
// uppercase characters like 1.11.0-ce-RC2 and uppercase characters are not valid
// in names of the k8s resources
func SuffixNameWithVersion() UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil || IsNameVersioned(given) {
return given
}
given.SetName(version.WithSuffixLower(given.GetName()))
return given
}
}
// SuffixSliceWithVersionAtPath updates the given unstructured instance's
// path with provided slice after suffixing each slice item with version
func SuffixSliceWithVersionAtPath(o []interface{}, path string) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil {
return given
}
slice := make([]string, 0, len(o))
for _, v := range o {
if s, ok := v.(string); ok {
slice = append(slice, s)
}
}
if len(slice) != len(o) {
return given
}
u := version.WithSuffixesIf(slice, version.IsNotVersioned)
util.SetNestedSlice(given.Object, u, strings.Split(path, ".")...)
return given
}
}
// SuffixStringSliceWithVersionAtPath updates the given unstructured instance's
// path with provided slice after suffixing each slice item with version
func SuffixStringSliceWithVersionAtPath(s []string, path string) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil {
return given
}
u := version.WithSuffixesIf(s, version.IsNotVersioned)
util.SetNestedSlice(given.Object, u, strings.Split(path, ".")...)
return given
}
}
// SuffixStringWithVersionAtPath updates the given unstructured instance's
// path with provided string after suffixing it with version
func SuffixStringWithVersionAtPath(s, path string) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil {
return given
}
u := version.WithSuffixIf(s, version.IsNotVersioned)
util.SetNestedField(given.Object, u, strings.Split(path, ".")...)
return given
}
}
// SuffixWithVersionAtPath suffixes the value(s) extracted from the provided path
// with version
//
// NOTE: Currently supports path having following values:
// - string
// - []string
func SuffixWithVersionAtPath(path string) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil {
return given
}
o := util.GetNestedField(given.Object, strings.Split(path, ".")...)
slice, ok := o.([]interface{})
if ok {
return SuffixSliceWithVersionAtPath(slice, path)(given)
}
sslice, ok := o.([]string)
if ok {
return SuffixStringSliceWithVersionAtPath(sslice, path)(given)
}
s, ok := o.(string)
if ok {
return SuffixStringWithVersionAtPath(s, path)(given)
}
return given
}
}
// UpdateLabels updates the unstructured instance's labels
//
// TODO
// Move this override flag to a Predicate. This will follow idiomatic Maya
// convention to separate conditional(s) logic from core business logic. This
// in turn helps in readability & maintainability of the codebase.
//
// e.g.
// func IsLabelSet(key string) UnstructuredPredicate {...}
// func IsLabelUnSet(key string) UnstructuredPredicate {...}
func UpdateLabels(l map[string]string, override bool) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) (updated *unstructured.Unstructured) {
if given == nil || len(l) == 0 {
return given
}
orig := given.GetLabels()
if orig == nil {
orig = map[string]string{}
}
for k, v := range l {
if override {
orig[k] = v
}
if len(orig[k]) == 0 {
orig[k] = v
}
}
given.SetLabels(orig)
return given
}
}
// TODO
// Convert this into a method of yet to be defined unstruct struct
// UnstructuredMap maps the given unstructured instance by executing the
// provided middleware. Map is considered if all the provided predicate
// evaluations succeed.
func UnstructuredMap(m UnstructuredMiddleware, p ...UnstructuredPredicate) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) *unstructured.Unstructured {
conds := UnstructuredPredicateList(p)
if conds.All(given) {
return m(given)
}
return given
}
}
// TODO
// Convert this into a method of yet to be defined unstruct struct
// UnstructuredMapIfAny maps the given unstructured instance by executing the
// provided middleware. Map is considered if at least one of the provided
// predicate evaluations succeed.
func UnstructuredMapIfAny(m UnstructuredMiddleware, p ...UnstructuredPredicate) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) *unstructured.Unstructured {
conds := UnstructuredPredicateList(p)
if conds.Any(given) {
return m(given)
}
return given
}
}
// TODO
// Convert this into a method of yet to be defined unstruct struct
// UnstructuredMapAll maps the given unstructured instance by executing all
// the provided middlewares. Map is considered if the provided predicate
// evaluations succeed.
func UnstructuredMapAll(m []UnstructuredMiddleware, p ...UnstructuredPredicate) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) *unstructured.Unstructured {
for _, mid := range m {
given = UnstructuredMap(mid, p...)(given)
}
return given
}
}
// TODO
// Convert this into a method of yet to be defined unstruct struct
// UnstructuredMapAllIfAny maps the given unstructured instance by executing all
// the provided middlewares. Map is considered if atleast one of the provided
// predicate evaluations succeed.
func UnstructuredMapAllIfAny(m []UnstructuredMiddleware, p ...UnstructuredPredicate) UnstructuredMiddleware {
return func(given *unstructured.Unstructured) *unstructured.Unstructured {
for _, mid := range m {
given = UnstructuredMapIfAny(mid, p...)(given)
}
return given
}
}
// UnstructedList represents a list of unstructured instances
type UnstructedList struct {
Items []*unstructured.Unstructured
}
// Map will map each of its instance against the provided middleware if the
// provided predicate evaluations succeed
func (u UnstructedList) Map(m UnstructuredMiddleware, p ...UnstructuredPredicate) (ul UnstructedList) {
for _, unstruct := range u.Items {
unstruct = UnstructuredMap(m, p...)(unstruct)
if unstruct != nil {
ul.Items = append(ul.Items, unstruct)
}
}
return
}
// MapAll will map all the provided middlewares against each of its instance if
// the provided predicate evaluations succeed
func (u UnstructedList) MapAll(m []UnstructuredMiddleware, p ...UnstructuredPredicate) (ul UnstructedList) {
for _, unstruct := range u.Items {
unstruct = UnstructuredMapAll(m, p...)(unstruct)
if unstruct != nil {
ul.Items = append(ul.Items, unstruct)
}
}
return
}
// MapAllIfAny will map all the provided middlewares against each of its
// instance if at least one of the provided predicate evaluations succeed
func (u UnstructedList) MapAllIfAny(m []UnstructuredMiddleware, p ...UnstructuredPredicate) (ul UnstructedList) {
for _, unstruct := range u.Items {
unstruct = UnstructuredMapAllIfAny(m, p...)(unstruct)
if unstruct != nil {
ul.Items = append(ul.Items, unstruct)
}
}
return
}