-
Notifications
You must be signed in to change notification settings - Fork 78
/
util.go
60 lines (48 loc) · 1.36 KB
/
util.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
package engine
import (
kcl "kusionstack.io/kclvm-go"
"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/log"
kyaml "kusionstack.io/kusion/pkg/util/yaml"
)
const (
Separator = ":"
)
func NewRequestResourceForKubernetes(r kcl.KCLResult) (*models.Resource, string, error) {
// Get kubernetes manifestations, such as kind, metadata.name, metadata.namespace etc
docs, err := kyaml.YAML2Documents(r.YAMLString())
if err != nil {
return nil, "", err
}
if len(docs) > 1 {
log.Warn("document size is greater than 1")
}
doc := docs[0]
// Parse kubernetes resource
apiVersion, err := kyaml.GetByPathString(doc, "$.apiVersion")
if err != nil {
return nil, "", err
}
kind, err := kyaml.GetByPathString(doc, "$.kind")
if err != nil {
return nil, "", err
}
metadataName, err := kyaml.GetByPathString(doc, "$.metadata.name")
if err != nil {
return nil, "", err
}
metadataNamespace, _ := kyaml.GetByPathString(doc, "$.metadata.namespace")
// Build request resource for kubernetes
return &models.Resource{
ID: BuildIDForKubernetes(apiVersion, kind, metadataNamespace, metadataName),
Attributes: r,
DependsOn: nil,
}, kind, nil
}
func BuildIDForKubernetes(apiVersion, kind, namespace, name string) string {
key := apiVersion + Separator + kind + Separator
if namespace != "" {
key += namespace + Separator
}
return key + name
}