-
Notifications
You must be signed in to change notification settings - Fork 88
/
app.go
232 lines (193 loc) · 6.67 KB
/
app.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package kustomize
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/base"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/util"
)
type WriteOptions struct {
BaseDir string
OverlaysDir string
RenderedDir string
Downstreams []string
KustomizeBinPath string
}
func WriteRenderedApp(options WriteOptions) error {
// cleanup existing rendered content if any
_, err := os.Stat(options.RenderedDir)
if err == nil {
if err := os.RemoveAll(options.RenderedDir); err != nil {
return errors.Wrap(err, "failed to remove existing rendered content")
}
}
for _, downstreamName := range options.Downstreams {
kustomizeBuildTarget := filepath.Join(options.OverlaysDir, "downstreams", downstreamName)
renderedApp, err := exec.Command(options.KustomizeBinPath, "build", kustomizeBuildTarget).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("kustomize stderr: %q", string(ee.Stderr))
}
return errors.Wrap(err, "failed to run kustomize build")
}
renderedAppFiles, err := util.SplitYAML(renderedApp)
if err != nil {
return errors.Wrap(err, "failed to split yaml")
}
for filename, content := range renderedAppFiles {
destPath := filepath.Join(options.RenderedDir, downstreamName, filename)
if err := writeRenderedFile(destPath, []byte(content)); err != nil {
return errors.Wrapf(err, "failed to write rendered app file %s", destPath)
}
}
_, renderedChartsFiles, err := RenderChartsArchive(options.BaseDir, options.OverlaysDir, downstreamName, options.KustomizeBinPath)
if err != nil {
return errors.Wrap(err, "failed to render charts archive")
}
for relPath, content := range renderedChartsFiles {
destPath := filepath.Join(options.RenderedDir, downstreamName, "charts", relPath)
if err := writeRenderedFile(destPath, []byte(content)); err != nil {
return errors.Wrapf(err, "failed to write rendered chart file %s", destPath)
}
}
}
return nil
}
func writeRenderedFile(destPath string, content []byte) error {
parentDir := filepath.Dir(destPath)
if err := os.MkdirAll(parentDir, 0755); err != nil {
return errors.Wrapf(err, "failed to mkdir %s", parentDir)
}
if err := ioutil.WriteFile(destPath, content, 0644); err != nil {
return errors.Wrap(err, "failed to write file")
}
return nil
}
func GetRenderedApp(versionArchive string, downstreamName, kustomizeBinPath string) ([]byte, map[string][]byte, error) {
// check if the app is already rendered
renderedAppDir := filepath.Join(versionArchive, "rendered", downstreamName)
if _, err := os.Stat(renderedAppDir); err == nil {
allContent := [][]byte{}
filesMap := map[string][]byte{}
err := filepath.Walk(renderedAppDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relPath, err := filepath.Rel(renderedAppDir, path)
if err != nil {
return errors.Wrapf(err, "failed to get relative path for %s", path)
}
// the charts directory includes helm charts to be installed using the helm cli,
// and those are processed separately.
if strings.Split(relPath, string(os.PathSeparator))[0] == "charts" {
return nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "failed to read file %s", path)
}
allContent = append(allContent, content)
filesMap[relPath] = content
return nil
})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to walk dir")
}
return bytes.Join(allContent, []byte("\n---\n")), filesMap, nil
}
baseDir := filepath.Join(versionArchive, "base")
filter := filterChartsInBasePath(baseDir)
if err := cleanBaseApp(baseDir, filter); err != nil {
return nil, nil, errors.Wrap(err, "failed to clean base app")
}
// older kots versions did not include the rendered app in the archive, so we have to render it
kustomizeBuildTarget := filepath.Join(versionArchive, "overlays", "downstreams", downstreamName)
allContent, err := exec.Command(kustomizeBinPath, "build", kustomizeBuildTarget).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return nil, nil, fmt.Errorf("kustomize stderr: %q", string(ee.Stderr))
}
return nil, nil, errors.Wrap(err, "failed to run kustomize build")
}
filesMap, err := util.SplitYAML(allContent)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to split yaml")
}
return allContent, filesMap, nil
}
// cleanBaseApp iterates over the base files and removes any files with nil map entries
// this does not include helm charts, which are processed separately.
// an optional filter can be passed to skip files that should not be removed.
//
// workaround for: https://github.com/kubernetes-sigs/kustomize/issues/5050
func cleanBaseApp(baseDir string, filter func(path string) (bool, error)) error {
if _, err := os.Stat(baseDir); err == nil {
err := filepath.Walk(baseDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filter != nil {
shouldFilter, err := filter(path)
if err != nil {
return errors.Wrapf(err, "failed to filter path %s", path)
}
if shouldFilter {
return nil
}
}
content, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "failed to read file %s", path)
}
_, manifest := base.GetGVKWithNameAndNs(content, "")
if manifest.APIVersion == "" || manifest.Kind == "" || manifest.Metadata.Name == "" {
// ignore invalid resources
return nil
}
if manifest.Kind == "CustomResourceDefinition" {
// ignore crds
return nil
}
newContent, err := kotsutil.RemoveNilFieldsFromYAML(content)
if err != nil {
return errors.Wrapf(err, "failed to remove empty mapping fields from %s", path)
}
if err := os.WriteFile(path, newContent, 0644); err != nil {
return errors.Wrapf(err, "failed to write file %s", path)
}
return nil
})
if err != nil {
return errors.Wrap(err, "failed to walk dir")
}
}
return nil
}
// filterChartsInBasePath returns a filter that can be passed to cleanBaseApp
// to skip files in the charts directory
func filterChartsInBasePath(basePath string) func(path string) (bool, error) {
return func(path string) (bool, error) {
relPath, err := filepath.Rel(basePath, path)
if err != nil {
return false, errors.Wrapf(err, "failed to get relative path for %s", path)
}
if strings.Split(relPath, string(os.PathSeparator))[0] == "charts" {
return true, nil
}
return false, nil
}
}