/
definition.go
461 lines (435 loc) · 13.3 KB
/
definition.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
/*
Copyright 2021 The KubeVela 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 definition contains some helper functions used in vela CLI
// and vela addon mechanism
package definition
import (
"context"
"fmt"
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
"cuelang.org/go/encoding/gocode/gocodec"
"cuelang.org/go/tools/fix"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
velacue "github.com/oam-dev/kubevela/pkg/cue"
"github.com/oam-dev/kubevela/pkg/cue/model/sets"
"github.com/oam-dev/kubevela/pkg/cue/model/value"
"github.com/oam-dev/kubevela/pkg/cue/packages"
)
const (
// DescriptionKey the key for accessing definition description
DescriptionKey = "definition.oam.dev/description"
// UserPrefix defines the prefix of user customized label or annotation
UserPrefix = "custom.definition.oam.dev/"
)
var (
// DefinitionTemplateKeys the keys for accessing definition template
DefinitionTemplateKeys = []string{"spec", "schematic", "cue", "template"}
// DefinitionTypeToKind maps the definition types to corresponding kinds
DefinitionTypeToKind = map[string]string{
"component": v1beta1.ComponentDefinitionKind,
"trait": v1beta1.TraitDefinitionKind,
"policy": v1beta1.PolicyDefinitionKind,
"workload": v1beta1.WorkloadDefinitionKind,
"scope": v1beta1.ScopeDefinitionKind,
"workflow-step": v1beta1.WorkflowStepDefinitionKind,
}
)
// Definition the general struct for handling all kinds of definitions like ComponentDefinition or TraitDefinition
type Definition struct {
unstructured.Unstructured
}
// SetGVK set the GroupVersionKind of Definition
func (def *Definition) SetGVK(kind string) {
def.SetGroupVersionKind(schema.GroupVersionKind{
Group: v1beta1.Group,
Version: v1beta1.Version,
Kind: kind,
})
}
// GetType gets the type of Definition
func (def *Definition) GetType() string {
kind := def.GetKind()
for k, v := range DefinitionTypeToKind {
if v == kind {
return k
}
}
return strings.ToLower(strings.TrimSuffix(kind, "Definition"))
}
// SetType sets the type of Definition
func (def *Definition) SetType(t string) error {
kind, ok := DefinitionTypeToKind[t]
if !ok {
return fmt.Errorf("invalid type %s", t)
}
def.SetGVK(kind)
return nil
}
// ToCUE converts Definition to CUE value (with predefined Definition's cue format)
func (def *Definition) ToCUE() (*cue.Value, string, error) {
annotations := map[string]string{}
for key, val := range def.GetAnnotations() {
if strings.HasPrefix(key, UserPrefix) {
annotations[strings.TrimPrefix(key, UserPrefix)] = val
}
}
desc := def.GetAnnotations()[DescriptionKey]
labels := map[string]string{}
for key, val := range def.GetLabels() {
if strings.HasPrefix(key, UserPrefix) {
labels[strings.TrimPrefix(key, UserPrefix)] = val
}
}
spec := map[string]interface{}{}
for key, val := range def.Object["spec"].(map[string]interface{}) {
if key != "schematic" {
spec[key] = val
}
}
obj := map[string]interface{}{
def.GetName(): map[string]interface{}{
"type": def.GetType(),
"description": desc,
"annotations": annotations,
"labels": labels,
"attributes": spec,
},
}
r := &cue.Runtime{}
codec := gocodec.New(r, &gocodec.Config{})
val, err := codec.Decode(obj)
if err != nil {
return nil, "", err
}
templateString, _, err := unstructured.NestedString(def.Object, DefinitionTemplateKeys...)
if err != nil {
return nil, "", err
}
templateString, err = formatCUEString(templateString)
if err != nil {
return nil, "", err
}
return &val, templateString, nil
}
// ToCUEString converts definition to CUE value and then encode to string
func (def *Definition) ToCUEString() (string, error) {
val, templateString, err := def.ToCUE()
if err != nil {
return "", err
}
metadataString, err := sets.ToString(*val)
if err != nil {
return "", err
}
f, err := parser.ParseFile("-", templateString, parser.ParseComments)
if err != nil {
return "", errors.Wrapf(err, "failed to parse template cue string")
}
f = fix.File(f)
var importDecls, templateDecls []ast.Decl
for _, decl := range f.Decls {
if importDecl, ok := decl.(*ast.ImportDecl); ok {
importDecls = append(importDecls, importDecl)
} else {
templateDecls = append(templateDecls, decl)
}
}
importString, err := encodeDeclsToString(importDecls)
if err != nil {
return "", errors.Wrapf(err, "failed to encode import decls")
}
templateString, err = encodeDeclsToString(templateDecls)
if err != nil {
return "", errors.Wrapf(err, "failed to encode template decls")
}
templateString = fmt.Sprintf("template: {\n%s}", templateString)
completeCUEString := importString + "\n" + metadataString + "\n" + templateString
if completeCUEString, err = formatCUEString(completeCUEString); err != nil {
return "", errors.Wrapf(err, "failed to format cue format string")
}
return completeCUEString, nil
}
// FromCUE converts CUE value (predefined Definition's cue format) to Definition
// nolint:gocyclo
func (def *Definition) FromCUE(val *cue.Value, templateString string) error {
if def.Object == nil {
def.Object = map[string]interface{}{}
}
annotations := map[string]string{}
for k, v := range def.GetAnnotations() {
if !strings.HasPrefix(k, UserPrefix) && k != DescriptionKey {
annotations[k] = v
}
}
labels := map[string]string{}
for k, v := range def.GetLabels() {
if !strings.HasPrefix(k, UserPrefix) {
annotations[k] = v
}
}
spec, ok := def.Object["spec"].(map[string]interface{})
if !ok {
spec = map[string]interface{}{}
}
codec := gocodec.New(&cue.Runtime{}, &gocodec.Config{})
nameFlag := false
fields, err := val.Fields()
if err != nil {
return err
}
for fields.Next() {
definitionName := fields.Label()
v := fields.Value()
if nameFlag {
return fmt.Errorf("duplicated definition name found, %s and %s", def.GetName(), definitionName)
}
nameFlag = true
def.SetName(definitionName)
_fields, err := v.Fields()
if err != nil {
return err
}
for _fields.Next() {
_key := _fields.Label()
_value := _fields.Value()
switch _key {
case "type":
_type, err := _value.String()
if err != nil {
return err
}
if err = def.SetType(_type); err != nil {
return err
}
case "description":
desc, err := _value.String()
if err != nil {
return err
}
annotations[DescriptionKey] = desc
case "annotations":
var _annotations map[string]string
if err := codec.Encode(_value, &_annotations); err != nil {
return err
}
for _k, _v := range _annotations {
annotations[UserPrefix+_k] = _v
}
case "labels":
var _labels map[string]string
if err := codec.Encode(_value, &_labels); err != nil {
return err
}
for _k, _v := range _labels {
labels[UserPrefix+_k] = _v
}
case "attributes":
if err := codec.Encode(_value, &spec); err != nil {
return err
}
}
}
}
def.SetAnnotations(annotations)
def.SetLabels(labels)
if err := unstructured.SetNestedField(spec, templateString, DefinitionTemplateKeys[1:]...); err != nil {
return err
}
def.Object["spec"] = spec
return nil
}
func encodeDeclsToString(decls []ast.Decl) (string, error) {
s := ""
for _, decl := range decls {
bs, err := format.Node(decl, format.Simplify())
if err != nil {
return "", errors.Wrapf(err, "failed to encode decl to string: %v", decl)
}
s += string(bs) + "\n"
}
return s, nil
}
// FromCUEString converts cue string into Definition
func (def *Definition) FromCUEString(cueString string, config *rest.Config) error {
r := &cue.Runtime{}
f, err := parser.ParseFile("-", cueString, parser.ParseComments)
if err != nil {
return err
}
n := fix.File(f)
var importDecls, metadataDecls, templateDecls []ast.Decl
for _, decl := range n.Decls {
if importDecl, ok := decl.(*ast.ImportDecl); ok {
importDecls = append(importDecls, importDecl)
} else if field, ok := decl.(*ast.Field); ok {
label := ""
switch l := field.Label.(type) {
case *ast.Ident:
label = l.Name
case *ast.BasicLit:
label = l.Value
}
if label == "" {
return errors.Errorf("found unexpected decl when parsing cue: %v", label)
}
if label == "template" {
if v, ok := field.Value.(*ast.StructLit); ok {
templateDecls = append(templateDecls, v.Elts...)
} else {
return errors.Errorf("unexpected decl found in template: %v", decl)
}
} else {
metadataDecls = append(metadataDecls, field)
}
}
}
if len(metadataDecls) == 0 {
return errors.Errorf("no metadata found, invalid")
}
if len(templateDecls) == 0 {
return errors.Errorf("no template found, invalid")
}
var importString, metadataString, templateString string
if importString, err = encodeDeclsToString(importDecls); err != nil {
return errors.Wrapf(err, "failed to encode import decls to string")
}
if metadataString, err = encodeDeclsToString(metadataDecls); err != nil {
return errors.Wrapf(err, "failed to encode metadata decls to string")
}
// notice that current template decls are concatenated without any blank lines which might be inconsistent with original cue file, but it would not affect the syntax
if templateString, err = encodeDeclsToString(templateDecls); err != nil {
return errors.Wrapf(err, "failed to encode template decls to string")
}
inst, err := r.Compile("-", metadataString)
if err != nil {
return err
}
templateString, err = formatCUEString(importString + templateString)
if err != nil {
return err
}
// validate template
if config != nil {
pd, err := packages.NewPackageDiscover(config)
if err != nil {
return err
}
if _, err = value.NewValue(templateString+"\n"+velacue.BaseTemplate, pd, ""); err != nil {
return err
}
} else if _, err = r.Compile("-", templateString+"\n"+velacue.BaseTemplate); err != nil {
return err
}
val := inst.Value()
return def.FromCUE(&val, templateString)
}
// ValidDefinitionTypes return the list of valid definition types
func ValidDefinitionTypes() []string {
var types []string
for k := range DefinitionTypeToKind {
types = append(types, k)
}
return types
}
// SearchDefinition search the Definition in k8s by traversing all possible results across types or namespaces
func SearchDefinition(definitionName string, c client.Client, definitionType string, namespace string) ([]unstructured.Unstructured, error) {
ctx := context.Background()
var kinds []string
if definitionType != "" {
kind, ok := DefinitionTypeToKind[definitionType]
if !ok {
return nil, fmt.Errorf("invalid definition type %s", kind)
}
kinds = []string{kind}
} else {
for _, kind := range DefinitionTypeToKind {
kinds = append(kinds, kind)
}
}
var listOptions []client.ListOption
if namespace != "" {
listOptions = []client.ListOption{client.InNamespace(namespace)}
}
var definitions []unstructured.Unstructured
for _, kind := range kinds {
objs := unstructured.UnstructuredList{}
objs.SetGroupVersionKind(schema.GroupVersionKind{
Group: v1beta1.Group,
Version: v1beta1.Version,
Kind: kind + "List",
})
if err := c.List(ctx, &objs, listOptions...); err != nil {
return nil, errors.Wrapf(err, "failed to get %s", kind)
}
for _, obj := range objs.Items {
if definitionName == "*" || obj.GetName() == definitionName {
definitions = append(definitions, obj)
}
}
}
return definitions, nil
}
// GetDefinitionDefaultSpec returns the default spec of Definition with given kind. This may be implemented with cue in the future.
func GetDefinitionDefaultSpec(kind string) map[string]interface{} {
switch kind {
case v1beta1.ComponentDefinitionKind:
return map[string]interface{}{
"workload": map[string]interface{}{
"definition": map[string]interface{}{
"apiVersion": "<change me> apps/v1",
"kind": "<change me> Deployment",
},
},
"schematic": map[string]interface{}{
"cue": map[string]interface{}{
"template": "output: {}\nparameter: {}\n",
},
},
}
case v1beta1.TraitDefinitionKind:
return map[string]interface{}{
"appliesToWorkloads": []interface{}{},
"conflictsWith": []interface{}{},
"workloadRefPath": "",
"definitionRef": "",
"podDisruptive": false,
"schematic": map[string]interface{}{
"cue": map[string]interface{}{
"template": "patch: {}\nparameter: {}\n",
},
},
}
}
return map[string]interface{}{}
}
func formatCUEString(cueString string) (string, error) {
f, err := parser.ParseFile("-", cueString, parser.ParseComments)
if err != nil {
return "", errors.Wrapf(err, "failed to parse file during format cue string")
}
n := fix.File(f)
b, err := format.Node(n, format.Simplify())
if err != nil {
return "", errors.Wrapf(err, "failed to format node during formating cue string")
}
return string(b), nil
}