-
Notifications
You must be signed in to change notification settings - Fork 784
/
services.go
616 lines (556 loc) · 17.9 KB
/
services.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package services
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
tools_watch "k8s.io/client-go/tools/watch"
)
const (
ExposeAnnotation = "fabric8.io/expose"
ExposeURLAnnotation = "fabric8.io/exposeUrl"
ExposeGeneratedByAnnotation = "fabric8.io/generated-by"
ExposeIngressName = "fabric8.io/ingress.name"
JenkinsXSkipTLSAnnotation = "jenkins-x.io/skip.tls"
ExposeIngressAnnotation = "fabric8.io/ingress.annotations"
CertManagerAnnotation = "certmanager.k8s.io/issuer"
CertManagerClusterAnnotation = "certmanager.k8s.io/cluster-issuer"
ServiceAppLabel = "app"
)
type ServiceURL struct {
Name string
URL string
}
func GetServices(client kubernetes.Interface, ns string) (map[string]*v1.Service, error) {
answer := map[string]*v1.Service{}
list, err := client.CoreV1().Services(ns).List(meta_v1.ListOptions{})
if err != nil {
return answer, fmt.Errorf("failed to load Services %s", err)
}
for _, r := range list.Items {
name := r.Name
copy := r
answer[name] = ©
}
return answer, nil
}
// GetServicesByName returns a list of Service objects from a list of service names
func GetServicesByName(client kubernetes.Interface, ns string, services []string) ([]*v1.Service, error) {
answer := make([]*v1.Service, 0)
svcList, err := client.CoreV1().Services(ns).List(meta_v1.ListOptions{})
if err != nil {
return answer, errors.Wrapf(err, "listing the services in namespace %q", ns)
}
for _, s := range svcList.Items {
i := util.StringArrayIndex(services, s.GetName())
if i > 0 {
copy := s
answer = append(answer, ©)
}
}
return answer, nil
}
func GetServiceNames(client kubernetes.Interface, ns string, filter string) ([]string, error) {
names := []string{}
list, err := client.CoreV1().Services(ns).List(meta_v1.ListOptions{})
if err != nil {
return names, fmt.Errorf("failed to load Services %s", err)
}
for _, r := range list.Items {
name := r.Name
if filter == "" || strings.Contains(name, filter) {
names = append(names, name)
}
}
sort.Strings(names)
return names, nil
}
func GetServiceURLFromMap(services map[string]*v1.Service, name string) string {
return GetServiceURL(services[name])
}
func FindServiceURL(client kubernetes.Interface, namespace string, name string) (string, error) {
log.Logger().Debugf("Finding service url for %s in namespace %s", name, namespace)
svc, err := client.CoreV1().Services(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return "", errors.Wrapf(err, "finding the service %s in namespace %s", name, namespace)
}
answer := GetServiceURL(svc)
if answer != "" {
log.Logger().Debugf("Found service url %s", answer)
return answer, nil
}
log.Logger().Debugf("Couldn't find service url, attempting to look up via ingress")
// lets try find the service via Ingress
ing, err := client.ExtensionsV1beta1().Ingresses(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
log.Logger().Debugf("Unable to finding ingress for %s in namespace %s - err %s", name, namespace, err)
return "", errors.Wrapf(err, "getting ingress for service %q in namespace %s", name, namespace)
}
url := IngressURL(ing)
if url == "" {
log.Logger().Debugf("Unable to find service url via ingress for %s in namespace %s", name, namespace)
}
return url, nil
}
func FindIngressURL(client kubernetes.Interface, namespace string, name string) (string, error) {
log.Logger().Debugf("Finding ingress url for %s in namespace %s", name, namespace)
// lets try find the service via Ingress
ing, err := client.ExtensionsV1beta1().Ingresses(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
log.Logger().Debugf("Error finding ingress for %s in namespace %s - err %s", name, namespace, err)
return "", nil
}
url := IngressURL(ing)
if url == "" {
log.Logger().Debugf("Unable to find url via ingress for %s in namespace %s", name, namespace)
}
return url, nil
}
// IngressURL returns the URL for the ingres
func IngressURL(ing *v1beta1.Ingress) string {
if ing != nil {
if len(ing.Spec.Rules) > 0 {
rule := ing.Spec.Rules[0]
hostname := rule.Host
for _, tls := range ing.Spec.TLS {
for _, h := range tls.Hosts {
if h != "" {
url := "https://" + h
log.Logger().Debugf("found service url %s", url)
return url
}
}
}
if hostname != "" {
url := "http://" + hostname
log.Logger().Debugf("found service url %s", url)
return url
}
}
}
return ""
}
// IngressHost returns the host for the ingres
func IngressHost(ing *v1beta1.Ingress) string {
if ing != nil {
if len(ing.Spec.Rules) > 0 {
rule := ing.Spec.Rules[0]
hostname := rule.Host
for _, tls := range ing.Spec.TLS {
for _, h := range tls.Hosts {
if h != "" {
return h
}
}
}
if hostname != "" {
return hostname
}
}
}
return ""
}
// IngressProtocol returns the scheme (https / http) for the Ingress
func IngressProtocol(ing *v1beta1.Ingress) string {
if ing != nil && len(ing.Spec.TLS) == 0 {
return "http"
}
return "https"
}
func FindServiceHostname(client kubernetes.Interface, namespace string, name string) (string, error) {
// lets try find the service via Ingress
ing, err := client.ExtensionsV1beta1().Ingresses(namespace).Get(name, meta_v1.GetOptions{})
if ing != nil && err == nil {
if len(ing.Spec.Rules) > 0 {
rule := ing.Spec.Rules[0]
hostname := rule.Host
for _, tls := range ing.Spec.TLS {
for _, h := range tls.Hosts {
if h != "" {
return h, nil
}
}
}
if hostname != "" {
return hostname, nil
}
}
}
return "", nil
}
// FindService looks up a service by name across all namespaces
func FindService(client kubernetes.Interface, name string) (*v1.Service, error) {
nsl, err := client.CoreV1().Namespaces().List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
for _, ns := range nsl.Items {
svc, err := client.CoreV1().Services(ns.GetName()).Get(name, meta_v1.GetOptions{})
if err == nil {
return svc, nil
}
}
return nil, errors.New("Service not found!")
}
// GetServiceURL returns the
func GetServiceURL(svc *v1.Service) string {
url := ""
if svc != nil && svc.Annotations != nil {
url = svc.Annotations[ExposeURLAnnotation]
}
if url == "" {
scheme := "http"
for _, port := range svc.Spec.Ports {
if port.Port == 443 {
scheme = "https"
break
}
}
// lets check if its a LoadBalancer
if svc.Spec.Type == v1.ServiceTypeLoadBalancer {
for _, ing := range svc.Status.LoadBalancer.Ingress {
if ing.IP != "" {
return scheme + "://" + ing.IP + "/"
}
if ing.Hostname != "" {
return scheme + "://" + ing.Hostname + "/"
}
}
}
}
return url
}
// FindServiceSchemePort parses the service definition and interprets http scheme in the absence of an external ingress
func FindServiceSchemePort(client kubernetes.Interface, namespace string, name string) (string, string, error) {
svc, err := client.CoreV1().Services(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return "", "", errors.Wrapf(err, "failed to find service %s in namespace %s", name, namespace)
}
return ExtractServiceSchemePort(svc)
}
func GetServiceURLFromName(c kubernetes.Interface, name, ns string) (string, error) {
return FindServiceURL(c, ns, name)
}
func FindServiceURLs(client kubernetes.Interface, namespace string) ([]ServiceURL, error) {
options := meta_v1.ListOptions{}
urls := []ServiceURL{}
svcs, err := client.CoreV1().Services(namespace).List(options)
if err != nil {
return urls, err
}
for _, s := range svcs.Items {
svc := s
url := GetServiceURL(&svc)
if url == "" {
url, _ = FindServiceURL(client, namespace, svc.Name)
}
if len(url) > 0 {
urls = append(urls, ServiceURL{
Name: svc.Name,
URL: url,
})
}
}
return urls, nil
}
// WaitForExternalIP waits for the pods of a deployment to become ready
func WaitForExternalIP(client kubernetes.Interface, name, namespace string, timeout time.Duration) error {
options := meta_v1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", name).String(),
}
w, err := client.CoreV1().Services(namespace).Watch(options)
if err != nil {
return err
}
defer w.Stop()
condition := func(event watch.Event) (bool, error) {
svc := event.Object.(*v1.Service)
return HasExternalAddress(svc), nil
}
ctx, _ := context.WithTimeout(context.Background(), timeout)
_, err = tools_watch.UntilWithoutRetry(ctx, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("service %s never became ready", name)
}
return nil
}
// WaitForService waits for a service to become ready
func WaitForService(client kubernetes.Interface, name, namespace string, timeout time.Duration) error {
options := meta_v1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", name).String(),
}
w, err := client.CoreV1().Services(namespace).Watch(options)
if err != nil {
return err
}
defer w.Stop()
condition := func(event watch.Event) (bool, error) {
svc := event.Object.(*v1.Service)
return svc.GetName() == name, nil
}
ctx, _ := context.WithTimeout(context.Background(), timeout)
_, err = tools_watch.UntilWithoutRetry(ctx, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("service %s never became ready", name)
}
return nil
}
func HasExternalAddress(svc *v1.Service) bool {
for _, v := range svc.Status.LoadBalancer.Ingress {
if v.IP != "" || v.Hostname != "" {
return true
}
}
return false
}
func CreateServiceLink(client kubernetes.Interface, currentNamespace, targetNamespace, serviceName, externalURL string) error {
annotations := make(map[string]string)
annotations[ExposeURLAnnotation] = externalURL
svc := v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
Name: serviceName,
Namespace: currentNamespace,
Annotations: annotations,
},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeExternalName,
ExternalName: fmt.Sprintf("%s.%s.svc.cluster.local", serviceName, targetNamespace),
},
}
_, err := client.CoreV1().Services(currentNamespace).Create(&svc)
if err != nil {
return err
}
return nil
}
func DeleteService(client *kubernetes.Clientset, namespace string, serviceName string) error {
return client.CoreV1().Services(namespace).Delete(serviceName, &meta_v1.DeleteOptions{})
}
func GetService(client kubernetes.Interface, currentNamespace, targetNamespace, serviceName string) error {
svc := v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
Name: serviceName,
Namespace: currentNamespace,
},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeExternalName,
ExternalName: fmt.Sprintf("%s.%s.svc.cluster.local", serviceName, targetNamespace),
},
}
_, err := client.CoreV1().Services(currentNamespace).Create(&svc)
if err != nil {
return err
}
return nil
}
func IsServicePresent(c kubernetes.Interface, name, ns string) (bool, error) {
svc, err := c.CoreV1().Services(ns).Get(name, meta_v1.GetOptions{})
if err != nil || svc == nil {
return false, err
}
return true, nil
}
// GetServiceAppName retrieves the application name from the service labels
func GetServiceAppName(c kubernetes.Interface, name, ns string) (string, error) {
svc, err := c.CoreV1().Services(ns).Get(name, meta_v1.GetOptions{})
if err != nil || svc == nil {
return "", errors.Wrapf(err, "retrieving service %q", name)
}
return ServiceAppName(svc), nil
}
// ServiceAppName retrives the application name from service labels. If no app lable exists,
// it returns the service name
func ServiceAppName(service *v1.Service) string {
if annotations := service.Annotations; annotations != nil {
ingName, ok := annotations[ExposeIngressName]
if ok {
return ingName
}
}
if labels := service.Labels; labels != nil {
app, ok := labels[ServiceAppLabel]
if ok {
return app
}
}
return service.GetName()
}
// AnnotateServicesWithCertManagerIssuer adds the cert-manager annotation to the services from the given namespace. If a list of
// services is provided, it will apply the annotation only to that specific services.
func AnnotateServicesWithCertManagerIssuer(c kubernetes.Interface, ns, issuer string, clusterIssuer bool, services ...string) ([]*v1.Service, error) {
result := make([]*v1.Service, 0)
svcList, err := GetServices(c, ns)
if err != nil {
return result, err
}
for _, s := range svcList {
// annotate only the services present in the list, if the list is empty annotate all services
if len(services) > 0 {
i := util.StringArrayIndex(services, s.GetName())
if i < 0 {
continue
}
}
if s.Annotations[ExposeAnnotation] == "true" && s.Annotations[JenkinsXSkipTLSAnnotation] != "true" {
existingAnnotations, _ := s.Annotations[ExposeIngressAnnotation]
// if no existing `fabric8.io/ingress.annotations` initialise and add else update with ClusterIssuer
certManagerAnnotation := CertManagerAnnotation
if clusterIssuer == true {
certManagerAnnotation = CertManagerClusterAnnotation
}
if len(existingAnnotations) > 0 {
s.Annotations[ExposeIngressAnnotation] = existingAnnotations + "\n" + certManagerAnnotation + ": " + issuer
} else {
s.Annotations[ExposeIngressAnnotation] = certManagerAnnotation + ": " + issuer
}
s, err = c.CoreV1().Services(ns).Update(s)
if err != nil {
return result, fmt.Errorf("failed to annotate and update service %s in namespace %s: %v", s.Name, ns, err)
}
result = append(result, s)
}
}
return result, nil
}
// AnnotateServicesWithBasicAuth annotates the services with nginx baisc auth annotations
func AnnotateServicesWithBasicAuth(client kubernetes.Interface, ns string, services ...string) error {
if len(services) == 0 {
return nil
}
svcList, err := GetServices(client, ns)
if err != nil {
return errors.Wrapf(err, "retrieving the services from namespace %q", ns)
}
for _, service := range svcList {
// Check if the service is in the white-list
idx := util.StringArrayIndex(services, service.GetName())
if idx < 0 {
continue
}
if service.Annotations == nil {
service.Annotations = map[string]string{}
}
// Add the required basic authentication annotation for nginx-ingress controller
ingressAnnotations := service.Annotations[ExposeIngressAnnotation]
basicAuthAnnotations := fmt.Sprintf(
"nginx.ingress.kubernetes.io/auth-type: basic\nnginx.ingress.kubernetes.io/auth-secret: %s\nnginx.ingress.kubernetes.io/auth-realm: Authentication is required to access this service",
kube.SecretBasicAuth)
if ingressAnnotations != "" {
ingressAnnotations = ingressAnnotations + "\n" + basicAuthAnnotations
} else {
ingressAnnotations = basicAuthAnnotations
}
service.Annotations[ExposeIngressAnnotation] = ingressAnnotations
_, err = client.CoreV1().Services(ns).Update(service)
if err != nil {
return errors.Wrapf(err, "updating the service %q in namesapce %q", service.GetName(), ns)
}
}
return nil
}
func CleanServiceAnnotations(c kubernetes.Interface, ns string, services ...string) error {
svcList, err := GetServices(c, ns)
if err != nil {
return err
}
for _, s := range svcList {
// clear the annotations only for the services provided in the list if the list
// is not empty, otherwise clear the annotations of all services
if len(services) > 0 {
i := util.StringArrayIndex(services, s.GetName())
if i < 0 {
continue
}
}
if s.Annotations[ExposeAnnotation] == "true" && s.Annotations[JenkinsXSkipTLSAnnotation] != "true" {
// if no existing `fabric8.io/ingress.annotations` initialise and add else update with ClusterIssuer
annotationsForIngress := s.Annotations[ExposeIngressAnnotation]
if len(annotationsForIngress) > 0 {
var newAnnotations []string
annotations := strings.Split(annotationsForIngress, "\n")
for _, element := range annotations {
annotation := strings.SplitN(element, ":", 2)
key, _ := annotation[0], strings.TrimSpace(annotation[1])
if key != CertManagerAnnotation && key != CertManagerClusterAnnotation {
newAnnotations = append(newAnnotations, element)
}
}
annotationsForIngress = ""
for _, v := range newAnnotations {
if len(annotationsForIngress) > 0 {
annotationsForIngress = annotationsForIngress + "\n" + v
} else {
annotationsForIngress = v
}
}
s.Annotations[ExposeIngressAnnotation] = annotationsForIngress
}
delete(s.Annotations, ExposeURLAnnotation)
_, err = c.CoreV1().Services(ns).Update(s)
if err != nil {
return fmt.Errorf("failed to clean service %s annotations in namespace %s: %v", s.Name, ns, err)
}
}
}
return nil
}
// ExtractServiceSchemePort is a utility function to interpret http scheme and port information from k8s service definitions
func ExtractServiceSchemePort(svc *v1.Service) (string, string, error) {
scheme := ""
port := ""
found := false
// Search in order of degrading priority
for _, p := range svc.Spec.Ports {
if p.Port == 443 { // Prefer 443/https if found
scheme = "https"
port = "443"
found = true
break
}
}
if !found {
for _, p := range svc.Spec.Ports {
if p.Port == 80 { // Use 80/http if found
scheme = "http"
port = "80"
found = true
}
}
}
if !found { // No conventional ports, so search for named https ports
for _, p := range svc.Spec.Ports {
if p.Protocol == "TCP" {
if p.Name == "https" {
scheme = "https"
port = strconv.FormatInt(int64(p.Port), 10)
found = true
break
}
}
}
}
if !found { // No conventional ports, so search for named http ports
for _, p := range svc.Spec.Ports {
if p.Name == "http" {
scheme = "http"
port = strconv.FormatInt(int64(p.Port), 10)
found = true
break
}
}
}
return scheme, port, nil
}