-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgrade_ingress.go
375 lines (319 loc) · 11.1 KB
/
upgrade_ingress.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
package cmd
import (
"errors"
"fmt"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"time"
)
var (
upgradeIngressLong = templates.LongDesc(`
Upgrades the Jenkins X Ingress rules
`)
upgradeIngressExample = templates.Examples(`
# Upgrades the Jenkins X Ingress rules
jx upgrade ingress
`)
)
const (
CertManagerDeployment = "cert-manager"
CertManagerNamespace = "cert-manager"
Exposecontroller = "exposecontroller"
)
// UpgradeIngressOptions the options for the create spring command
type UpgradeIngressOptions struct {
CreateOptions
SkipCertManager bool
Cluster bool
Namespaces []string
Version string
TargetNamespaces []string
IngressConfig kube.IngressConfig
}
// NewCmdUpgradeIngress defines the command
func NewCmdUpgradeIngress(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &UpgradeIngressOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "ingress",
Short: "Upgrades Ingress rules",
Aliases: []string{"ing"},
Long: upgradeIngressLong,
Example: upgradeIngressExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Cluster, "cluster", "", false, "Enable cluster wide Ingress upgrade")
cmd.Flags().StringArrayVarP(&options.Namespaces, "namespaces", "", []string{}, "Namespaces to upgrade")
cmd.Flags().BoolVarP(&options.SkipCertManager, "skip-certmanager", "", false, "Skips certmanager installation")
return cmd
}
// Run implements the command
func (o *UpgradeIngressOptions) Run() error {
_, _, err := o.KubeClient()
if err != nil {
return fmt.Errorf("cannot connect to kubernetes cluster: %v", err)
}
o.devNamespace, _, err = kube.GetDevNamespace(o.kubeClient, o.currentNamespace)
if err != nil {
return err
}
// if existing ingress exist in the namespaces ask do you want to delete them?
ingressToDelete, err := o.getExistingIngressRules()
if err != nil {
return err
}
// wizard to ask for config values
err = o.confirmExposecontrollerConfig()
if err != nil {
return err
}
// confirm values
util.Confirm(fmt.Sprintf("Using config values %v, ok?", o.IngressConfig), true, "")
// save details to a configmap
err = kube.SaveIngressConfig(o.kubeClient, o.devNamespace, o.IngressConfig)
if err != nil {
return err
}
err = o.cleanServiceAnnotations()
if err != nil {
return err
}
// if tls create CRDs
if o.IngressConfig.TLS {
err = o.ensureCertmanagerSetup()
if err != nil {
return err
}
}
// annotate any service that has expose=true with correct certmanager staging / prod annotation
err = o.annotateExposedServicesWithCertManager()
if err != nil {
return err
}
// delete ingress
for name, namespace := range ingressToDelete {
log.Infof("Deleting ingress %s/%s\n", namespace, name)
err := o.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("cannot delete ingress rule %s in namespace %s: %v", name, namespace, err)
}
}
err = o.recreateIngressRules()
if err != nil {
return err
}
err = o.updateJenkinsURL(o.TargetNamespaces)
if err != nil {
return err
}
// todo wait for certs secrets to update ingress rules?
log.Success("Ingress rules recreated\n")
if o.IngressConfig.TLS {
log.Warn("It can take around 5 minutes for Cert Manager to get certificates from Lets Encrypt and update Ingress rules\n")
log.Info("Use the following commands to diagnose any issues:\n")
log.Infof("jx logs %s -n %s\n", CertManagerDeployment, CertManagerNamespace)
log.Info("kubectl describe certificates\n")
log.Info("kubectl describe issuers\n")
}
return nil
}
func (o *UpgradeIngressOptions) getExistingIngressRules() (map[string]string, error) {
existingIngressNames := map[string]string{}
var confirmMessage string
if o.Cluster {
confirmMessage = "Existing ingress rules found in the cluster. Confirm to delete all and recreate them"
ings, err := o.kubeClient.ExtensionsV1beta1().Ingresses("").List(metav1.ListOptions{})
if err != nil {
return existingIngressNames, fmt.Errorf("cannot list all ingresses in cluster: %v", err)
}
for _, i := range ings.Items {
if i.Annotations[kube.ExposeGeneratedByAnnotation] == Exposecontroller {
existingIngressNames[i.Name] = i.Namespace
}
}
nsList, err := o.kubeClient.CoreV1().Namespaces().List(metav1.ListOptions{})
for _, n := range nsList.Items {
o.TargetNamespaces = append(o.TargetNamespaces, n.Name)
}
} else if len(o.Namespaces) > 0 {
confirmMessage = fmt.Sprintf("Existing ingress rules found in namespaces %v namespace. Confirm to delete and recreate them", o.Namespaces)
// loop round each
for _, n := range o.Namespaces {
ings, err := o.kubeClient.ExtensionsV1beta1().Ingresses(n).List(metav1.ListOptions{})
if err != nil {
return existingIngressNames, fmt.Errorf("cannot list all ingresses in cluster: %v", err)
}
for _, i := range ings.Items {
if i.Annotations[kube.ExposeGeneratedByAnnotation] == Exposecontroller {
existingIngressNames[i.Name] = i.Namespace
}
}
o.TargetNamespaces = append(o.TargetNamespaces, n)
}
} else {
confirmMessage = "Existing ingress rules found in current namespace. Confirm to delete and recreate them"
// fall back to current ns only
log.Infof("looking for existing ingress rules in current namesace %s\n", o.currentNamespace)
ings, err := o.kubeClient.ExtensionsV1beta1().Ingresses(o.currentNamespace).List(metav1.ListOptions{})
if err != nil {
return existingIngressNames, fmt.Errorf("cannot list all ingresses in cluster: %v", err)
}
for _, i := range ings.Items {
if i.Annotations[kube.ExposeGeneratedByAnnotation] == Exposecontroller {
existingIngressNames[i.Name] = i.Namespace
}
}
o.TargetNamespaces = append(o.TargetNamespaces, o.currentNamespace)
}
confirm := &survey.Confirm{
Message: confirmMessage,
Default: true,
}
flag := true
err := survey.AskOne(confirm, &flag, nil)
if err != nil {
return existingIngressNames, err
}
if !flag {
return existingIngressNames, errors.New("Not able to automatically delete existing ingress rules. Either delete manually or change the scope the command should run in")
}
return existingIngressNames, nil
}
func (o *UpgradeIngressOptions) confirmExposecontrollerConfig() error {
// get current ingress config to use as existing defaults
devNamespace, _, err := kube.GetDevNamespace(o.kubeClient, o.currentNamespace)
if err != nil {
return fmt.Errorf("cannot find a dev team namespace to get existing exposecontroller config from. %v", err)
}
o.IngressConfig, err = kube.GetIngressConfig(o.kubeClient, devNamespace)
if err != nil {
// carry on as it just means we dont have any defaults
}
o.IngressConfig.Exposer, err = util.PickNameWithDefault([]string{"Ingress", "Route"}, "Expose type", o.IngressConfig.Exposer)
if err != nil {
return err
}
o.IngressConfig.Domain, err = util.PickValue("Domain:", o.IngressConfig.Domain, true)
if err != nil {
return err
}
if !strings.HasSuffix(o.IngressConfig.Domain, "nip.io") {
o.IngressConfig.TLS = util.Confirm("If your network is publicly available would you like to enable cluster wide TLS?", true, "Enables cert-manager and configures TLS with signed certificates from LetsEncrypt")
if o.IngressConfig.TLS {
clusterIssuer, err := util.PickNameWithDefault([]string{"prod", "staging"}, "Use LetsEncrypt staging or production? Warning if testing use staging else you may be rate limited:", "staging")
if err != nil {
return err
}
o.IngressConfig.Issuer = "letsencrypt-" + clusterIssuer
if o.IngressConfig.Email == "" {
email1, err := o.getCommandOutput("", "git", "config", "user.email")
if err != nil {
return err
}
o.IngressConfig.Email = strings.TrimSpace(email1)
}
o.IngressConfig.Email, err = util.PickValue("Email address to register with LetsEncrypt:", o.IngressConfig.Email, true)
if err != nil {
return err
}
}
}
return nil
}
func (o *UpgradeIngressOptions) recreateIngressRules() error {
devNamespace, _, err := kube.GetDevNamespace(o.kubeClient, o.currentNamespace)
if err != nil {
return fmt.Errorf("cannot find a dev team namespace to get existing exposecontroller config from. %v", err)
}
for _, n := range o.TargetNamespaces {
o.cleanExposecontrollerReources(n)
err := o.cleanTLSSecrets(n)
if err != nil {
return err
}
err = kube.CleanCertmanagerResources(o.kubeClient, n, o.IngressConfig)
if err != nil {
return err
}
err = o.runExposecontroller(devNamespace, n, o.IngressConfig)
if err != nil {
return err
}
}
return nil
}
func (o *UpgradeIngressOptions) ensureCertmanagerSetup() error {
if !o.SkipCertManager {
log.Infof("Looking for %s deployment in namespace %s\n", CertManagerDeployment, CertManagerNamespace)
_, err := kube.GetDeploymentPods(o.kubeClient, CertManagerDeployment, CertManagerNamespace)
if err != nil {
ok := util.Confirm("CertManager deployment not found, shall we install it now?", true, "CertManager automatically configures Ingress rules with TLS using signed certificates from LetsEncrypt")
if ok {
values := []string{"rbac.create=true", "ingressShim.extraArgs='{--default-issuer-name=letsencrypt-staging,--default-issuer-kind=Issuer}'"}
err = o.installChart("cert-manager", "stable/cert-manager", "", CertManagerNamespace, true, values)
if err != nil {
return fmt.Errorf("CertManager deployment failed: %v", err)
}
log.Info("waiting for CertManager deployment to be ready, this can take a few minutes\n")
err = kube.WaitForDeploymentToBeReady(o.kubeClient, CertManagerDeployment, CertManagerNamespace, 10*time.Minute)
if err != nil {
return err
}
}
}
}
return nil
}
func (o *UpgradeIngressOptions) annotateExposedServicesWithCertManager() error {
for _, n := range o.TargetNamespaces {
err := kube.AnnotateNamespaceServicesWithCertManager(o.kubeClient, n, o.IngressConfig.Issuer)
if err != nil {
return err
}
}
return nil
}
func (o *UpgradeIngressOptions) cleanServiceAnnotations() error {
for _, n := range o.TargetNamespaces {
err := kube.CleanServiceAnnotations(o.kubeClient, n)
if err != nil {
return err
}
}
return nil
}
func (o *UpgradeIngressOptions) cleanTLSSecrets(ns string) error {
// delete the tls related secrets so we dont reuse old ones when switching from http to https
secrets, err := o.kubeClient.CoreV1().Secrets(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, s := range secrets.Items {
if strings.HasPrefix(s.Name, "tls-") {
err := o.kubeClient.CoreV1().Secrets(ns).Delete(s.Name, &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete tls secret %s: %v", s.Name, err)
}
}
}
return nil
}