-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_env_apply.go
232 lines (206 loc) · 7.04 KB
/
step_env_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
package env
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/jenkins-x/jx/v2/pkg/platform"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/namespace"
"github.com/ghodss/yaml"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
helm_cmd "github.com/jenkins-x/jx/v2/pkg/cmd/step/helm"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// StepEnvApplyOptions contains the command line flags
type StepEnvApplyOptions struct {
StepEnvOptions
Namespace string
Dir string
ReleaseName string
Wait bool
Force bool
DisableHelmVersion bool
ChangeNs bool
Vault bool
}
var (
// stepEnvApplyLong long description
stepEnvApplyLong = templates.LongDesc(`
Applies the GitOps source code (by default in the current directory) to the Environment.
This command will lazily create an environment, setup Helm and build and apply any helm charts defined in the env/Chart.yaml
`)
// StepEnvApplyExample example
stepEnvApplyExample = templates.Examples(`
# setup and/or update the helm charts for the environment
jx step env apply --namespace jx-staging
`)
)
// NewCmdStepEnvApply registers the command
func NewCmdStepEnvApply(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepEnvApplyOptions{
StepEnvOptions: StepEnvOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "apply",
Short: "Applies the GitOps source code to an environment",
Aliases: []string{""},
Long: stepEnvApplyLong,
Example: stepEnvApplyExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The Kubernetes namespace to apply the helm charts to")
cmd.Flags().StringVarP(&options.ReleaseName, "name", "r", "", "The name of the release")
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The directory to look for the environment chart")
cmd.Flags().BoolVarP(&options.ChangeNs, "change-namespace", "", false, "Set the given namespace as the current namespace in Kubernetes configuration")
cmd.Flags().BoolVarP(&options.Vault, "vault", "", false, "Environment secrets are stored in vault")
// step helm apply flags
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
}
// Run performs the comamand
func (o *StepEnvApplyOptions) Run() error {
var err error
dir := o.Dir
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return errors.Wrap(err, "getting the working directory")
}
}
ns, err := o.GetDeployNamespace(o.Namespace)
if err != nil {
return err
}
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
o.SetDevNamespace(ns)
apisClient, err := o.ApiExtensionsClient()
if err != nil {
return errors.Wrap(err, "creating the API extensions client")
}
err = kube.RegisterAllCRDs(apisClient)
if err != nil {
return errors.Wrap(err, "registering all CRDs")
}
// now lets find the dev environment to know what kind of helmer to use
chartFile := filepath.Join(dir, helm.ChartFileName)
exists, err := util.FileExists(chartFile)
if err != nil {
return errors.Wrap(err, "checking if file exits")
}
if !exists {
envDir := filepath.Join(dir, "env")
chartFile2 := filepath.Join(envDir, helm.ChartFileName)
exists2, err := util.FileExists(chartFile2)
if exists2 && err == nil {
dir = envDir
} else {
return fmt.Errorf("there is no Environment chart file at %s or %s\nplease try specify the directory containing the Chart.yaml or env/Chart.yaml with --dir", chartFile, chartFile2)
}
}
devEnvFile := filepath.Join(dir, "templates", "dev-env.yaml")
exists, err = util.FileExists(chartFile)
if exists && err == nil {
// lets setup the Helmer based on the current settings
log.Logger().Infof("Loading the latest Dev Environment configuration from %s", devEnvFile)
env := v1.Environment{}
data, err := ioutil.ReadFile(devEnvFile)
if err != nil {
return errors.Wrapf(err, "loading configuration file %s", devEnvFile)
}
err = yaml.Unmarshal(data, &env)
if err != nil {
return errors.Wrapf(err, "unmarshalling YAML file %s", devEnvFile)
}
teamSettings := &env.Spec.TeamSettings
// disable the modify of the Dev Environment lazily...
o.ModifyDevEnvironmentFn = func(callback func(env *v1.Environment) error) error {
err = callback(&env)
return err
}
helm := o.NewHelm(false, teamSettings.HelmBinary, teamSettings.NoTiller, teamSettings.HelmTemplate)
o.SetHelm(helm)
// ensure there's a development namespace setup
err = kube.EnsureDevNamespaceCreatedWithoutEnvironment(kubeClient, ns)
if err != nil {
return errors.Wrapf(err, "creating namespace %s for development environment", ns)
}
if o.ReleaseName == "" {
o.ReleaseName = platform.JenkinsXPlatformRelease
}
} else {
// ensure there's a development namespace setup
err = kube.EnsureNamespaceCreated(kubeClient, ns, nil, nil)
if err != nil {
return errors.Wrapf(err, "creating namespace %s for environment", ns)
}
}
// Change the current namesapce before applying the environment step
if o.ChangeNs {
_, currentNs, err := o.KubeClientAndNamespace()
if err != nil {
return errors.Wrap(err, "creating the kube client")
}
if currentNs != ns {
nsOptions := &namespace.NamespaceOptions{
CommonOptions: o.CommonOptions,
}
nsOptions.BatchMode = true
nsOptions.Args = []string{ns}
err := nsOptions.Run()
if err != nil {
log.Logger().Warnf("Failed to set context to namespace %s: %s", ns, err)
}
o.ResetClientsAndNamespaces()
}
}
stepHelmBuild := &helm_cmd.StepHelmBuildOptions{
StepHelmOptions: helm_cmd.StepHelmOptions{
StepOptions: step.StepOptions{
CommonOptions: o.CommonOptions,
},
Dir: dir,
},
}
err = stepHelmBuild.Run()
if err != nil {
return errors.Wrapf(err, "building helm chart in dir %s", dir)
}
stepApply := &helm_cmd.StepHelmApplyOptions{
StepHelmOptions: stepHelmBuild.StepHelmOptions,
Namespace: ns,
ReleaseName: o.ReleaseName,
Wait: o.Wait,
DisableHelmVersion: o.DisableHelmVersion,
Force: o.Force,
Vault: o.Vault,
}
err = stepApply.Run()
if err != nil {
return errors.Wrapf(err, "applying the helm chart in dir %s", dir)
}
log.Logger().Infof("Environment applied in namespace %s", util.ColorInfo(ns))
return nil
}