-
Notifications
You must be signed in to change notification settings - Fork 785
/
install.go
333 lines (304 loc) · 10.7 KB
/
install.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
package apps
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/environments"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/vault"
"k8s.io/client-go/kubernetes"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/surveyutils"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
// InstallOptions are shared options for installing, removing or upgrading apps for either GitOps or HelmOps
type InstallOptions struct {
Helmer helm.Helmer
KubeClient kubernetes.Interface
InstallTimeout string
JxClient versioned.Interface
Namespace string
EnvironmentsDir string
GitProvider gits.GitProvider
ConfigureGitFn environments.ConfigureGitFn
Gitter gits.Gitter
Verbose bool
DevEnv *jenkinsv1.Environment
BatchMode bool
In terminal.FileReader
Out terminal.FileWriter
Err io.Writer
GitOps bool
TeamName string
VaultClient *vault.Client
valuesFiles *environments.ValuesFiles // internal variable used to track, most be passed in
}
// DirectlyAddAppToGitOps adds the app at a particular version (
// or latest if not specified) from the repository with username and password. A releaseName can be specified.
// Values can be passed with in files or as a slice of name=value pairs. An alias can be specified.
// GitOps or HelmOps will be automatically chosen based on the o.GitOps flag
func (o *InstallOptions) AddApp(app string, version string, repository string, username string, password string,
releaseName string, valuesFiles []string, setValues []string, alias string, helmUpdate bool) error {
o.valuesFiles = &environments.ValuesFiles{
Items: valuesFiles,
}
// The chart inspector allows us to operate on the unpacked chart.
// We need to ask questions then as we have access to the schema, and can add secrets.
interrogateChartFn := o.createInterrogateChartFn(version, app, repository, username, password, alias, true)
// Called whilst the chart is unpacked and modifiable
installAppFunc := func(dir string) error {
//Ask the questions, this is an install, so no existing values
chartDetails, err := interrogateChartFn(dir, make(map[string]interface{}))
defer chartDetails.Cleanup()
if err != nil {
return err
}
if o.GitOps {
opts := GitOpsOptions{
InstallOptions: o,
}
err := opts.AddApp(app, dir, chartDetails.Version, repository, alias)
if err != nil {
return errors.Wrapf(err, "adding app %s version %s with alias %s using gitops", app, version, alias)
}
} else {
opts := HelmOpsOptions{
InstallOptions: o,
}
err = opts.AddApp(app, dir, chartDetails.Version, chartDetails.Values, repository, username, password,
releaseName,
setValues,
helmUpdate)
if err != nil {
return errors.Wrapf(err, "adding app %s version %s with alias %s using helm", app, version, alias)
}
}
return nil
}
// Do the actual work
return helm.InspectChart(app, version, repository, username, password, o.Helmer, installAppFunc)
}
//GetApps gets a list of installed apps
func (o *InstallOptions) GetApps(kubeClient kubernetes.Interface, namespace string, appNames []string) (apps *jenkinsv1.AppList, err error) {
client := o.JxClient
if err != nil {
return nil, errors.Wrap(err, "getting jx client")
}
listOptions := v1.ListOptions{}
if len(appNames) > 0 {
selector := fmt.Sprintf(helm.LabelAppName+" in (%s)", strings.Join(appNames[:], ", "))
listOptions.LabelSelector = selector
}
apps, err = client.JenkinsV1().Apps(namespace).List(listOptions)
if err != nil {
return nil, errors.Wrap(err, "listing apps")
}
return apps, nil
}
//DeleteApp deletes the app. An alias and releaseName can be specified. GitOps or HelmOps will be automatically chosen based on the o.GitOps flag
func (o *InstallOptions) DeleteApp(app string, alias string, releaseName string, purge bool) error {
o.valuesFiles = &environments.ValuesFiles{
Items: make([]string, 0),
}
if o.GitOps {
opts := GitOpsOptions{
InstallOptions: o,
}
err := opts.DeleteApp(app, alias)
if err != nil {
return err
}
} else {
opts := HelmOpsOptions{
InstallOptions: o,
}
err := opts.DeleteApp(app, releaseName, true)
if err != nil {
return err
}
}
return nil
}
// UpgradeApp upgrades the app (or all apps if empty) to a particular version (
// or the latest if not specified) from the repository with username and password. An alias can be specified.
// GitOps or HelmOps will be automatically chosen based on the o.GitOps flag
func (o *InstallOptions) UpgradeApp(app string, version string, repository string, username string, password string,
releaseName string, alias string, update bool, askExisting bool) error {
o.valuesFiles = &environments.ValuesFiles{
Items: make([]string, 0),
}
interrogateChartFunc := o.createInterrogateChartFn(version, app, repository, username, password, alias, askExisting)
// The chart inspector allows us to operate on the unpacked chart.
// We need to ask questions then as we have access to the schema, and can add secrets.
if o.GitOps {
opts := GitOpsOptions{
InstallOptions: o,
}
// Asking questions is a bit more complex in this case as the existing values file is in the environment
// repo, so we need to ask questions once we have that repo available
err := opts.UpgradeApp(app, version, repository, username, password, alias, interrogateChartFunc)
if err != nil {
return err
}
} else {
upgradeAppFunc := func(dir string) error {
// Try to load existing answers from the apps CRD
appCrdName := fmt.Sprintf("%s-%s", releaseName, app)
appResource, err := o.JxClient.JenkinsV1().Apps(o.Namespace).Get(appCrdName, v1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "getting App CRD %s", appResource.Name)
}
var existingValues map[string]interface{}
if appResource.Annotations != nil {
if encodedValues, ok := appResource.Annotations[ValuesAnnotation]; ok {
existingValuesBytes, err := base64.StdEncoding.DecodeString(encodedValues)
if err != nil {
log.Warnf("Error decoding base64 encoded string from %s on %s\n%s\n", ValuesAnnotation,
appCrdName, encodedValues)
}
err = json.Unmarshal(existingValuesBytes, &existingValues)
if err != nil {
return errors.Wrapf(err, "unmarshaling %s", string(existingValuesBytes))
}
}
}
// Ask the questions
chartDetails, err := interrogateChartFunc(dir, existingValues)
defer chartDetails.Cleanup()
if err != nil {
return errors.Wrapf(err, "asking questions")
}
opts := HelmOpsOptions{
InstallOptions: o,
}
err = opts.UpgradeApp(app, version, repository, username, password, releaseName, alias, update)
if err != nil {
return err
}
return nil
}
// Do the actual work
err := helm.InspectChart(app, version, repository, username, password, o.Helmer, upgradeAppFunc)
if err != nil {
return err
}
}
return nil
}
// ChartDetails are details about a chart returned by the chart interrogator
type ChartDetails struct {
Values []byte
Version string
Cleanup func()
}
func (o *InstallOptions) createInterrogateChartFn(version string, app string, repository string, username string,
password string, alias string, askExisting bool) func(chartDir string,
existing map[string]interface{}) (*ChartDetails, error) {
return func(chartDir string, existing map[string]interface{}) (*ChartDetails, error) {
var schema []byte
chartDetails := ChartDetails{
Cleanup: func() {},
}
if version == "" {
var err error
_, version, err = helm.LoadChartNameAndVersion(filepath.Join(chartDir, "Chart.yaml"))
if err != nil {
return &chartDetails, errors.Wrapf(err, "error loading chart from %s", chartDir)
}
if o.Verbose {
log.Infof("No version specified so using latest version which is %s\n", util.ColorInfo(version))
}
}
schemaFile := filepath.Join(chartDir, "values.schema.json")
if _, err := os.Stat(schemaFile); !os.IsNotExist(err) {
schema, err = ioutil.ReadFile(schemaFile)
if err != nil {
return &chartDetails, errors.Wrapf(err, "error reading schema file %s", schemaFile)
}
}
var values []byte
if schema != nil {
if o.valuesFiles != nil && len(o.valuesFiles.Items) > 0 {
log.Warnf("values.yaml specified by --valuesFiles will be used despite presence of schema in app")
}
var err error
var secrets []*surveyutils.GeneratedSecret
values, secrets, err = GenerateQuestions(schema, o.BatchMode, askExisting, existing, o.In, o.Out, o.Err)
if err != nil {
return &chartDetails, errors.Wrapf(err, "asking questions for schema %s", schemaFile)
}
cleanupValues, err := o.handleValues(chartDir, app, values)
chartDetails.Cleanup = func() {
cleanupValues()
}
if err != nil {
return &chartDetails, err
}
cleanupSecrets, err := o.handleSecrets(chartDir, app, secrets)
chartDetails.Cleanup = func() {
cleanupSecrets()
cleanupValues()
}
if err != nil {
return &chartDetails, err
}
chartDetails.Cleanup = func() {
cleanupSecrets()
cleanupValues()
}
}
chartDetails.Version = version
chartDetails.Values = values
return &chartDetails, nil
}
}
func (o *InstallOptions) handleValues(dir string, app string, values []byte) (func(), error) {
valuesFile, cleanup, err := AddValuesToChart(dir, app, values, o.Verbose)
if err != nil {
return cleanup, err
}
if valuesFile != "" {
o.valuesFiles.Items = append(o.valuesFiles.Items, valuesFile)
}
return cleanup, nil
}
func (o *InstallOptions) handleSecrets(dir string, app string, generatedSecrets []*surveyutils.GeneratedSecret) (func(),
error) {
if o.VaultClient != nil {
var vaultBasepath string
if o.GitOps {
gitInfo, err := gits.ParseGitURL(o.DevEnv.Spec.Source.URL)
if err != nil {
return nil, err
}
vaultBasepath = strings.Join([]string{"gitOps", gitInfo.Organisation, gitInfo.Name}, "/")
} else {
vaultBasepath = strings.Join([]string{"teams", o.TeamName}, "/")
}
f, err := AddSecretsToVault(generatedSecrets, *o.VaultClient, vaultBasepath)
if err != nil {
return func() {}, errors.Wrapf(err, "adding secrets to vault with basepath %s for %s", vaultBasepath,
app)
}
return f, nil
}
secretsFile, f, err := AddSecretsToTemplate(dir, app, generatedSecrets)
if err != nil {
return func() {}, errors.Wrapf(err, "adding secrets to template for %s", app)
}
if secretsFile != "" {
o.valuesFiles.Items = append(o.valuesFiles.Items, secretsFile)
}
return f, nil
}