-
Notifications
You must be signed in to change notification settings - Fork 787
/
add_app.go
400 lines (356 loc) · 11.2 KB
/
add_app.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package cmd
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/vault"
"github.com/jenkins-x/jx/pkg/surveyutils"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/util"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// AddAppOptions the options for the create spring command
type AddAppOptions struct {
AddOptions
GitOps bool
DevEnv *jenkinsv1.Environment
Repo string
Username string
Password string
Alias string
// allow git to be configured externally before a PR is created
ConfigureGitCallback ConfigureGitFolderFn
Namespace string
Version string
ReleaseName string
SetValues []string
ValueFiles []string
HelmUpdate bool
}
const (
optionHelmUpdate = "helm-update"
optionValues = "values"
optionSet = "set"
optionAlias = "alias"
)
const (
appsGeneratedSecretKey = "appsGeneratedSecrets"
)
const secretTemplate = `
{{- range .Values.generatedSecrets }}
apiVersion: v1
data:
{{ .key }}: {{ .value }}
kind: Secret
metadata:
name: {{ .name }}
type: Opaque
{{- end }}
`
// NewCmdAddApp creates a command object for the "create" command
func NewCmdAddApp(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &AddAppOptions{
AddOptions: AddOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "app",
Short: "Adds an app",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addFlags(cmd, kube.DefaultNamespace, "", "")
return cmd
}
func (o *AddAppOptions) addFlags(cmd *cobra.Command, defaultNamespace string, defaultOptionRelease string, defaultVersion string) {
// Common flags
cmd.Flags().StringVarP(&o.Version, "version", "v", defaultVersion,
"The chart version to install")
cmd.Flags().StringVarP(&o.Repo, "repository", "", "",
"The repository from which the app should be installed (default specified in your dev environment)")
cmd.Flags().StringVarP(&o.Username, "username", "", "",
"The username for the repository")
cmd.Flags().StringVarP(&o.Password, "password", "", "",
"The password for the repository")
cmd.Flags().BoolVarP(&o.BatchMode, optionBatchMode, "b", false, "In batch mode the command never prompts for user input")
cmd.Flags().BoolVarP(&o.Verbose, optionVerbose, "", false, "Enable verbose logging")
cmd.Flags().StringVarP(&o.Alias, optionAlias, "", "",
"An alias to use for the app (available when using GitOps for your dev environment)")
cmd.Flags().StringVarP(&o.ReleaseName, optionRelease, "r", defaultOptionRelease,
"The chart release name (available when NOT using GitOps for your dev environment)")
cmd.Flags().BoolVarP(&o.HelmUpdate, optionHelmUpdate, "", true,
"Should we run helm update first to ensure we use the latest version (available when NOT using GitOps for your dev environment)")
cmd.Flags().StringVarP(&o.Namespace, optionNamespace, "n", defaultNamespace, "The Namespace to install into (available when NOT using GitOps for your dev environment)")
cmd.Flags().StringArrayVarP(&o.ValueFiles, optionValues, "f", []string{}, "List of locations for values files, "+
"can be local files or URLs (available when NOT using GitOps for your dev environment)")
cmd.Flags().StringArrayVarP(&o.SetValues, optionSet, "s", []string{},
"The chart set values (can specify multiple or separate values with commas: key1=val1,key2=val2) (available when NOT using GitOps for your dev environment)")
}
// Run implements this command
func (o *AddAppOptions) Run() error {
o.GitOps, o.DevEnv = o.GetDevEnv()
if o.Repo == "" {
o.Repo = o.DevEnv.Spec.TeamSettings.AppsRepository
}
var vaultBasepath string
var vaultClient vault.Client
useVault := o.UseVault()
if useVault {
var err error
if o.GitOps {
gitInfo, err := gits.ParseGitURL(o.DevEnv.Spec.Source.URL)
if err != nil {
return err
}
vaultBasepath = strings.Join([]string{"gitOps", gitInfo.Organisation, gitInfo.Name}, "/")
} else {
teamName, _, err := o.TeamAndEnvironmentNames()
if err != nil {
return err
}
vaultBasepath = strings.Join([]string{"teams", teamName}, "/")
}
vaultClient, err = o.CreateSystemVaultClient()
if err != nil {
return err
}
}
if o.GitOps {
msg := "unable to specify --%s when using GitOps for your dev environment"
if o.ReleaseName != "" {
return util.InvalidOptionf(optionRelease, o.ReleaseName, msg, optionRelease)
}
if !o.HelmUpdate {
return util.InvalidOptionf(optionHelmUpdate, o.HelmUpdate, msg, optionHelmUpdate)
}
if o.Namespace != "" {
return util.InvalidOptionf(optionNamespace, o.Namespace, msg, optionNamespace)
}
if len(o.SetValues) > 0 {
return util.InvalidOptionf(optionSet, o.SetValues, msg, optionSet)
}
if len(o.ValueFiles) > 1 {
return util.InvalidOptionf(optionValues, o.SetValues,
"no more than one --%s can be specified when using GitOps for your dev environment", optionValues)
}
if !useVault {
return fmt.Errorf("cannot install apps without a vault when using GitOps for your dev environment")
}
}
if !o.GitOps {
if o.Alias != "" {
return util.InvalidOptionf(optionAlias, o.Alias,
"unable to specify --%s when NOT using GitOps for your dev environment", optionAlias)
}
}
args := o.Args
if len(args) == 0 {
return o.Cmd.Help()
}
if len(args) > 1 {
return o.Cmd.Help()
}
if o.Repo == "" {
return fmt.Errorf("must specify a repository")
}
for _, app := range args {
var version string
if o.Version != "" {
version = o.Version
}
var schema []byte
err := helm.InspectChart(app, version, o.Repo, o.Username, o.Password, o.Helm(), func(dir string) error {
if version == "" {
var err error
_, version, err = helm.LoadChartNameAndVersion(filepath.Join(dir, "Chart.yaml"))
if err != nil {
return errors.Wrapf(err, "error loading chart from %s", dir)
}
if o.Verbose {
log.Infof("No version specified so using latest version which is %s\n", util.ColorInfo(version))
}
}
schemaFile := filepath.Join(dir, "values.schema.json")
if _, err := os.Stat(schemaFile); !os.IsNotExist(err) {
schema, err = ioutil.ReadFile(schemaFile)
if err != nil {
return errors.Wrapf(err, "error reading schema file %s", schemaFile)
}
}
if schema != nil {
secrets := make([]*surveyutils.GeneratedSecret, 0)
schemaOptions := surveyutils.JSONSchemaOptions{
CreateSecret: func(name string, key string, value string) (*jenkinsv1.ResourceReference, error) {
secret := &surveyutils.GeneratedSecret{
Name: name,
Key: key,
Value: value,
}
secrets = append(secrets, secret)
return &jenkinsv1.ResourceReference{
Name: name,
Kind: "Secret",
}, nil
},
}
values, err := schemaOptions.GenerateValues(schema, []string{}, o.In, o.Out, o.Err)
if err != nil {
return errors.Wrapf(err, "error generating values for schema %s", schemaFile)
}
valuesYaml, err := yaml.JSONToYAML(values)
if err != nil {
return errors.Wrapf(err, "error converting values from json to yaml\n\n%v", values)
}
if o.Verbose {
log.Infof("Generated values.yaml:\n\n%v\n", util.ColorInfo(string(valuesYaml)))
}
// We write a secret template into the chart, append the values for the generated secrets to values.yaml
if len(secrets) > 0 {
if useVault {
for _, secret := range secrets {
path := strings.Join([]string{vaultBasepath, secret.Name}, "/")
err := vault.WriteMap(vaultClient, path, map[string]interface{}{
secret.Key: secret.Value,
})
if err != nil {
return err
}
}
} else {
// For each secret, we write a file into the chart
templatesDir := filepath.Join(dir, "templates")
err = os.MkdirAll(templatesDir, 0700)
if err != nil {
return err
}
fileName := filepath.Join(templatesDir, "app-generated-secret-template.yaml")
err := ioutil.WriteFile(fileName, []byte(secretTemplate), 0755)
if err != nil {
return err
}
allSecrets := map[string][]*surveyutils.GeneratedSecret{
appsGeneratedSecretKey: secrets,
}
ybs, err := json.Marshal(allSecrets)
if err != nil {
return err
}
valuesYaml = append(valuesYaml, ybs...)
}
}
if err != nil {
return err
}
if !o.GitOps {
if len(o.ValueFiles) > 0 && schema != nil {
log.Warnf("values.yaml specified by --valuesFiles will be used despite presence of schema in app")
} else if schema != nil {
valuesFile, err := ioutil.TempFile("", fmt.Sprintf("%s-values.yaml", app))
defer func() {
err = valuesFile.Close()
if err != nil {
log.Warnf("Error closing %s because %v\n", valuesFile.Name(), err)
}
err = util.DeleteFile(valuesFile.Name())
if err != nil {
log.Warnf("Error deleting %s because %v\n", valuesFile.Name(), err)
}
}()
if err != nil {
return err
}
_, err = valuesFile.Write(valuesYaml)
if err != nil {
return err
}
o.ValueFiles = []string{
valuesFile.Name(),
}
}
if err != nil {
return err
}
}
}
if o.GitOps {
err := o.createPR(app, dir, version)
if err != nil {
return err
}
} else {
err := o.installApp(app, dir, version)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func (o *AddAppOptions) createPR(app string, dir string, version string) error {
branchNameText := "add-app-" + app + "-" + version
title := fmt.Sprintf("Add %s %s", app, version)
message := fmt.Sprintf("Add app %s %s", app, version)
pullRequestInfo, err := o.createEnvironmentPullRequest(o.DevEnv, o.CreateAddRequirementFn(app, o.Alias, version,
o.Repo, o.ValueFiles, dir),
&branchNameText, &title,
&message,
nil, o.ConfigureGitCallback)
if err != nil {
return errors.Wrapf(err, "creating pr for %s", app)
}
log.Infof("Added app via Pull Request %s\n", pullRequestInfo.PullRequest.URL)
return nil
}
func (o *AddAppOptions) installApp(name string, chart string, version string) error {
err := o.ensureHelm()
if err != nil {
return errors.Wrap(err, "failed to ensure that helm is present")
}
setValues := make([]string, 0)
for _, vs := range o.SetValues {
setValues = append(setValues, strings.Split(vs, ",")...)
}
err = o.installChartOptions(helm.InstallChartOptions{
ReleaseName: name,
Chart: chart,
Version: version,
Ns: o.Namespace,
HelmUpdate: o.HelmUpdate,
SetValues: setValues,
ValueFiles: o.ValueFiles,
Repository: o.Repo,
Username: o.Username,
Password: o.Password,
})
if err != nil {
return fmt.Errorf("failed to install name %s: %v", name, err)
}
// Attach the secrets to the name CRD
return o.OnAppInstall(name, version)
}