-
Notifications
You must be signed in to change notification settings - Fork 18
/
manifests.go
363 lines (313 loc) · 9.26 KB
/
manifests.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package helmchart
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/solo-io/go-utils/installutils"
"github.com/solo-io/go-utils/installutils/helmignore"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/releaseutil"
"sigs.k8s.io/yaml"
"github.com/google/go-github/v32/github"
"github.com/solo-io/go-utils/tarutils"
"github.com/solo-io/go-utils/vfsutils"
"github.com/spf13/afero"
"github.com/solo-io/go-utils/contextutils"
"k8s.io/apimachinery/pkg/runtime"
yaml2json "k8s.io/apimachinery/pkg/util/yaml"
"github.com/solo-io/go-utils/installutils/kuberesource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type Manifests []releaseutil.Manifest
func (m Manifests) Find(name string) *releaseutil.Manifest {
for _, man := range m {
if man.Name == name {
return &man
}
}
return nil
}
func (m Manifests) Names() []string {
var names []string
for _, m := range sortByKind(m) {
names = append(names, m.Name)
}
return names
}
func (m Manifests) CombinedString() string {
buf := &bytes.Buffer{}
for _, m := range sortByKind(m) {
data := m.Content
b := filepath.Base(m.Name)
if b == "NOTES.txt" {
continue
}
if strings.HasPrefix(b, "_") {
continue
}
fmt.Fprintf(buf, "---\n# Source: %s\n", m.Name)
fmt.Fprintln(buf, data)
}
return buf.String()
}
var yamlSeparator = regexp.MustCompile("\n---")
func (m Manifests) ResourceList() (kuberesource.UnstructuredResources, error) {
snippets := yamlSeparator.Split(m.CombinedString(), -1)
var resources kuberesource.UnstructuredResources
for _, objectYaml := range snippets {
if IsEmptyManifest(objectYaml) {
continue
}
jsn, err := yaml2json.ToJSON([]byte(objectYaml))
if err != nil {
return nil, err
}
uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, jsn)
if err != nil {
return nil, err
}
if resourceList, ok := uncastObj.(*unstructured.UnstructuredList); ok {
for _, item := range resourceList.Items {
resources = append(resources, &item)
}
continue
}
resources = append(resources, uncastObj.(*unstructured.Unstructured))
}
return resources, nil
}
func ManifestsFromResources(resources kuberesource.UnstructuredResources) (Manifests, error) {
var resourceYamls []string
for _, res := range resources {
rawJson, err := runtime.Encode(unstructured.UnstructuredJSONScheme, res)
if err != nil {
return nil, err
}
yam, err := yaml.JSONToYAML(rawJson)
if err != nil {
return nil, err
}
resourceYamls = append(resourceYamls, string(yam))
}
return Manifests{{Head: &releaseutil.SimpleHead{}, Content: strings.Join(resourceYamls, "\n---\n")}}, nil
}
var commentRegex = regexp.MustCompile("#.*")
func IsEmptyManifest(manifest string) bool {
removeComments := commentRegex.ReplaceAllString(manifest, "")
removeNewlines := strings.Replace(removeComments, "\n", "", -1)
removeDashes := strings.Replace(removeNewlines, "---", "", -1)
removeSpaces := strings.Replace(removeDashes, " ", "", -1)
return removeSpaces == ""
}
func RenderManifests(ctx context.Context, chartUri, values, releaseName, namespace, kubeVersion string) (Manifests, error) {
file, err := tarutils.RetrieveArchive(afero.NewOsFs(), chartUri)
if err != nil {
return nil, err
}
defer file.Close()
// Check chart requirements to make sure all dependencies are present in /charts
chart, err := loader.LoadArchive(file)
if err != nil {
return nil, errors.Wrapf(err, "loading chart")
}
return renderManifests(ctx, chart, values, releaseName, namespace, kubeVersion)
}
func renderManifests(ctx context.Context, c *chart.Chart, values, releaseName, namespace, kubeVersion string) ([]releaseutil.Manifest, error) {
valuesYaml, err := chartutil.ReadValues([]byte(values))
if err != nil {
return nil, err
}
chartValues, err := chartutil.ToRenderValues(c, valuesYaml, chartutil.ReleaseOptions{
Name: releaseName,
Namespace: namespace,
}, nil)
if err != nil {
return nil, err
}
renderedTemplates, err := engine.Render(c, chartValues)
if err != nil {
return nil, err
}
for file, man := range renderedTemplates {
if IsEmptyManifest(man) {
contextutils.LoggerFrom(ctx).Debugf("is an empty manifest, removing %v", file)
delete(renderedTemplates, file)
}
}
manifests := installutils.SplitManifests(renderedTemplates)
return sortByKind(manifests), nil
}
type GithubChartRef struct {
Owner string
Repo string
Ref string
ChartDirectory string
}
func RenderChartsFromGithub(ctx context.Context, parentRef GithubChartRef) (map[string]*chart.Chart, error) {
fs := afero.NewMemMapFs()
codeDir, err := vfsutils.MountCode(fs, ctx, github.NewClient(nil), parentRef.Owner, parentRef.Repo, parentRef.Ref)
if err != nil {
return nil, err
}
defer fs.Remove(codeDir)
chartParent := filepath.Join(codeDir, parentRef.ChartDirectory)
subdirs, err := afero.ReadDir(fs, chartParent)
if err != nil {
return nil, err
}
charts := make(map[string]*chart.Chart)
for _, subdir := range subdirs {
chartRoot := filepath.Join(chartParent, subdir.Name())
rules, err := getRulesFromArchive(fs, chartRoot)
if err != nil {
return nil, err
}
chart, err := loadFiles(rules, fs, chartRoot+"/")
if err != nil {
return nil, err
}
charts[subdir.Name()] = chart
}
return charts, nil
}
func RenderChartFromGithub(ctx context.Context, ref GithubChartRef) (*chart.Chart, error) {
fs := afero.NewMemMapFs()
codeDir, err := vfsutils.MountCode(fs, ctx, github.NewClient(nil), ref.Owner, ref.Repo, ref.Ref)
if err != nil {
return nil, err
}
defer fs.Remove(codeDir)
chartRoot := filepath.Join(codeDir, ref.ChartDirectory)
rules, err := getRulesFromArchive(fs, chartRoot)
if err != nil {
return nil, err
}
return loadFiles(rules, fs, chartRoot+"/")
}
func RenderManifestsFromGithub(ctx context.Context, ref GithubChartRef, values, releaseName, namespace, kubeVersion string) ([]releaseutil.Manifest, error) {
chart, err := RenderChartFromGithub(ctx, ref)
if err != nil {
return nil, err
}
return renderManifests(ctx, chart, values, releaseName, namespace, kubeVersion)
}
func getRulesFromArchive(fs afero.Fs, chartRoot string) (*helmignore.Rules, error) {
rules := helmignore.Empty()
helmignorePath := filepath.Join(chartRoot, helmignore.HelmIgnore)
exists, err := afero.Exists(fs, helmignorePath)
if err != nil {
return nil, errors.Wrapf(err, "error checking if helmignore exists")
}
if exists {
contents, err := afero.ReadFile(fs, helmignorePath)
if err != nil {
return nil, errors.Wrapf(err, "error reading helmignore")
}
reader := bytes.NewReader(contents)
r, err := helmignore.Parse(reader)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse .helmignore")
}
rules = r
}
rules.AddDefaults()
return rules, nil
}
func loadFiles(rules *helmignore.Rules, fs afero.Fs, chartDir string) (*chart.Chart, error) {
var files []*loader.BufferedFile
walk := func(name string, fi os.FileInfo, err error) error {
n := strings.TrimPrefix(name, chartDir)
if n == "" {
// No need to process top level. Avoid bug with helmignore .* matching
// empty names. See issue 1779.
return nil
}
// Normalize to / since it will also work on Windows
n = filepath.ToSlash(n)
if err != nil {
return err
}
if fi.IsDir() {
// Directory-based ignore rules should involve skipping the entire
// contents of that directory.
if rules.Ignore(n, fi) {
return filepath.SkipDir
}
return nil
}
// If a .helmignore file matches, skip this file.
if rules.Ignore(n, fi) {
return nil
}
data, err := afero.ReadFile(fs, name)
if err != nil {
return fmt.Errorf("error reading %s: %s", n, err)
}
files = append(files, &loader.BufferedFile{Name: n, Data: data})
return nil
}
if err := afero.Walk(fs, chartDir, walk); err != nil {
return nil, err
}
return loader.LoadFiles(files)
}
// adapted from Helm 2
// https://github.com/helm/helm/blob/release-2.16/pkg/tiller/kind_sorter.go#L113
type kindSorter struct {
ordering map[string]int
manifests []releaseutil.Manifest
}
func newKindSorter(m []releaseutil.Manifest, s []string) *kindSorter {
o := make(map[string]int, len(s))
for v, k := range s {
o[k] = v
}
return &kindSorter{
manifests: m,
ordering: o,
}
}
func (k *kindSorter) Len() int { return len(k.manifests) }
func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifests[j], k.manifests[i] }
func (k *kindSorter) Less(i, j int) bool {
a := k.manifests[i]
b := k.manifests[j]
first, aok := k.ordering[a.Head.Kind]
second, bok := k.ordering[b.Head.Kind]
if !aok && !bok {
// if both are unknown then sort alphabetically by kind and name
if a.Head.Kind != b.Head.Kind {
return a.Head.Kind < b.Head.Kind
}
return a.Name < b.Name
}
// unknown kind is last
if !aok {
return false
}
if !bok {
return true
}
// if same kind sub sort alphanumeric
if first == second {
return a.Name < b.Name
}
// sort different kinds
return first < second
}
// SortByKind sorts manifests in InstallOrder
func sortByKind(manifests []releaseutil.Manifest) []releaseutil.Manifest {
ordering := kuberesource.InstallOrder
ks := newKindSorter(manifests, ordering)
sort.Sort(ks)
return ks.manifests
}