This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect.go
120 lines (98 loc) · 3.36 KB
/
connect.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
package cmd
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/gookit/color"
"github.com/platformercloud/platformer-cli/internal/auth"
"github.com/platformercloud/platformer-cli/internal/cli"
"github.com/platformercloud/platformer-cli/internal/config"
"github.com/platformercloud/platformer-cli/internal/mizzen"
"github.com/spf13/cobra"
)
var (
orgNameFlag string
projectNameFlag string
connectCmd = &cobra.Command{
Use: "connect",
Short: "Connect your Kubernetes Cluster to Platformer Cloud",
Run: func(cmd *cobra.Command, args []string) {
cli.HandleErrorAndExit(connectCluster())
},
}
)
// @todo - needs to be changed in production. These values must be injected at compile time somehow...
const consoleURL = "https://beta.console.platformer.com"
func init() {
connectCmd.Flags().StringVarP(&orgNameFlag, "organization", "o", "", "--organization=<ORG_NAME> or -o <ORG_NAME>")
connectCmd.Flags().StringVarP(&projectNameFlag, "project", "p", "", "--project=<PROJECT_NAME> or -p <PROJECT_NAME>")
rootCmd.AddCommand(connectCmd)
}
func connectCluster() error {
if orgNameFlag == "" {
orgNameFlag = config.GetDefaultOrg()
}
if projectNameFlag == "" {
projectNameFlag = config.GetDefaultProject()
}
organization, err := auth.GetOrganizationIDFromName(orgNameFlag)
if err != nil {
return &cli.UserError{Message: "invalid organization"}
}
project, err := auth.GetProjectIDFromName(organization.ID, projectNameFlag)
if err != nil {
return &cli.UserError{Message: "invalid project"}
}
kw, err := mizzen.NewKubectlWrapper()
if err != nil {
return &cli.UserError{Message: "kubectl binary not found. Please install and configure kubectl first."}
}
clusterList, err := getClusterList(kw)
if err != nil {
return err
}
var qs = []*survey.Question{
{
Name: "clusterName",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "Select Cluster to connect",
Options: clusterList,
},
},
}
var selection struct{ ClusterName string }
if err := survey.Ask(qs, &selection); err != nil {
return cli.TransformSurveyError(err)
}
green := color.FgGreen.Render
confirmPrompt := &survey.Confirm{
Default: true,
Message: fmt.Sprintf("Cluster %s will be connected under Organization %s and Project %s. Proceed?",
green(selection.ClusterName), green(orgNameFlag), green(projectNameFlag),
),
}
var confirm bool
if err := survey.AskOne(confirmPrompt, &confirm, survey.WithValidator(survey.Required)); err != nil {
return cli.TransformSurveyError(err)
}
if !confirm {
return &cli.CancelError{}
}
fmt.Println("Connecting your cluster... This may take a few minutes. Please do not exit the terminal.")
if err := mizzen.ConnectAndInstallAgent(kw, organization.ID, project.ID, selection.ClusterName); err != nil {
return err
}
fmt.Println(green("Success!"), "Cluster", selection.ClusterName, "has been successfully connected to Platformer Cloud")
fmt.Println("You may now deploy applications to your application through the platformer console:", consoleURL)
return nil
}
func getClusterList(kw *mizzen.KubectlWrapper) ([]string, cli.Error) {
clusters, err := kw.ListClusters()
if err != nil {
return nil, &cli.InternalError{Message: "failed to get-clusters from kubectl", Err: err}
}
if len(clusters) == 0 {
return nil, &cli.UserError{Message: "no clusters were found from kubectl"}
}
return clusters, nil
}