-
Notifications
You must be signed in to change notification settings - Fork 783
/
step_helm_apply.go
158 lines (135 loc) · 4 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
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"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/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// StepHelmApplyOptions contains the command line flags
type StepHelmApplyOptions struct {
StepHelmOptions
Namespace string
ReleaseName string
Wait bool
Force bool
DisableHelmVersion 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", "secrets.yaml"}
)
func NewCmdStepHelmApply(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepHelmApplyOptions{
StepHelmOptions: StepHelmOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
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", "", "", "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")
return cmd
}
func (o *StepHelmApplyOptions) Run() error {
var err error
chartName := o.Dir
dir := o.Dir
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return err
}
}
if !o.DisableHelmVersion {
(&StepHelmVersionOptions{}).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")
}
if ns == "" {
return fmt.Errorf("No --namespace option specified or $DEPLOY_NAMESPACE environment variable available")
}
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
err = kube.EnsureNamespaceCreated(kubeClient, ns, nil, nil)
if err != nil {
return err
}
releaseName := o.ReleaseName
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)
// lets discover any local value files
valueFiles := []string{}
for _, name := range defaultValueFileNames {
file := filepath.Join(dir, name)
exists, err := util.FileExists(file)
if exists && err == nil {
valueFiles = append(valueFiles, file)
}
}
log.Infof("Using values files: %s\n", strings.Join(valueFiles, ", "))
if o.Wait {
timeout := 600
err = o.Helm().UpgradeChart(chartName, releaseName, ns, nil, true, &timeout, o.Force, true, nil, valueFiles, "")
} else {
err = o.Helm().UpgradeChart(chartName, releaseName, ns, nil, true, nil, o.Force, false, nil, valueFiles, "")
}
if err != nil {
return err
}
return nil
}