forked from keptn/keptn
/
install.go
606 lines (512 loc) · 15.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"time"
keptnutils "github.com/keptn/go-utils/pkg/utils"
"github.com/keptn/keptn/cli/utils"
"github.com/keptn/keptn/cli/utils/credentialmanager"
"github.com/spf13/cobra"
)
var configFilePath *string
var installerVersion *string
var platformIdentifier *string
const gke = "gke"
const aks = "aks"
const eks = "eks"
const openshift = "openshift"
const kubernetes = "kubernetes"
const installerPrefixURL = "https://raw.githubusercontent.com/keptn/keptn/"
const installerSuffixPath = "/installer/manifests/installer/installer.yaml"
const rbacSuffixPath = "/installer/manifests/installer/rbac.yaml"
type platform interface {
checkRequirements() error
getCreds() interface{}
getGithubCreds() *githubCredentials
checkCreds() error
readCreds()
printCreds()
}
type githubCredentials struct {
GithubPersonalAccessToken string `json:"githubPersonalAccessToken"`
GithubOrg string `json:"githubOrg"`
GithubUserName string `json:"githubUserName"`
}
var p platform
// installCmd represents the version command
var installCmd = &cobra.Command{
Use: "install",
Short: "Installs keptn on your Kubernetes cluster",
Long: `Installs keptn on your Kubernetes cluster
Example:
keptn install`,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if insecureSkipTLSVerify {
kubectlOptions = "--insecure-skip-tls-verify=true"
}
err := setPlatform()
if err != nil {
return err
}
isInstallerAvailable, err := checkInstallerAvailablity()
if err != nil || !isInstallerAvailable {
return errors.New("Installers not found under:\n" +
getInstallerURL() + "\n" + getRbacURL())
}
if p.checkRequirements() != nil {
return err
}
// Check whether kubectl is installed
isKubAvailable, err := utils.IsKubectlAvailable()
if err != nil || !isKubAvailable {
return errors.New(`keptn requires 'kubectl' but it is not available.
Please see https://kubernetes.io/docs/tasks/tools/install-kubectl/`)
}
if configFilePath != nil && *configFilePath != "" {
// Config was provided in form of a file
err = parseConfig(*configFilePath)
if err != nil {
return err
}
_, eks := p.(*eksPlatform)
if !eks {
// Verify the provided config
// Check whether all data is provided
if p.getGithubCreds().GithubPersonalAccessToken == "" ||
p.getGithubCreds().GithubOrg == "" || p.getGithubCreds().GithubUserName == "" {
return errors.New("Incomplete credential file " + *configFilePath)
}
err = checkGithubCreds()
if err != nil {
return err
}
}
// Check whether the authentication at the cluster is valid
err = p.checkCreds()
if err != nil {
return err
}
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
utils.PrintLog("Installing keptn...", utils.InfoLevel)
var err error
if !mocking {
if configFilePath == nil || *configFilePath == "" {
err = readCreds()
if err != nil {
return err
}
}
return doInstallation()
}
fmt.Println("Skipping intallation due to mocking flag")
return nil
},
}
func checkGithubCreds() error {
// Check GitHub token and org
validScopeRes, err := utils.HasTokenRepoScope(p.getGithubCreds().GithubPersonalAccessToken)
if err != nil {
return err
}
if !validScopeRes {
return errors.New("Personal access token requies at least a 'repo'-scope")
}
validOrg, err := utils.IsOrgExisting(p.getGithubCreds().GithubPersonalAccessToken, p.getGithubCreds().GithubOrg)
if err != nil {
return err
}
if !validOrg {
return errors.New("Provided organization " + p.getGithubCreds().GithubOrg + " does not exist.")
}
return nil
}
func setPlatform() error {
*platformIdentifier = strings.ToLower(*platformIdentifier)
switch *platformIdentifier {
case gke:
p = newGKEPlatform()
return nil
case aks:
p = newAKSPlatform()
return nil
case eks:
p = newEKSPlatform()
return nil
case openshift:
p = newOpenShiftPlatform()
return nil
case kubernetes:
p = newKubernetesPlatform()
return nil
default:
return errors.New("Unsupported platform '" + *platformIdentifier + "'. The following platforms are supported: aks, eks, gke, openshift, and kubernetes")
}
}
func init() {
rootCmd.AddCommand(installCmd)
configFilePath = installCmd.Flags().StringP("creds", "c", "", "The name of the creds file")
installCmd.Flags().MarkHidden("creds")
installerVersion = installCmd.Flags().StringP("keptn-version", "k", "master", "The branch or tag of the version which is installed")
installCmd.Flags().MarkHidden("keptn-version")
platformIdentifier = installCmd.Flags().StringP("platform", "p", "gke", "The platform to run keptn on [aks,eks,gke,openshift,kubernetes]")
installCmd.PersistentFlags().BoolVarP(&insecureSkipTLSVerify, "insecure-skip-tls-verify", "s", false, "Skip tls verification for kubectl commands")
}
func checkInstallerAvailablity() (bool, error) {
resp, err := http.Get(getInstallerURL())
if err != nil {
return false, err
}
if resp.StatusCode != http.StatusOK {
return false, nil
}
resp, err = http.Get(getRbacURL())
if err != nil {
return false, err
}
if resp.StatusCode != http.StatusOK {
return false, nil
}
return true, nil
}
func getInstallerURL() string {
return installerPrefixURL + *installerVersion + installerSuffixPath
}
func getRbacURL() string {
return installerPrefixURL + *installerVersion + rbacSuffixPath
}
// Preconditions: 1. Already authenticated against the cluster; 2. Github credentials are checked
func doInstallation() error {
path, err := keptnutils.GetKeptnDirectory()
if err != nil {
return err
}
installerPath := path + "installer.yaml"
// get the YAML for the installer pod
if err := utils.DownloadFile(installerPath, getInstallerURL()); err != nil {
return err
}
if err := utils.Replace(installerPath,
utils.PlaceholderReplacement{PlaceholderValue: "PLATFORM_PLACEHOLDER", DesiredValue: *platformIdentifier}); err != nil {
return err
}
_, aks := p.(*aksPlatform)
_, eks := p.(*eksPlatform)
_, gke := p.(*gkePlatform)
_, k8s := p.(*kubernetesPlatform)
if gke || aks || k8s || eks {
options := options{"apply", "-f", getRbacURL()}
options.appendIfNotEmpty(kubectlOptions)
_, err = keptnutils.ExecuteCommand("kubectl", options)
if err != nil {
return fmt.Errorf("Error while applying RBAC for installer pod: %s \nAborting installation", err.Error())
}
}
utils.PrintLog("Deploying keptn installer pod...", utils.InfoLevel)
o := options{"apply", "-f", installerPath}
o.appendIfNotEmpty(kubectlOptions)
_, err = keptnutils.ExecuteCommand("kubectl", o)
if err != nil {
return fmt.Errorf("Error while deploying keptn installer pod: %s \nAborting installation", err.Error())
}
utils.PrintLog("Installer pod deployed successfully.", utils.InfoLevel)
installerPodName, err := waitForInstallerPod()
if err != nil {
return err
}
err = getInstallerLogs(installerPodName)
if err != nil {
return err
}
if eks {
o = options{"get", "svc", "istio-ingressgateway", "-n", "istio-system",
"-ojsonpath={.status.loadBalancer.ingress[0].hostname}"}
o.appendIfNotEmpty(kubectlOptions)
hostname, err := keptnutils.ExecuteCommand("kubectl", o)
if err != nil {
return err
}
fmt.Println("Please create a Route53 Hosted Zone with a wildcard record set for " + hostname)
fmt.Println("Afterwards, call 'keptn configure domain YOUR_ROUTE53_DOMAIN'")
} else {
// installation finished, get auth token and endpoint
err = authUsingKube()
if err != nil {
return err
}
err = configure(p.getGithubCreds().GithubOrg,
p.getGithubCreds().GithubUserName, p.getGithubCreds().GithubPersonalAccessToken)
if err != nil {
return err
}
}
return os.Remove(installerPath)
}
func parseConfig(configFile string) error {
data, err := utils.ReadFile(configFile)
if err != nil {
return err
}
return json.Unmarshal([]byte(data), p.getCreds())
}
func readCreds() error {
credsStr, err := credentialmanager.GetInstallCreds()
if err != nil {
credsStr = ""
}
// Ignore unmarshaling error
json.Unmarshal([]byte(credsStr), p.getCreds())
fmt.Print("Please enter the following information or press enter to keep the old value:\n")
_, eks := p.(*eksPlatform)
for {
p.readCreds()
if !eks {
readGithubUserName(p.getGithubCreds())
// Check if the access token has the necessary permissions and the github org exists
validScopeRes := false
for !validScopeRes {
readGithubPersonalAccessToken(p.getGithubCreds())
validScopeRes, err = utils.HasTokenRepoScope(p.getGithubCreds().GithubPersonalAccessToken)
if err != nil {
return err
}
if !validScopeRes {
fmt.Println("GitHub Personal Access Token requies at least a 'repo'-scope")
p.getGithubCreds().GithubPersonalAccessToken = ""
}
}
validOrg := false
for !validOrg {
readGithubOrg(p.getGithubCreds())
validOrg, err = utils.IsOrgExisting(p.getGithubCreds().GithubPersonalAccessToken, p.getGithubCreds().GithubOrg)
if err != nil {
return err
}
if !validOrg {
fmt.Println("Provided GitHub Organization " + p.getGithubCreds().GithubOrg + " does not exist.")
p.getGithubCreds().GithubOrg = ""
}
}
}
fmt.Println()
fmt.Println("Please confirm that the provided information is correct: ")
p.printCreds()
if !eks {
fmt.Println("GitHub User Name: " + p.getGithubCreds().GithubUserName)
fmt.Println("GitHub Personal Access Token: " + p.getGithubCreds().GithubPersonalAccessToken)
fmt.Println("GitHub Organization: " + p.getGithubCreds().GithubOrg)
}
fmt.Println()
fmt.Println("Is this all correct? (y/n)")
reader := bufio.NewReader(os.Stdin)
in, err := reader.ReadString('\n')
if err != nil {
return err
}
in = strings.TrimSpace(in)
if in == "y" || in == "yes" {
break
}
}
newCreds, _ := json.Marshal(p.getCreds())
newCredsStr := strings.Replace(string(newCreds), "\r\n", "\n", -1)
newCredsStr = strings.Replace(newCredsStr, "\n", "", -1)
return credentialmanager.SetInstallCreds(newCredsStr)
}
func readGithubUserName(creds *githubCredentials) {
readUserInput(&creds.GithubUserName,
"^(([a-zA-Z0-9]+-)*[a-zA-Z0-9]+)$",
"GitHub User Name",
"Please enter a valid GitHub User Name.",
)
}
func readGithubPersonalAccessToken(creds *githubCredentials) {
readUserInput(&creds.GithubPersonalAccessToken,
"^[a-z0-9]{40}$",
"GitHub Personal Access Token",
"Please enter a valid GitHub Personal Access Token.",
)
}
func readGithubOrg(creds *githubCredentials) {
readUserInput(&creds.GithubOrg,
"^(([a-zA-Z0-9]+-)*[a-zA-Z0-9]+)$",
"GitHub Organization",
"Please enter a valid GitHub Organization.",
)
}
func readUserInput(value *string, regex string, promptMessage string, regexViolationMessage string) {
var re *regexp.Regexp
validateRegex := false
if regex != "" {
re = regexp.MustCompile(regex)
validateRegex = true
}
keepAsking := true
reader := bufio.NewReader(os.Stdin)
for keepAsking {
fmt.Printf("%s [%s]: ", promptMessage, *value)
userInput, _ := reader.ReadString('\n')
userInput = strings.TrimSpace(strings.Replace(userInput, "\r\n", "\n", -1))
if userInput != "" || *value == "" {
if validateRegex && !re.MatchString(userInput) {
fmt.Println(regexViolationMessage)
} else {
*value = userInput
keepAsking = false
}
} else {
keepAsking = false
}
}
}
func waitForInstallerPod() (string, error) {
podName := ""
podRunning := false
for ok := true; ok; ok = !podRunning {
time.Sleep(5 * time.Second)
options := options{"get",
"pods",
"-l",
"app=installer",
"-ojson"}
options.appendIfNotEmpty(kubectlOptions)
out, err := keptnutils.ExecuteCommand("kubectl", options)
if err != nil {
return "", fmt.Errorf("Error while retrieving installer pod: %s\n. Aborting installation", err)
}
var podInfo map[string]interface{}
err = json.Unmarshal([]byte(out), &podInfo)
if err == nil && podInfo != nil {
podStatusArray := podInfo["items"].([]interface{})
if len(podStatusArray) > 0 {
podStatus := podStatusArray[0].(map[string]interface{})["status"].(map[string]interface{})["phase"].(string)
if podStatus == "Running" {
podName = podStatusArray[0].(map[string]interface{})["metadata"].(map[string]interface{})["name"].(string)
podRunning = true
}
}
}
}
return podName, nil
}
func getInstallerLogs(podName string) error {
fmt.Printf("Getting logs of pod %s\n", podName)
options := options{"logs",
podName,
"-c",
"keptn-installer",
"-f"}
options.appendIfNotEmpty(kubectlOptions)
execCmd := exec.Command(
"kubectl", options...,
)
stdoutIn, _ := execCmd.StdoutPipe()
stderrIn, _ := execCmd.StderrPipe()
err := execCmd.Start()
if err != nil {
return fmt.Errorf("Could not get installer pod logs: '%s'", err)
}
// cmd.Wait() should be called only after we finish reading from stdoutIn and stderrIn.
cRes := make(chan bool)
cErr := make(chan error)
go func() {
res, err := copyAndCapture(stdoutIn, "keptn-installer.log")
cRes <- res
cErr <- err
}()
installSuccessfulStdErr, errStdErr := copyAndCapture(stderrIn, "keptn-installer-err.log")
installSuccessfulStdOut := <-cRes
errStdOut := <-cErr
if errStdErr != nil {
return errStdErr
}
if errStdOut != nil {
return errStdOut
}
err = execCmd.Wait()
if err != nil {
return fmt.Errorf("Could not get installer pod logs: '%s'", err)
}
if !installSuccessfulStdErr || !installSuccessfulStdOut {
return errors.New("keptn installation was unsuccessful")
}
return nil
}
func copyAndCapture(r io.Reader, fileName string) (bool, error) {
var file *os.File
errorOccured := false
installSuccessful := true
firstRead := true
const successMsg = "Installation of keptn complete."
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if firstRead {
// If something is read from the provided stream (stdin or stderr),
// the data of the stream has to contain the 'successMsg'
// for considering the keptn installation successful.
installSuccessful = false
firstRead = false
}
if file == nil {
// Only create file on-demand
var err error
file, err = createFileInKeptnDirectory(fileName)
if err != nil {
return false, fmt.Errorf("Could not write logs into file: '%s", err)
}
defer file.Close()
}
file.WriteString(scanner.Text() + "\n")
file.Sync()
var reg = regexp.MustCompile(`\[keptn\|[a-zA-Z]+\]`)
txt := scanner.Text()
matches := reg.FindStringSubmatch(txt)
if len(matches) == 1 {
msgLogLevel := matches[0]
msgLogLevel = strings.TrimPrefix(msgLogLevel, "[keptn|")
msgLogLevel = strings.TrimSuffix(msgLogLevel, "]")
msgLogLevel = strings.TrimSpace(msgLogLevel)
var fullSufixReg = regexp.MustCompile(`\[keptn\|[a-zA-Z]+\]\s+\[.*\]`)
outputStr := strings.TrimSpace(fullSufixReg.ReplaceAllString(txt, ""))
utils.PrintLogStringLevel(outputStr, msgLogLevel)
if utils.GetLogLevel(msgLogLevel) == utils.QuietLevel {
errorOccured = true
}
if outputStr == successMsg {
installSuccessful = true
}
}
}
return !errorOccured && installSuccessful, nil
}
func createFileInKeptnDirectory(fileName string) (*os.File, error) {
path, err := keptnutils.GetKeptnDirectory()
if err != nil {
return nil, err
}
return os.Create(path + fileName)
}