This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
desired.go
114 lines (91 loc) · 3.04 KB
/
desired.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
package desired
import (
"os"
"path/filepath"
"strings"
yamlfile "github.com/caos/orbos/internal/utils/yaml"
"gopkg.in/yaml.v3"
"github.com/caos/orbos/internal/operator/boom/labels"
"github.com/caos/orbos/internal/operator/boom/name"
"github.com/caos/orbos/internal/utils/helper"
"github.com/caos/orbos/internal/utils/kustomize"
"github.com/caos/orbos/mntr"
"github.com/pkg/errors"
)
func Apply(monitor mntr.Monitor, resultFilePath, namespace string, appName name.Application, force bool) error {
resultFileDirPath := filepath.Dir(resultFilePath)
if err := prepareAdditionalFiles(resultFilePath, namespace, appName); err != nil {
return err
}
// apply resources
cmd, err := kustomize.New(resultFileDirPath)
if err != nil {
return err
}
cmd = cmd.Apply(force)
return errors.Wrapf(helper.Run(monitor, cmd.Build()), "Failed to apply with file %s", resultFilePath)
}
func Get(monitor mntr.Monitor, resultFilePath, namespace string, appName name.Application) ([]*helper.Resource, error) {
resultFileDirPath := filepath.Dir(resultFilePath)
if err := prepareAdditionalFiles(resultFilePath, namespace, appName); err != nil {
return nil, err
}
// apply resources
cmd, err := kustomize.New(resultFileDirPath)
if err != nil {
return nil, err
}
out, err := helper.RunWithOutput(monitor, cmd.Build())
if err != nil {
return nil, errors.Wrapf(err, "Failed to apply with file %s", resultFilePath)
}
resources := make([]*helper.Resource, 0)
parts := strings.Split(string(out), "\n---\n")
for _, part := range parts {
if part == "" {
continue
}
var resource helper.Resource
if err := yaml.Unmarshal([]byte(part), &resource); err != nil {
return nil, err
}
resources = append(resources, &resource)
}
return resources, nil
}
func prepareAdditionalFiles(resultFilePath, namespace string, appName name.Application) error {
resultFileDirPath := filepath.Dir(resultFilePath)
resultFileKustomizePath := filepath.Join(resultFileDirPath, "kustomization.yaml")
resultFileTransformerPath := filepath.Join(resultFileDirPath, "transformer.yaml")
if helper.FileExists(resultFileKustomizePath) {
if err := os.Remove(resultFileKustomizePath); err != nil {
return err
}
}
if helper.FileExists(resultFileTransformerPath) {
if err := os.Remove(resultFileTransformerPath); err != nil {
return err
}
}
transformer := &kustomize.LabelTransformer{
ApiVersion: "builtin",
Kind: "LabelTransformer",
Metadata: &kustomize.Metadata{
Name: "LabelTransformer",
},
Labels: labels.GetAllApplicationLabels(appName),
FieldSpecs: []*kustomize.FieldSpec{&kustomize.FieldSpec{Path: "metadata/labels", Create: true}},
}
if err := yamlfile.New(resultFileTransformerPath).AddStruct(transformer); err != nil {
return err
}
kustomizeFile := kustomize.File{
Namespace: namespace,
Resources: []string{filepath.Base(resultFilePath)},
Transformers: []string{filepath.Base(resultFileTransformerPath)},
}
if err := yamlfile.New(resultFileKustomizePath).AddStruct(kustomizeFile); err != nil {
return err
}
return nil
}