-
Notifications
You must be signed in to change notification settings - Fork 88
/
write.go
190 lines (155 loc) · 4.74 KB
/
write.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
package base
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
kustomizetypes "sigs.k8s.io/kustomize/api/types"
)
type WriteOptions struct {
BaseDir string
Overwrite bool
ExcludeKotsKinds bool
}
func (b *Base) WriteBase(options WriteOptions) error {
renderDir := filepath.Join(options.BaseDir, b.Path)
_, err := os.Stat(renderDir)
if err == nil {
if options.Overwrite {
if err := os.RemoveAll(renderDir); err != nil {
return errors.Wrap(err, "failed to remove previous content in base")
}
} else {
return fmt.Errorf("directory %s already exists", renderDir)
}
}
if _, err := os.Stat(renderDir); os.IsNotExist(err) {
if err := os.MkdirAll(renderDir, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir for base root")
}
}
resources, patches, err := deduplicateOnContent(b.Files, options.ExcludeKotsKinds)
if err != nil {
return errors.Wrap(err, "failed to deduplicate content")
}
kustomizeResources := []string{}
kustomizePatches := []kustomizetypes.PatchStrategicMerge{}
kustomizeBases := []string{}
for _, file := range resources {
fileRenderPath := path.Join(renderDir, file.Path)
d, _ := path.Split(fileRenderPath)
if _, err := os.Stat(d); os.IsNotExist(err) {
if err := os.MkdirAll(d, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
}
if err := ioutil.WriteFile(fileRenderPath, file.Content, 0644); err != nil {
return errors.Wrap(err, "failed to write base file")
}
kustomizeResources = append(kustomizeResources, path.Join(".", file.Path))
}
for _, file := range patches {
fileRenderPath := path.Join(renderDir, file.Path)
d, _ := path.Split(fileRenderPath)
if _, err := os.Stat(d); os.IsNotExist(err) {
if err := os.MkdirAll(d, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
}
if err := ioutil.WriteFile(fileRenderPath, file.Content, 0644); err != nil {
return errors.Wrap(err, "failed to write base file")
}
kustomizePatches = append(kustomizePatches, kustomizetypes.PatchStrategicMerge(path.Join(".", file.Path)))
}
for _, base := range b.Bases {
if base.Path == "" {
return errors.New("kustomize sub-base path cannot be empty")
}
if err := base.WriteBase(options); err != nil {
return errors.Wrapf(err, "failed to render base %q", base.Path)
}
kustomizeBases = append(kustomizeBases, base.Path)
}
kustomization := kustomizetypes.Kustomization{
TypeMeta: kustomizetypes.TypeMeta{
APIVersion: "kustomize.config.k8s.io/v1beta1",
Kind: "Kustomization",
},
Resources: kustomizeResources,
PatchesStrategicMerge: kustomizePatches,
Bases: kustomizeBases,
}
if b.Namespace != "" {
kustomization.Namespace = b.Namespace
}
if err := k8sutil.WriteKustomizationToFile(&kustomization, path.Join(renderDir, "kustomization.yaml")); err != nil {
return errors.Wrap(err, "failed to write kustomization to file")
}
return nil
}
func deduplicateOnContent(files []BaseFile, excludeKotsKinds bool) ([]BaseFile, []BaseFile, error) {
resources := []BaseFile{}
patches := []BaseFile{}
foundGVKNames := [][]byte{}
singleDocs := convertToSingleDocs(files)
for _, file := range singleDocs {
writeToKustomization, err := file.ShouldBeIncludedInBaseKustomization(excludeKotsKinds)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to check if file should be included")
}
if !writeToKustomization {
continue
}
if writeToKustomization {
found := false
thisGVKName := GetGVKWithNameHash(file.Content)
for _, gvkName := range foundGVKNames {
if bytes.Compare(gvkName, thisGVKName) == 0 {
found = true
}
}
if !found || thisGVKName == nil {
resources = append(resources, file)
foundGVKNames = append(foundGVKNames, thisGVKName)
} else {
patches = append(patches, file)
}
}
}
return resources, patches, nil
}
func convertToSingleDocs(files []BaseFile) []BaseFile {
singleDocs := []BaseFile{}
for _, file := range files {
docs := bytes.Split(file.Content, []byte("\n---\n"))
if len(docs) == 1 {
singleDocs = append(singleDocs, file)
continue
}
for idx, doc := range docs {
if len(bytes.TrimSpace(doc)) == 0 {
continue
}
filename := file.Path
if idx > 0 {
filename = strings.TrimSuffix(file.Path, filepath.Ext(file.Path))
filename = fmt.Sprintf("%s-%d%s", filename, idx+1, filepath.Ext(file.Path))
}
baseFile := BaseFile{
Path: filename,
Content: doc,
}
singleDocs = append(singleDocs, baseFile)
}
}
return singleDocs
}
func (b *Base) GetOverlaysDir(options WriteOptions) string {
renderDir := options.BaseDir
return path.Join(renderDir, "..", "overlays")
}