generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chart.go
250 lines (217 loc) · 7.78 KB
/
chart.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
package source
import (
"fmt"
"github.com/broadinstitute/thelma/internal/thelma/charts/semver"
"github.com/broadinstitute/thelma/internal/thelma/tools/helm"
"github.com/broadinstitute/thelma/internal/thelma/tools/yq"
"github.com/broadinstitute/thelma/internal/thelma/utils/shell"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
"io/ioutil"
"path"
"strings"
)
// initial version that is assigned to brand-new charts
const initialChartVersion = "0.1.0"
// binary used for running Helm Docs
const helmDocsProg = "helm-docs"
// repository prefix for local dependencies
const fileRepoPrefix = "file://.."
// name of Helm's chart manifest file
const chartManifestFile = "Chart.yaml"
// ChartManifest struct used to unmarshal Helm chart.yaml files.
type ChartManifest struct {
Name string
Version string
Dependencies []struct {
Name string
Repository string
Version string
}
}
// Chart represents a Helm chart source directory on the local filesystem.
type Chart interface {
// Name returns the name of this chart
Name() string
// Path returns the path to this chart on disk
Path() string
// BumpChartVersion updates chart version in chart.yaml
BumpChartVersion(latestPublishedVersion string) (string, error)
// UpdateDependencies runs `helm dependency update` on the local copy of the chart.
UpdateDependencies() error
// PackageChart runs `helm package` to package a chart
PackageChart(destPath string) error
// GenerateDocs re-generates README documentation for the given chart
GenerateDocs() error
// LocalDependencies returns the names of local dependencies / subcharts (using Helm's "file://" repo support)
LocalDependencies() []string
// SetDependencyVersion sets the version of a dependency in this chart's Chart.yaml
SetDependencyVersion(dependencyName string, newVersion string) error
// ManifestVersion returns the version of this chart in Chart.yaml
ManifestVersion() string
}
// Implements Chart interface
type chart struct {
name string // name of the chart
path string // path to the chart directory on the local filesystem
manifest ChartManifest // manifest parsed subset of chart.yaml
shellRunner shell.Runner // shell runner instance to use for executing commands
}
// NewChart constructs a Chart
func NewChart(chartSourceDir string, shellRunner shell.Runner) (Chart, error) {
manifestFile := path.Join(chartSourceDir, chartManifestFile)
manifest, err := loadManifest(manifestFile)
if err != nil {
return nil, err
}
return &chart{
name: manifest.Name,
path: path.Dir(manifestFile),
manifest: manifest,
shellRunner: shellRunner,
}, nil
}
// Name of thist chart
func (c *chart) Name() string {
return c.name
}
// Path to the chart on the filesystem
func (c *chart) Path() string {
return c.path
}
// BumpChartVersion update chart version in chart.yaml
func (c *chart) BumpChartVersion(latestPublishedVersion string) (string, error) {
nextVersion := c.nextVersion(latestPublishedVersion)
expression := fmt.Sprintf(".version = %q", nextVersion)
manifestFile := path.Join(c.path, chartManifestFile)
if err := yq.New(c.shellRunner).Write(expression, manifestFile); err != nil {
return nextVersion, err
}
if err := c.reloadManifest(); err != nil {
return nextVersion, err
}
if c.manifest.Version != nextVersion {
return nextVersion, fmt.Errorf("error updating %s chart version to %s in %s: version is still %s after update", c.name, nextVersion, manifestFile, c.manifest.Version)
}
return nextVersion, nil
}
// BuildDependencies runs `helm dependency update` on the local copy of the chart.
func (c *chart) UpdateDependencies() error {
cmd := shell.Command{
Prog: helm.ProgName,
Args: []string{
"dependency",
"update",
"--skip-refresh",
},
Dir: c.path,
}
return c.shellRunner.Run(cmd)
}
// PackageChart runs `helm package` to package a chart
func (c *chart) PackageChart(destPath string) error {
cmd := shell.Command{
Prog: helm.ProgName,
Args: []string{
"package",
".",
"--destination",
destPath,
},
Dir: c.path,
}
return c.shellRunner.Run(cmd)
}
// GenerateDocs re-generates README documentation for the given chart
func (c *chart) GenerateDocs() error {
cmd := shell.Command{
Prog: helmDocsProg,
Args: []string{
".",
},
Dir: c.path,
}
return c.shellRunner.Run(cmd)
}
func (c *chart) LocalDependencies() []string {
var dependencies []string
for _, dependency := range c.manifest.Dependencies {
log.Debug().Msgf("processing chart %s dependency: %v", c.name, dependency)
if !strings.HasPrefix(dependency.Repository, fileRepoPrefix) {
log.Debug().Msgf("dependency %s is not from a %s repository, ignoring", dependency.Name, fileRepoPrefix)
continue
}
dependencies = append(dependencies, dependency.Name)
}
return dependencies
}
func (c *chart) SetDependencyVersion(dependencyName string, newVersion string) error {
expression := fmt.Sprintf(`(.dependencies.[] | select(.name == %q) | .version) |= %q`, dependencyName, newVersion)
manifestFile := path.Join(c.path, chartManifestFile)
if err := yq.New(c.shellRunner).Write(expression, manifestFile); err != nil {
return err
}
if err := c.reloadManifest(); err != nil {
return err
}
for _, dependency := range c.manifest.Dependencies {
if dependency.Name == dependencyName {
if dependency.Version == newVersion {
log.Debug().Msgf("updated version for dependency %s to %s in %s", dependencyName, newVersion, manifestFile)
return nil
} else {
return fmt.Errorf("error setting dependency %s to version %s in %s: dependency not found", dependencyName, newVersion, manifestFile)
}
}
}
return fmt.Errorf("error setting dependency %s to version %s in %s: dependency not found", dependencyName, newVersion, manifestFile)
}
func (c *chart) ManifestVersion() string {
return c.manifest.Version
}
func (c *chart) nextVersion(latestPublishedVersion string) string {
sourceVersion := c.manifest.Version
nextPublishedVersion, err := semver.MinorBump(latestPublishedVersion)
if err != nil {
log.Debug().Msgf("chart %s: could not determine next minor version for chart: %v", c.name, err)
if !semver.IsValid(sourceVersion) {
log.Debug().Msgf("chart %s: version in chart.yaml is invalid: %q", c.name, sourceVersion)
log.Debug().Msgf("chart %s: falling back to default initial chart version: %q", c.name, initialChartVersion)
return initialChartVersion
}
log.Debug().Msgf("chart %s: falling back to source version %q", c.name, sourceVersion)
return sourceVersion
}
if !semver.IsValid(sourceVersion) {
log.Debug().Msgf("chart %s: version in chart.yaml is invalid: %q", c.name, sourceVersion)
log.Debug().Msgf("chart %s: will set to next computed version %q", c.name, nextPublishedVersion)
return nextPublishedVersion
}
if semver.Compare(sourceVersion, nextPublishedVersion) > 0 {
log.Debug().Msgf("chart %s: source version %q > next computed version %q, will use source version", c.name, sourceVersion, nextPublishedVersion)
return sourceVersion
}
return nextPublishedVersion
}
func (c *chart) reloadManifest() error {
manifestFile := path.Join(c.path, chartManifestFile)
manifest, err := loadManifest(manifestFile)
if err != nil {
return fmt.Errorf("manifest reload failed: %v", err)
}
log.Debug().Msgf("chart %s: reloaded manifest file %s", c.name, manifestFile)
c.manifest = manifest
return nil
}
func loadManifest(manifestFile string) (ChartManifest, error) {
var manifest ChartManifest
content, err := ioutil.ReadFile(manifestFile)
if err != nil {
return manifest, fmt.Errorf("error reading chart manifest %s: %v", manifestFile, err)
}
if err := yaml.Unmarshal(content, &manifest); err != nil {
return manifest, fmt.Errorf("error parsing chart manifest %s: %v", manifestFile, err)
}
log.Debug().Msgf("loaded chart manifest from %s: %v", manifestFile, manifest)
return manifest, nil
}