-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathcharts.go
215 lines (178 loc) · 4.85 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
package helm
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Masterminds/semver"
"sigs.k8s.io/yaml"
)
// 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 := ioutil.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
}
// 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() error {
dir := c.ChartDir()
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
log.Println("Syncing Repositories ...")
if err := c.Helm.RepoUpdate(Opts{Repositories: c.Manifest.Repositories}); err != nil {
return err
}
log.Println("Pulling Charts ...")
for _, r := range c.Manifest.Requires {
chartName := parseReqName(r.Chart)
chartPath := filepath.Join(dir, chartName)
if _, err := os.Stat(chartPath); err == nil {
log.Printf(" %s@%s exists", r.Chart, r.Version.String())
continue
}
err := c.Helm.Pull(r.Chart, r.Version.String(), PullOpts{
Destination: dir,
Opts: Opts{Repositories: c.Manifest.Repositories},
})
if err != nil {
return err
}
log.Printf(" %s@%s downloaded", r.Chart, r.Version.String())
}
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))
skip := func(s string, err error) {
log.Printf(" Skipping %s: %s.", s, err)
}
// parse new charts, append in memory
added := 0
for _, s := range reqs {
r, err := parseReq(s)
if err != nil {
skip(s, err)
continue
}
if c.Manifest.Requires.Has(*r) {
skip(s, fmt.Errorf("already exists"))
continue
}
c.Manifest.Requires = append(c.Manifest.Requires, *r)
added++
log.Println(" OK:", s)
}
// write out
if err := write(c.Manifest, c.ManifestFile()); err != nil {
return err
}
// skipped some? fail then
if added != len(reqs) {
return fmt.Errorf("%v Charts 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()
}
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 ioutil.WriteFile(dest, data, 0644)
}
var chartExp = regexp.MustCompile(`\w+\/.+@.+`)
// parseReq parses a requirement from a string of the format `repo/name@version`
func parseReq(s string) (*Requirement, error) {
if !chartExp.MatchString(s) {
return nil, fmt.Errorf("not of form 'repo/chart@version'")
}
elems := strings.Split(s, "@")
chart := elems[0]
ver, err := semver.NewVersion(elems[1])
if errors.Is(err, semver.ErrInvalidSemVer) {
return nil, fmt.Errorf("version is invalid")
} else if err != nil {
return nil, fmt.Errorf("version is invalid: %s", err)
}
return &Requirement{
Chart: chart,
Version: *ver,
}, nil
}
// parseReqName parses a name from a string of the format `repo/name`
func parseReqName(s string) string {
elems := strings.Split(s, "/")
name := elems[1]
return name
}