-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathgetComponents.go
More file actions
87 lines (76 loc) · 1.87 KB
/
getComponents.go
File metadata and controls
87 lines (76 loc) · 1.87 KB
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
package manifests
import (
"context"
"encoding/json"
"github.com/meshery/meshkit/utils"
k8s "github.com/meshery/meshkit/utils/kubernetes"
"gopkg.in/yaml.v3"
"io"
"strings"
)
func GetFromManifest(ctx context.Context, url string, resource int, cfg Config) (*Component, error) {
manifest, err := utils.ReadFileSource(url)
if err != nil {
return nil, err
}
comp, err := GenerateComponents(ctx, manifest, resource, cfg)
if err != nil {
return nil, err
}
return comp, nil
}
func GetFromHelm(ctx context.Context, url string, resource int, cfg Config) (*Component, error) {
manifest, err := k8s.GetManifestsFromHelm(url)
if err != nil {
return nil, err
}
comp, err := GenerateComponents(ctx, manifest, resource, cfg)
if err != nil {
return nil, err
}
return comp, nil
}
func GetCrdsFromHelm(url string) ([]string, error) {
manifest, err := k8s.GetManifestsFromHelm(url)
if err != nil {
return nil, err
}
manifest = repairYaml(manifest)
dec := yaml.NewDecoder(strings.NewReader(manifest))
var mans []string
for {
var parsedYaml map[string]interface{}
if err := dec.Decode(&parsedYaml); err != nil {
if err == io.EOF {
break
}
return nil, err
}
b, err := json.Marshal(parsedYaml)
if err != nil {
return nil, err
}
mans = append(mans, string(b))
}
return removeNonCrdValues(mans), nil
}
func repairYaml(doc string) string {
fixed := strings.ReplaceAll(doc, "\napiVersion:", "\n---\napiVersion:")
for strings.Contains(fixed, "\n---\n---\n") {
fixed = strings.ReplaceAll(fixed, "\n---\n---\n", "\n---\n")
}
return fixed
}
func removeNonCrdValues(crds []string) []string {
out := make([]string, 0)
for _, crd := range crds {
var crdMap map[string]interface{}
if err := json.Unmarshal([]byte(crd), &crdMap); err != nil {
continue
}
if crdMap["kind"] == "CustomResourceDefinition" {
out = append(out, crd)
}
}
return out
}