-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_helm_apply.go
295 lines (263 loc) · 8.91 KB
/
step_helm_apply.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
package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/helm"
configio "github.com/jenkins-x/jx/pkg/io"
"github.com/jenkins-x/jx/pkg/io/secrets"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/vault"
"github.com/mholt/archiver"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// StepHelmApplyOptions contains the command line flags
type StepHelmApplyOptions struct {
StepHelmOptions
Namespace string
ReleaseName string
Wait bool
Force bool
DisableHelmVersion bool
Vault bool
}
var (
StepHelmApplyLong = templates.LongDesc(`
Applies the helm chart in a given directory.
This step is usually used to apply any GitOps promotion changes into a Staging or Production cluster.
`)
StepHelmApplyExample = templates.Examples(`
# apply the chart in the env folder to namespace jx-staging
jx step helm apply --dir env --namespace jx-staging
`)
defaultValueFileNames = []string{"values.yaml", "myvalues.yaml", helm.SecretsFileName, filepath.Join("env", helm.SecretsFileName)}
)
func NewCmdStepHelmApply(commonOpts *CommonOptions) *cobra.Command {
options := StepHelmApplyOptions{
StepHelmOptions: StepHelmOptions{
StepOptions: StepOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "apply",
Short: "Applies the helm chart in a given directory",
Aliases: []string{""},
Long: StepHelmApplyLong,
Example: StepHelmApplyExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addStepHelmFlags(cmd)
cmd.Flags().StringVarP(&options.Namespace, "namespace", "", "", "The Kubernetes namespace to apply the helm chart to")
cmd.Flags().StringVarP(&options.ReleaseName, "name", "n", "", "The name of the release")
cmd.Flags().BoolVarP(&options.Wait, "wait", "", true, "Wait for Kubernetes readiness probe to confirm deployment")
cmd.Flags().BoolVarP(&options.Force, "force", "f", true, "Whether to to pass '--force' to helm to help deal with upgrading if a previous promote failed")
cmd.Flags().BoolVar(&options.DisableHelmVersion, "no-helm-version", false, "Don't set Chart version before applying")
cmd.Flags().BoolVarP(&options.Vault, "vault", "", false, "Helm secrets are stored in vault")
return cmd
}
func (o *StepHelmApplyOptions) Run() error {
var err error
chartName := o.Dir
dir := o.Dir
releaseName := o.ReleaseName
// let allow arguments to be passed in like for `helm install releaseName dir`
args := o.Args
if releaseName == "" && len(args) > 0 {
releaseName = args[0]
}
if dir == "" && len(args) > 1 {
dir = args[1]
}
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return err
}
}
if !o.DisableHelmVersion {
(&StepHelmVersionOptions{
StepHelmOptions: StepHelmOptions{
StepOptions: StepOptions{
CommonOptions: &CommonOptions{},
},
},
}).Run()
}
_, err = o.helmInitDependencyBuild(dir, o.defaultReleaseCharts())
if err != nil {
return err
}
helmBinary, noTiller, helmTemplate, err := o.TeamHelmBin()
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
ns = os.Getenv("DEPLOY_NAMESPACE")
}
kubeClient, curNs, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
if ns == "" {
ns = curNs
log.Infof("No --namespace option specified or $DEPLOY_NAMESPACE environment variable available so defaulting to using namespace %s\n", ns)
}
err = kube.EnsureNamespaceCreated(kubeClient, ns, nil, nil)
if err != nil {
return err
}
if releaseName == "" {
releaseName = ns
if helmBinary != "helm" || noTiller || helmTemplate {
releaseName = "jx"
}
}
info := util.ColorInfo
log.Infof("Applying helm chart at %s as release name %s to namespace %s\n", info(dir), info(releaseName), info(ns))
o.Helm().SetCWD(dir)
valueFiles := []string{}
for _, name := range defaultValueFileNames {
file := filepath.Join(dir, name)
exists, err := util.FileExists(file)
if exists && err == nil {
valueFiles = append(valueFiles, file)
}
}
if (o.GetSecretsLocation() == secrets.VaultLocationKind) || o.Vault {
store := configio.NewFileStore()
secretsFiles, err := o.fetchSecretFilesFromVault(dir, store)
if err != nil {
return errors.Wrap(err, "fetching secrets files from vault")
}
for _, sf := range secretsFiles {
if util.StringArrayIndex(valueFiles, sf) < 0 {
log.Infof("adding secret file %s\n", sf)
valueFiles = append(valueFiles, sf)
}
}
defer func() {
for _, secretsFile := range secretsFiles {
err := util.DestroyFile(secretsFile)
if err != nil {
log.Warnf("Failed to cleanup the secrets files (%s): %v",
strings.Join(secretsFiles, ", "), err)
}
}
}()
}
chartValues, err := helm.GenerateValues(dir, nil, true)
if err != nil {
return errors.Wrapf(err, "generating values.yaml for tree from %s", dir)
}
chartValuesFile := filepath.Join(dir, helm.ValuesFileName)
err = ioutil.WriteFile(chartValuesFile, chartValues, 0755)
if err != nil {
return errors.Wrapf(err, "writing values.yaml for tree to %s", chartValuesFile)
}
log.Infof("Wrote chart values.yaml %s generated from directory tree\n", chartValuesFile)
log.Infof("Using values files: %s\n", strings.Join(valueFiles, ", "))
err = o.applyTemplateOverrides(chartName)
if err != nil {
return errors.Wrap(err, "applying chart overrides")
}
if o.Wait {
timeout := 600
err = o.Helm().UpgradeChart(chartName, releaseName, ns, "", true, timeout, o.Force, true, nil, valueFiles,
"", "", "", false)
} else {
err = o.Helm().UpgradeChart(chartName, releaseName, ns, "", true, -1, o.Force, false, nil, valueFiles, "",
"", "", false)
}
if err != nil {
return errors.Wrapf(err, "upgrading helm chart '%s'", chartName)
}
return nil
}
func (o *StepHelmApplyOptions) applyTemplateOverrides(chartName string) error {
log.Infof("Applying chart overrides")
templateOverrides, err := filepath.Glob(chartName + "/../*/templates/*.yaml")
for _, overrideSrc := range templateOverrides {
if !strings.Contains(overrideSrc, "/env/") {
data, err := ioutil.ReadFile(overrideSrc)
if err == nil {
writeTemplateParts := strings.Split(overrideSrc, string(os.PathSeparator))
depChartsDir := filepath.Join(chartName, "charts")
depChartName := writeTemplateParts[len(writeTemplateParts)-3]
templateName := writeTemplateParts[len(writeTemplateParts)-1]
depChartDir := filepath.Join(depChartsDir, depChartName)
// If the chart directory does not exist explode the tgz
if exists, err := util.DirExists(depChartDir); err == nil && !exists {
chartArchives, _ := filepath.Glob(filepath.Join(depChartsDir, depChartName+"*.tgz"))
if len(chartArchives) == 1 {
log.Infof("Exploding chart %s", chartArchives[0])
archiver.Unarchive(chartArchives[0], depChartsDir)
// Remove the unexploded chart
os.Remove(chartArchives[0])
}
}
overrideDst := filepath.Join(depChartDir, "templates", templateName)
log.Infof("Copying chart override %s \n", overrideSrc)
err = ioutil.WriteFile(overrideDst, data, util.DefaultWritePermissions)
if err != nil {
log.Warnf("Error copying template %s to %s\n", overrideSrc, overrideDst)
}
}
}
}
return err
}
func (o *StepHelmApplyOptions) fetchSecretFilesFromVault(dir string, store configio.ConfigStore) ([]string, error) {
log.Infof("Fetching secrets from vault into directory %q\n", dir)
files := []string{}
client, err := o.SystemVaultClient(kube.DefaultNamespace)
if err != nil {
return files, errors.Wrap(err, "retrieving the system Vault")
}
secretNames, err := client.List(vault.GitOpsSecretsPath)
if err != nil {
return files, errors.Wrap(err, "listing the GitOps secrets in Vault")
}
secretPaths := []string{}
for _, secretName := range secretNames {
if secretName == vault.GitOpsTemplatesPath {
templatesPath := vault.GitOpsSecretPath(vault.GitOpsTemplatesPath)
templatesSecretNames, err := client.List(templatesPath)
if err == nil {
for _, templatesSecretName := range templatesSecretNames {
templateSecretPath := vault.GitOpsTemplatesPath + templatesSecretName
secretPaths = append(secretPaths, templateSecretPath)
}
}
} else {
secretPaths = append(secretPaths, secretName)
}
}
for _, secretPath := range secretPaths {
gitopsSecretPath := vault.GitOpsSecretPath(secretPath)
secret, err := client.ReadYaml(gitopsSecretPath)
if err != nil {
return files, errors.Wrapf(err, "retrieving the secret %q from Vault", secretPath)
}
secretFile := filepath.Join(dir, secretPath)
err = store.Write(secretFile, []byte(secret))
if err != nil {
return files, errors.Wrapf(err, "saving the secret file %q", secretFile)
}
log.Infof("Saved secrets file %s\n", util.ColorInfo(secretFile))
files = append(files, secretFile)
}
return files, nil
}