-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_gke_service_account.go
168 lines (139 loc) · 4.49 KB
/
create_gke_service_account.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
package create
import (
"errors"
"fmt"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
survey "gopkg.in/AlecAivazis/survey.v1"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cloud/gke"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
)
type CreateGkeServiceAccountFlags struct {
Name string
Project string
SkipLogin bool
}
type CreateGkeServiceAccountOptions struct {
options.CreateOptions
Flags CreateGkeServiceAccountFlags
}
var (
createGkeServiceAccountExample = templates.Examples(`
jx create gke-service-account
# to specify the options via flags
jx create gke-service-account --name my-service-account --project my-gke-project
`)
)
// NewCmdCreateGkeServiceAccount creates a command object for the "create" command
func NewCmdCreateGkeServiceAccount(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateGkeServiceAccountOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "gke-service-account",
Short: "Creates a GKE service account",
Example: createGkeServiceAccountExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.addFlags(cmd, true)
return cmd
}
func (options *CreateGkeServiceAccountOptions) addFlags(cmd *cobra.Command, addSharedFlags bool) {
cmd.Flags().StringVarP(&options.Flags.Name, "name", "n", "", "The name of the service account to create")
cmd.Flags().StringVarP(&options.Flags.Project, "project", "p", "", "The GCP project to create the service account in")
if addSharedFlags {
cmd.Flags().BoolVarP(&options.Flags.SkipLogin, "skip-login", "", false, "Skip Google auth if already logged in via gcloud auth")
}
}
// Run implements this command
func (o *CreateGkeServiceAccountOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
if !o.Flags.SkipLogin {
err := o.RunCommandVerbose("gcloud", "auth", "login", "--brief")
if err != nil {
return err
}
}
if o.Flags.Name == "" {
prompt := &survey.Input{
Message: "Name for the service account",
}
err := survey.AskOne(prompt, &o.Flags.Name, func(val interface{}) error {
// since we are validating an Input, the assertion will always succeed
if str, ok := val.(string); !ok || len(str) < 6 {
return errors.New("Service Account name must be longer than 5 characters")
}
return nil
}, surveyOpts)
if err != nil {
return err
}
}
if o.Flags.Project == "" {
projectId, err := o.getGoogleProjectId()
if err != nil {
return err
}
o.Flags.Project = projectId
}
path, err := o.GCloud().GetOrCreateServiceAccount(o.Flags.Name, o.Flags.Project, util.HomeDir(), gke.RequiredServiceAccountRoles)
if err != nil {
return err
}
log.Logger().Infof("Created service account key %s", util.ColorInfo(path))
return nil
}
// asks to chose from existing projects or optionally creates one if none exist
func (o *CreateGkeServiceAccountOptions) getGoogleProjectId() (string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
existingProjects, err := gke.GetGoogleProjects()
if err != nil {
return "", err
}
var projectId string
if len(existingProjects) == 0 {
confirm := &survey.Confirm{
Message: fmt.Sprintf("No existing Google Projects exist, create one now?"),
Default: true,
}
flag := true
err = survey.AskOne(confirm, &flag, nil, surveyOpts)
if err != nil {
return "", err
}
if !flag {
return "", errors.New("no google project to create cluster in, please manual create one and rerun this wizard")
}
if flag {
return "", errors.New("auto creating projects not yet implemented, please manually create one and rerun the wizard")
}
} else if len(existingProjects) == 1 {
projectId = existingProjects[0]
log.Logger().Infof("Using the only Google Cloud Project %s to create the cluster", util.ColorInfo(projectId))
} else {
prompts := &survey.Select{
Message: "Google Cloud Project:",
Options: existingProjects,
Help: "Select a Google Project to create the cluster in",
}
err := survey.AskOne(prompts, &projectId, nil, surveyOpts)
if err != nil {
return "", err
}
}
if projectId == "" {
return "", errors.New("no Google Cloud Project to create cluster in, please manual create one and rerun this wizard")
}
return projectId, nil
}