-
Notifications
You must be signed in to change notification settings - Fork 90
/
kustomization.go
55 lines (44 loc) · 1.34 KB
/
kustomization.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
package k8sutil
import (
"io/ioutil"
"sort"
"strings"
"github.com/pkg/errors"
kustomizetypes "sigs.k8s.io/kustomize/v3/pkg/types"
"sigs.k8s.io/yaml"
)
func ReadKustomizationFromFile(file string) (*kustomizetypes.Kustomization, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrap(err, "failed to read kustomization file")
}
k := kustomizetypes.Kustomization{}
if err := yaml.Unmarshal(b, &k); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal kustomization")
}
return &k, nil
}
// implementing Len Swap and Less allows sorting the type directly
type kustPatches []kustomizetypes.PatchStrategicMerge
func (s kustPatches) Len() int {
return len(s)
}
func (s kustPatches) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s kustPatches) Less(i, j int) bool {
return strings.Compare(string(s[i]), string(s[j])) < 0
}
func WriteKustomizationToFile(kustomization *kustomizetypes.Kustomization, file string) error {
sort.Strings(kustomization.Bases)
sort.Strings(kustomization.Resources)
sort.Sort(kustPatches(kustomization.PatchesStrategicMerge))
b, err := yaml.Marshal(kustomization)
if err != nil {
return errors.Wrap(err, "failed to marshal kustomization")
}
if err := ioutil.WriteFile(file, b, 0644); err != nil {
return errors.Wrap(err, "failed to write kustomization file")
}
return nil
}