-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_addon_cloudbees.go
284 lines (245 loc) · 8.68 KB
/
create_addon_cloudbees.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
package cmd
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
rbacv1 "k8s.io/api/rbac/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
defaultCloudBeesReleaseName = "cb"
defaultCloudBeesNamespace = "jx"
coreRepoName = "cb"
cbServiceName = "cb-core"
coreRepoUrl = "https://%s:%s@chartmuseum.jx.charts-demo.cloudbees.com"
serviceaccountsClusterAdmin = "serviceaccounts-cluster-admin"
defaultCloudBeesVersion = ""
)
var (
CreateAddonCloudBeesLong = templates.LongDesc(`
Creates the CloudBees app for Kubernetes addon
CloudBees app for Kubernetes provides unified Continuous Delivery Environment console to make it easier to do CI/CD and Environments across a number of microservices and teams
For more information please see [https://www.cloudbees.com/blog/want-help-build-cloudbees-kubernetes-jenkins-x](https://www.cloudbees.com/blog/want-help-build-cloudbees-kubernetes-jenkins-x)
`)
CreateAddonCloudBeesExample = templates.Examples(`
# Create the cloudbees addon
jx create addon cloudbees
`)
)
// CreateAddonCloudBeesOptions the options for the create spring command
type CreateAddonCloudBeesOptions struct {
CreateAddonOptions
Sso bool
Basic bool
Password string
}
// NewCmdCreateAddonCloudBees creates a command object for the "create" command
func NewCmdCreateAddonCloudBees(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateAddonCloudBeesOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "cloudbees",
Short: "Create the CloudBees app for Kubernetes (a web console for working with CI/CD, Environments and GitOps)",
Aliases: []string{"cloudbee", "cb", "core", "kubecd"},
Long: CreateAddonCloudBeesLong,
Example: CreateAddonCloudBeesExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Sso, "sso", "", false, "Enable single sign-on")
cmd.Flags().BoolVarP(&options.Basic, "basic", "", false, "Enable basic auth")
cmd.Flags().StringVarP(&options.Password, "password", "p", "", "Password to access UI when using basic auth. Defaults to default Jenkins X admin password.")
options.addCommonFlags(cmd)
options.addFlags(cmd, defaultCloudBeesNamespace, defaultCloudBeesReleaseName, defaultCloudBeesVersion)
return cmd
}
// Run implements the command
func (o *CreateAddonCloudBeesOptions) Run() error {
if o.Sso == false && o.Basic == false {
return fmt.Errorf("please use --sso or --basic flag")
}
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
c, _, err := o.KubeClient()
if err != nil {
return err
}
// todo add correct roles to core rather than make EVERY service account cluster admin
_, err = c.RbacV1().ClusterRoleBindings().Get(serviceaccountsClusterAdmin, v1.GetOptions{})
if err != nil {
ok := false
log.Warn("CloudBees app for Kubernetes is in preview and for now requires cluster admin to be granted to ALL service accounts in your cluster. Check CLI help for more info.\n")
prompt := &survey.Confirm{
Message: "create cluster admin rolebinding?",
Default: false,
Help: "a cluster admin role provides full privileges and therefore this action should not be run on anything other than a demo cluster that can be recreated",
}
survey.AskOne(prompt, &ok, nil, surveyOpts)
if !ok {
log.Info("aborting the core addon\n")
return nil
}
rb := rbacv1.ClusterRoleBinding{
ObjectMeta: v1.ObjectMeta{
Name: serviceaccountsClusterAdmin,
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: "cluster-admin",
},
Subjects: []rbacv1.Subject{
{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Group",
Name: "system:serviceaccounts",
},
},
}
_, err = c.RbacV1().ClusterRoleBindings().Create(&rb)
if err != nil {
return err
}
}
// check if helm repo is missing, the repo is authenticated and includes username/password so check with dummy values
// first as we wont need to prompt for username password if the host part of the URL matches an existing repo
missing, err := o.isHelmRepoMissing(fmt.Sprintf(coreRepoUrl, "dummy", "dummy"))
if err != nil {
return err
}
if missing {
log.Infof(`
You will need your username and password to install this addon while it is in preview.
To register to get your username/password to to: %s
`, util.ColorInfo("https://pages.cloudbees.com/K8s"))
username := ""
prompt := &survey.Input{
Message: "CloudBees Preview username",
Help: "CloudBees is in private preview which requires a username / password for installation",
}
survey.AskOne(prompt, &username, nil, surveyOpts)
password := ""
passPrompt := &survey.Password{
Message: "CloudBees Preview password",
Help: "CloudBees is in private preview which requires a username / password for installation",
}
survey.AskOne(passPrompt, &password, nil, surveyOpts)
err := o.addHelmRepoIfMissing(fmt.Sprintf(coreRepoUrl, username, password), coreRepoName)
if err != nil {
return err
}
}
if o.Sso {
log.Infof("Configuring %s...\n", util.ColorInfo("single sign-on"))
o.devNamespace, _, err = kube.GetDevNamespace(o.KubeClientCached, o.currentNamespace)
if err != nil {
return errors.Wrap(err, "retrieving the development namespace")
}
ingressConfig, err := kube.GetIngressConfig(o.KubeClientCached, o.devNamespace)
if err != nil {
return errors.Wrap(err, "retrieving existing ingress configuration")
}
domain, err := util.PickValue("Domain:", ingressConfig.Domain, true, o.In, o.Out, o.Err)
if err != nil {
return errors.Wrap(err, "reading domain")
}
dexURL, err := util.PickValue("Dex URL:", fmt.Sprintf("https://dex.sso.%s", ingressConfig.Domain), true, o.In, o.Out, o.Err)
if err != nil {
return errors.Wrap(err, "reading dex URL")
}
// Strip the trailing slash automatically
dexURL = strings.TrimSuffix(dexURL, "/")
err = o.ensureCertmanager()
if err != nil {
return errors.Wrap(err, "ensuring cert-manager is installed")
}
ingressConfig.TLS = true
ingressConfig.Issuer = kube.CertmanagerIssuerProd
err = kube.CleanCertmanagerResources(o.KubeClientCached, o.Namespace, ingressConfig)
if err != nil {
return errors.Wrap(err, "creating cert-manager issuer")
}
values := []string{
"sso.create=true",
"sso.oidcIssuerUrl=" + dexURL,
"sso.domain=" + domain,
"sso.certIssuerName=" + ingressConfig.Issuer}
if len(o.SetValues) > 0 {
o.SetValues = o.SetValues + "," + strings.Join(values, ",")
} else {
o.SetValues = strings.Join(values, ",")
}
} else {
// Disable SSO for basic auth
o.SetValues = strings.Join([]string{"sso.create=false"}, ",")
}
err = o.CreateAddon("cb")
if err != nil {
return err
}
if o.Basic {
devNamespace, _, err := kube.GetDevNamespace(o.KubeClientCached, o.currentNamespace)
if err != nil {
return fmt.Errorf("cannot find a dev team namespace to get existing exposecontroller config from. %v", err)
}
if o.Password == "" {
o.Password, err = o.getDefaultAdminPassword(devNamespace)
if err != nil {
return err
}
}
svc, err := c.CoreV1().Services(o.currentNamespace).Get(cbServiceName, metav1.GetOptions{})
if err != nil {
return err
}
annotationsUpdated := false
if svc.Annotations[kube.AnnotationExpose] == "" {
svc.Annotations[kube.AnnotationExpose] = "true"
annotationsUpdated = true
}
if svc.Annotations[kube.AnnotationIngress] == "" {
svc.Annotations[kube.AnnotationIngress] = "nginx.ingress.kubernetes.io/auth-type: basic\nnginx.ingress.kubernetes.io/auth-secret: jx-basic-auth"
annotationsUpdated = true
}
if annotationsUpdated {
svc, err = o.KubeClientCached.CoreV1().Services(o.Namespace).Update(svc)
if err != nil {
return fmt.Errorf("failed to update service %s/%s", o.Namespace, cbServiceName)
}
}
_, _, err = o.KubeClient()
if err != nil {
return err
}
log.Infof("using exposecontroller config from dev namespace %s\n", devNamespace)
log.Infof("target namespace %s\n", o.Namespace)
// create the ingress rule
err = o.expose(devNamespace, o.Namespace, o.Password)
if err != nil {
return err
}
}
return nil
}