-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathcharts.go
332 lines (280 loc) · 7.91 KB
/
charts.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
package helm
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Masterminds/semver"
"sigs.k8s.io/yaml"
)
var (
// https://regex101.com/r/7xFFtU/3
chartExp = regexp.MustCompile(`^(?P<chart>\w+\/.+)@(?P<version>[^:\n\s]+)(?:\:(?P<path>[\w-. ]+))?$`)
repoExp = regexp.MustCompile(`^\w+$`)
)
// LoadChartfile opens a Chartfile tree
func LoadChartfile(projectRoot string) (*Charts, error) {
// make sure project root is valid
abs, err := filepath.Abs(projectRoot)
if err != nil {
return nil, err
}
// open chartfile
chartfile := filepath.Join(abs, Filename)
data, err := os.ReadFile(chartfile)
if err != nil {
return nil, err
}
// parse it
c := Chartfile{
Version: Version,
Directory: DefaultDir,
}
if err := yaml.UnmarshalStrict(data, &c); err != nil {
return nil, err
}
for i, r := range c.Requires {
if r.Chart == "" {
return nil, fmt.Errorf("requirements[%v]: 'chart' must be set", i)
}
}
// return Charts handle
charts := &Charts{
Manifest: c,
projectRoot: abs,
// default to ExecHelm, but allow injecting from the outside
Helm: ExecHelm{},
}
return charts, nil
}
// Charts exposes the central Chartfile management functions
type Charts struct {
// Manifest are the chartfile.yaml contents. It holds data about the developers intentions
Manifest Chartfile
// projectRoot is the enclosing directory of chartfile.yaml
projectRoot string
// Helm is the helm implementation underneath. ExecHelm is the default, but
// any implementation of the Helm interface may be used
Helm Helm
}
// chartManifest represents a Helm chart's Chart.yaml
type chartManifest struct {
Name string `yaml:"name"`
Version semver.Version `yaml:"version"`
}
// ChartDir returns the directory pulled charts are saved in
func (c Charts) ChartDir() string {
return filepath.Join(c.projectRoot, c.Manifest.Directory)
}
// ManifestFile returns the full path to the chartfile.yaml
func (c Charts) ManifestFile() string {
return filepath.Join(c.projectRoot, Filename)
}
// Vendor pulls all Charts specified in the manifest into the local charts
// directory. It fetches the repository index before doing so.
func (c Charts) Vendor(prune bool) error {
dir := c.ChartDir()
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
// Check that there are no output conflicts before vendoring
if err := c.Manifest.Requires.CheckForOutputConflicts(); err != nil {
return err
}
expectedDirs := make(map[string]bool)
repositoriesUpdated := false
log.Println("Vendoring...")
for _, r := range c.Manifest.Requires {
chartSubDir := parseReqName(r.Chart)
if r.Directory != "" {
chartSubDir = r.Directory
}
chartPath := filepath.Join(dir, chartSubDir)
expectedDirs[chartSubDir] = true
_, err := os.Stat(chartPath)
if err == nil {
chartManifestPath := filepath.Join(chartPath, "Chart.yaml")
chartManifestBytes, err := os.ReadFile(chartManifestPath)
if err != nil {
return fmt.Errorf("reading chart manifest: %w", err)
}
var chartYAML chartManifest
if err := yaml.Unmarshal(chartManifestBytes, &chartYAML); err != nil {
return fmt.Errorf("unmarshalling chart manifest: %w", err)
}
if chartYAML.Version.String() == r.Version.String() {
log.Printf(" %s exists", r)
continue
} else {
log.Printf("Removing %s", r)
if err := os.RemoveAll(chartPath); err != nil {
return err
}
}
} else if !os.IsNotExist(err) {
return err
}
if !repositoriesUpdated {
log.Println("Syncing Repositories ...")
if err := c.Helm.RepoUpdate(Opts{Repositories: c.Manifest.Repositories}); err != nil {
return err
}
repositoriesUpdated = true
}
log.Println("Pulling Charts ...")
if repoName := parseReqRepo(r.Chart); !c.Manifest.Repositories.HasName(repoName) {
return fmt.Errorf("repository %q not found for chart %q", repoName, r.Chart)
}
err = c.Helm.Pull(r.Chart, r.Version.String(), PullOpts{
Destination: dir,
ExtractDirectory: r.Directory,
Opts: Opts{Repositories: c.Manifest.Repositories},
})
if err != nil {
return err
}
log.Printf(" %s@%s downloaded", r.Chart, r.Version.String())
}
if prune {
items, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("error listing the content of the charts dir: %w", err)
}
for _, i := range items {
if !expectedDirs[i.Name()] {
itemType := "file"
if i.IsDir() {
itemType = "directory"
}
log.Printf("Pruning %s: %s", itemType, i.Name())
if err := os.RemoveAll(filepath.Join(dir, i.Name())); err != nil {
return err
}
}
}
}
return nil
}
// Add adds every Chart in reqs to the Manifest after validation, and runs
// Vendor afterwards
func (c *Charts) Add(reqs []string) error {
log.Printf("Adding %v Charts ...", len(reqs))
// parse new charts, append in memory
requirements := c.Manifest.Requires
for _, s := range reqs {
r, err := parseReq(s)
if err != nil {
skip(s, err)
continue
}
if requirements.Has(*r) {
skip(s, fmt.Errorf("already exists"))
continue
}
requirements = append(requirements, *r)
log.Println(" OK:", s)
}
if err := requirements.CheckForOutputConflicts(); err != nil {
return err
}
// write out
added := len(requirements) - len(c.Manifest.Requires)
c.Manifest.Requires = requirements
if err := write(c.Manifest, c.ManifestFile()); err != nil {
return err
}
// skipped some? fail then
if added != len(reqs) {
return fmt.Errorf("%v Chart(s) were skipped. Please check above logs for details", len(reqs)-added)
}
// worked fine? vendor it
log.Printf("Added %v Charts to helmfile.yaml. Vendoring ...", added)
return c.Vendor(false)
}
func (c *Charts) AddRepos(repos ...Repo) error {
added := 0
for _, r := range repos {
if c.Manifest.Repositories.Has(r) {
skip(r.Name, fmt.Errorf("already exists"))
continue
}
if !repoExp.MatchString(r.Name) {
skip(r.Name, fmt.Errorf("invalid name. cannot contain any special characters"))
continue
}
c.Manifest.Repositories = append(c.Manifest.Repositories, r)
added++
log.Println(" OK:", r.Name)
}
// write out
if err := write(c.Manifest, c.ManifestFile()); err != nil {
return err
}
if added != len(repos) {
return fmt.Errorf("%v Repo(s) were skipped. Please check above logs for details", len(repos)-added)
}
return nil
}
func InitChartfile(path string) (*Charts, error) {
c := Chartfile{
Version: Version,
Repositories: []Repo{{
Name: "stable",
URL: "https://charts.helm.sh/stable",
}},
Requires: make(Requirements, 0),
}
if err := write(c, path); err != nil {
return nil, err
}
return LoadChartfile(filepath.Dir(path))
}
// write saves a Chartfile to dest
func write(c Chartfile, dest string) error {
data, err := yaml.Marshal(c)
if err != nil {
return err
}
return os.WriteFile(dest, data, 0644)
}
// parseReq parses a requirement from a string of the format `repo/name@version`
func parseReq(s string) (*Requirement, error) {
matches := chartExp.FindStringSubmatch(s)
if matches == nil {
return nil, fmt.Errorf("not of form 'repo/chart@version(:path)' where repo contains no special characters")
}
chart := matches[1]
ver, err := semver.NewVersion(matches[2])
if errors.Is(err, semver.ErrInvalidSemVer) {
return nil, fmt.Errorf("version is invalid: %s", matches[2])
} else if err != nil {
return nil, fmt.Errorf("error parsing semver: %s", err)
}
directory := ""
if len(matches) == 4 {
directory = matches[3]
}
return &Requirement{
Chart: chart,
Version: *ver,
Directory: directory,
}, nil
}
// parseReqRepo parses a repo from a string of the format `repo/name`
func parseReqRepo(s string) string {
elems := strings.SplitN(s, "/", 2)
repo := elems[0]
return repo
}
// parseReqName parses a name from a string of the format `repo/name`
func parseReqName(s string) string {
elems := strings.SplitN(s, "/", 2)
name := elems[1]
return name
}
func skip(s string, err error) {
log.Printf(" Skipping %s: %s.", s, err)
}