forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm_helpers.go
202 lines (183 loc) · 5.51 KB
/
helm_helpers.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
package helm
import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/util"
"k8s.io/helm/pkg/chartutil"
)
const (
RequirementsFileName = "requirements.yaml"
DefaultHelmRepositoryURL = "http://jenkins-x-chartmuseum:8080"
defaultEnvironmentChartDir = "env"
)
// copied from helm to minimise dependencies...
// Dependency describes a chart upon which another chart depends.
//
// Dependencies can be used to express developer intent, or to capture the state
// of a chart.
type Dependency struct {
// Name is the name of the dependency.
//
// This must mach the name in the dependency's Chart.yaml.
Name string `json:"name"`
// Version is the version (range) of this chart.
//
// A lock file will always produce a single version, while a dependency
// may contain a semantic version range.
Version string `json:"version,omitempty"`
// The URL to the repository.
//
// Appending `index.yaml` to this string should result in a URL that can be
// used to fetch the repository index.
Repository string `json:"repository"`
// A yaml path that resolves to a boolean, used for enabling/disabling charts (e.g. subchart1.enabled )
Condition string `json:"condition,omitempty"`
// Tags can be used to group charts for enabling/disabling together
Tags []string `json:"tags,omitempty"`
// Enabled bool determines if chart should be loaded
Enabled bool `json:"enabled,omitempty"`
// ImportValues holds the mapping of source values to parent key to be imported. Each item can be a
// string or pair of child/parent sublist items.
ImportValues []interface{} `json:"import-values,omitempty"`
// Alias usable alias to be used for the chart
Alias string `json:"alias,omitempty"`
}
// ErrNoRequirementsFile to detect error condition
type ErrNoRequirementsFile error
// Requirements is a list of requirements for a chart.
//
// Requirements are charts upon which this chart depends. This expresses
// developer intent.
type Requirements struct {
Dependencies []*Dependency `json:"dependencies"`
}
// DepSorter Used to avoid merge conflicts by sorting deps by name
type DepSorter []*Dependency
func (a DepSorter) Len() int { return len(a) }
func (a DepSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a DepSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }
// SetAppVersion sets the version of the app to use
func (r *Requirements) SetAppVersion(app string, version string, repository string, alias string) {
if r.Dependencies == nil {
r.Dependencies = []*Dependency{}
}
for _, dep := range r.Dependencies {
if dep != nil && dep.Name == app {
dep.Version = version
dep.Repository = repository
dep.Alias = alias
return
}
}
r.Dependencies = append(r.Dependencies, &Dependency{
Name: app,
Version: version,
Repository: repository,
Alias: alias,
})
sort.Sort(DepSorter(r.Dependencies))
}
// RemoveApp removes the given app name. Returns true if a dependency was removed
func (r *Requirements) RemoveApp(app string) bool {
for i, dep := range r.Dependencies {
if dep != nil && dep.Name == app {
r.Dependencies = append(r.Dependencies[:i], r.Dependencies[i+1:]...)
sort.Sort(DepSorter(r.Dependencies))
return true
}
}
return false
}
// FindRequirementsFileName returns the default requirements.yaml file name
func FindRequirementsFileName(dir string) (string, error) {
names := []string{
filepath.Join(dir, defaultEnvironmentChartDir, RequirementsFileName),
filepath.Join(dir, RequirementsFileName),
}
for _, name := range names {
exists, err := util.FileExists(name)
if err != nil {
return "", err
}
if exists {
return name, nil
}
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, f := range files {
if f.IsDir() {
name := filepath.Join(dir, f.Name(), RequirementsFileName)
exists, err := util.FileExists(name)
if err != nil {
return "", err
}
if exists {
return name, nil
}
}
}
dirs := []string{
filepath.Join(dir, defaultEnvironmentChartDir),
dir,
}
for _, d := range dirs {
name := filepath.Join(d, RequirementsFileName)
exists, err := util.FileExists(d)
if err != nil {
return "", err
}
if exists {
return name, nil
}
}
return "", fmt.Errorf("Could not deduce the default requirements.yaml file name")
}
// LoadRequirementsFile loads the requirements file or creates empty requirements if the file does not exist
func LoadRequirementsFile(fileName string) (*Requirements, error) {
exists, err := util.FileExists(fileName)
if err != nil {
return nil, err
}
if exists {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
return LoadRequirements(data)
}
r := &Requirements{}
return r, nil
}
// LoadRequirements loads the requirements from some data
func LoadRequirements(data []byte) (*Requirements, error) {
r := &Requirements{}
return r, yaml.Unmarshal(data, r)
}
// SaveRequirementsFile saves the requirements file
func SaveRequirementsFile(fileName string, requirements *Requirements) error {
data, err := yaml.Marshal(requirements)
if err != nil {
return err
}
return ioutil.WriteFile(fileName, data, util.DefaultWritePermissions)
}
func LoadChartName(chartFile string) (string, error) {
chart, err := chartutil.LoadChartfile(chartFile)
if err != nil {
return "", err
}
return chart.Name, nil
}
func LoadChartNameAndVersion(chartFile string) (string, string, error) {
chart, err := chartutil.LoadChartfile(chartFile)
if err != nil {
return "", "", err
}
return chart.Name, chart.Version, nil
}