-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_addon_cloudbees.go
252 lines (214 loc) · 7.74 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
package cmd
import (
"fmt"
"strings"
"time"
survey "gopkg.in/AlecAivazis/survey.v1"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/kube/pki"
"github.com/jenkins-x/jx/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
defaultCloudBeesReleaseName = "cb"
defaultCloudBeesNamespace = "jx"
coreRepoName = "cb"
cbServiceName = "cb-core"
coreRepoUrl = "https://chartmuseum.jx.charts-demo.cloudbees.com"
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(commonOpts *CommonOptions) *cobra.Command {
options := &CreateAddonCloudBeesOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOpts,
},
},
}
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.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)
client, err := o.KubeClient()
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(coreRepoUrl)
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(coreRepoUrl, coreRepoName, username, password)
if err != nil {
return err
}
}
if o.Sso {
log.Infof("Configuring %s...\n", util.ColorInfo("single sign-on"))
o.devNamespace, _, err = kube.GetDevNamespace(client, o.currentNamespace)
if err != nil {
return errors.Wrap(err, "retrieving the development namespace")
}
ingressConfig, err := kube.GetIngressConfig(client, 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")
}
certClient, err := o.CertManagerClient()
if err != nil {
return errors.Wrap(err, "creating cert-manager client")
}
ingressConfig.TLS = true
ingressConfig.Issuer = pki.CertManagerIssuerProd
err = pki.CreateCertManagerResources(certClient, 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=" + pki.CertManagerIssuerProd}
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.Sso {
// wait for cert to be issued
certName := pki.CertSecretPrefix + "core"
log.Infof("Waiting for cert: %s...\n", util.ColorInfo(certName))
certMngrClient, err := o.CertManagerClient()
if err != nil {
return errors.Wrap(err, "creating the cert-manager client")
}
err = pki.WaitCertificateIssuedReady(certMngrClient, certName, o.Namespace, 3*time.Minute)
if err != nil {
return err // this is already wrapped by the previous call
}
log.Infof("Ready Cert: %s\n", util.ColorInfo(certName))
}
if o.Basic {
devNamespace, _, err := kube.GetDevNamespace(client, 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 := client.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 = client.CoreV1().Services(o.Namespace).Update(svc)
if err != nil {
return fmt.Errorf("failed to update service %s/%s", o.Namespace, cbServiceName)
}
}
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
}
}
log.Infof("Addon installed successfully.\n\n %s Open the app in a browser\n\n", util.ColorInfo("jx cloudbees"))
return nil
}