-
Notifications
You must be signed in to change notification settings - Fork 788
/
step_helm.go
267 lines (229 loc) · 8.72 KB
/
step_helm.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
package helm
import (
"fmt"
"path/filepath"
"text/template"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/pkg/errors"
"k8s.io/helm/pkg/chartutil"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
)
const (
PROW_JOB_ID = "PROW_JOB_ID"
REPO_OWNER = "REPO_OWNER"
REPO_NAME = "REPO_NAME"
PULL_PULL_SHA = "PULL_PULL_SHA"
)
// StepHelmOptions contains the command line flags
type StepHelmOptions struct {
step.StepOptions
Dir string
https bool
GitProvider string
versionResolver *versionstream.VersionResolver
}
// NewCmdStepHelm Steps a command object for the "step" command
func NewCmdStepHelm(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepHelmOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "helm",
Short: "helm [command]",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.AddCommand(NewCmdStepHelmApply(commonOpts))
cmd.AddCommand(NewCmdStepHelmBuild(commonOpts))
cmd.AddCommand(NewCmdStepHelmDelete(commonOpts))
cmd.AddCommand(NewCmdStepHelmEnv(commonOpts))
cmd.AddCommand(NewCmdStepHelmInstall(commonOpts))
cmd.AddCommand(NewCmdStepHelmList(commonOpts))
cmd.AddCommand(NewCmdStepHelmRelease(commonOpts))
cmd.AddCommand(NewCmdStepHelmVersion(commonOpts))
return cmd
}
// Run implements this command
func (o *StepHelmOptions) Run() error {
return o.Cmd.Help()
}
func (o *StepHelmOptions) addStepHelmFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "The directory containing the helm chart to apply")
cmd.Flags().BoolVarP(&o.https, "clone-https", "", true, "Clone the environment Git repo over https rather than ssh which uses `git@foo/bar.git`")
cmd.Flags().BoolVarP(&o.RemoteCluster, "remote", "", false, "If enabled assume we are in a remote cluster such as a stand alone Staging/Production cluster")
cmd.Flags().StringVarP(&o.GitProvider, "git-provider", "", "github.com", "The Git provider for the environment Git repository")
}
func (o *StepHelmOptions) discoverValuesFiles(dir string) ([]string, error) {
valuesFiles := []string{}
for _, name := range []string{"values.yaml", helm.SecretsFileName, "myvalues.yaml"} {
path := filepath.Join(dir, name)
exists, err := util.FileExists(path)
if err != nil {
return valuesFiles, err
}
if exists {
valuesFiles = append(valuesFiles, path)
}
}
return valuesFiles, nil
}
func (o *StepHelmOptions) getOrCreateVersionResolver(requirementsConfig *config.RequirementsConfig) (*versionstream.VersionResolver, error) {
if o.versionResolver == nil {
vs := requirementsConfig.VersionStream
var err error
o.versionResolver, err = o.CreateVersionResolver(vs.URL, vs.Ref)
if err != nil {
return o.versionResolver, errors.Wrapf(err, "failed to create version resolver")
}
}
return o.versionResolver, nil
}
func (o *StepHelmOptions) verifyRequirementsYAML(resolver *versionstream.VersionResolver, prefixes *versionstream.RepositoryPrefixes, fileName string) error {
req, err := helm.LoadRequirementsFile(fileName)
if err != nil {
return errors.Wrapf(err, "failed to load %s", fileName)
}
modified := false
for _, dep := range req.Dependencies {
if dep.Version == "" {
name := dep.Alias
if name == "" {
name = dep.Name
}
repo := dep.Repository
if repo == "" {
return fmt.Errorf("cannot to find a version for dependency %s in file %s as there is no 'repository'", name, fileName)
}
prefix := prefixes.PrefixForURL(repo)
if prefix == "" {
return fmt.Errorf("the helm repository %s does not have an associated prefix in in the 'charts/repositories.yml' file the version stream, so we cannot default the version in file %s", repo, fileName)
}
newVersion := ""
fullChartName := prefix + "/" + dep.Name
newVersion, err := resolver.StableVersionNumber(versionstream.KindChart, fullChartName)
if err != nil {
return errors.Wrapf(err, "failed to find version of chart %s in file %s", fullChartName, fileName)
}
if newVersion == "" {
return fmt.Errorf("failed to find a version for dependency %s in file %s in the current version stream - please either add an explicit version to this file or add chart %s to the version stream", name, fileName, fullChartName)
}
dep.Version = newVersion
modified = true
log.Logger().Debugf("adding version %s to dependency %s in file %s", newVersion, name, fileName)
}
}
if modified {
err = helm.SaveFile(fileName, req)
if err != nil {
return errors.Wrapf(err, "failed to save %s", fileName)
}
log.Logger().Debugf("adding dependency versions to file %s", fileName)
}
return nil
}
func (o *StepHelmOptions) replaceMissingVersionsFromVersionStream(requirementsConfig *config.RequirementsConfig, dir string) error {
fileName := filepath.Join(dir, helm.RequirementsFileName)
exists, err := util.FileExists(fileName)
if err != nil {
return errors.Wrapf(err, "failed to check for file %s", fileName)
}
if !exists {
log.Logger().Infof("No requirements file: %s so not checking for missing versions\n", fileName)
return nil
}
vs := requirementsConfig.VersionStream
log.Logger().Infof("Verifying the helm requirements versions in dir: %s using version stream URL: %s and git ref: %s\n", o.Dir, vs.URL, vs.Ref)
resolver, err := o.getOrCreateVersionResolver(requirementsConfig)
if err != nil {
return errors.Wrapf(err, "failed to create version resolver")
}
prefixes, err := resolver.GetRepositoryPrefixes()
if err != nil {
return errors.Wrapf(err, "failed to load repository prefixes")
}
err = o.verifyRequirementsYAML(resolver, prefixes, fileName)
if err != nil {
return errors.Wrapf(err, "failed to replace missing versions in file %s", fileName)
}
return nil
}
func (o *StepHelmOptions) createFuncMap(requirementsConfig *config.RequirementsConfig) (template.FuncMap, error) {
funcMap := helm.NewFunctionMap()
resolver, err := o.getOrCreateVersionResolver(requirementsConfig)
if err != nil {
return funcMap, err
}
// represents the helm template function
// which can be used like: `{{ versionStream "chart" "foo/bar" }}
funcMap["versionStream"] = func(kindString, name string) string {
kind := versionstream.VersionKind(kindString)
version, err := resolver.StableVersionNumber(kind, name)
if err != nil {
log.Logger().Errorf("failed to find %s version for %s in the version stream due to: %s\n", kindString, name, err.Error())
}
return version
}
return funcMap, nil
}
func (o *StepHelmOptions) overwriteProviderValues(requirements *config.RequirementsConfig, requirementsFileName string, valuesData []byte, params chartutil.Values, providersValuesDir string) ([]byte, error) {
provider := requirements.Cluster.Provider
if provider == "" {
log.Logger().Warnf("No provider in the requirements file %s\n", requirementsFileName)
return valuesData, nil
}
valuesTmplYamlFile := filepath.Join(providersValuesDir, provider, "values.tmpl.yaml")
exists, err := util.FileExists(valuesTmplYamlFile)
if err != nil {
return valuesData, errors.Wrapf(err, "failed to check if file exists: %s", valuesTmplYamlFile)
}
log.Logger().Infof("Applying the kubernetes overrides at %s\n", util.ColorInfo(valuesTmplYamlFile))
if !exists {
log.Logger().Warnf("No provider specific values overrides exist in file %s\n", valuesTmplYamlFile)
return valuesData, nil
}
funcMap, err := o.createFuncMap(requirements)
if err != nil {
return valuesData, err
}
overrideData, err := helm.ReadValuesYamlFileTemplateOutput(valuesTmplYamlFile, params, funcMap, requirements)
if err != nil {
return valuesData, errors.Wrapf(err, "failed to load provider specific helm value overrides %s", valuesTmplYamlFile)
}
if len(overrideData) == 0 {
return valuesData, nil
}
// now lets apply the overrides
values, err := helm.LoadValues(valuesData)
if err != nil {
return valuesData, errors.Wrapf(err, "failed to unmarshal the default helm values")
}
overrides, err := helm.LoadValues(overrideData)
if err != nil {
return valuesData, errors.Wrapf(err, "failed to unmarshal the default helm values")
}
util.CombineMapTrees(values, overrides)
data, err := yaml.Marshal(values)
return data, err
}
func (o *StepHelmOptions) getChartValues(targetNS string) ([]string, []string) {
return []string{
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
}, []string{
fmt.Sprintf("global.jxNs=%s", targetNS),
}
}