/
wait.go
125 lines (111 loc) · 3.17 KB
/
wait.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
/*
* *******************************************************************************
* * Copyright (c) 2019 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package k8s
import (
"context"
"errors"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8swatch "k8s.io/apimachinery/pkg/watch"
)
func getErrorMsg(resource, namespace, name, event string) string {
return fmt.Sprintf("Failed to wait for %s %s/%s because it is %s", resource, namespace, name, event)
}
func (cl *Client) WaitForLoadBalancer(namespace, name string, timeoutSeconds int64) (addr string, err error) {
// Get watch handler to observe changes to services
watch, err := cl.CoreV1().Services(namespace).Watch(context.Background(), metav1.ListOptions{TimeoutSeconds: &timeoutSeconds})
if err != nil {
return
}
// Wait for Services to have addresses allocated
for event := range watch.ResultChan() {
if event.Type == k8swatch.Error || event.Type == k8swatch.Deleted {
err = errors.New(getErrorMsg("service", namespace, name, string(event.Type)))
return
}
svc, ok := event.Object.(*corev1.Service)
if !ok {
err = errors.New(getErrorMsg("service", namespace, name, string(event.Type)))
return
}
// Ignore irrelevant service events
if svc.Name != name {
continue
}
// Loadbalancer must be ready
if len(svc.Status.LoadBalancer.Ingress) == 0 {
continue
}
// Check addresses
ip := svc.Status.LoadBalancer.Ingress[0].IP
host := svc.Status.LoadBalancer.Ingress[0].Hostname
if ip != "" {
addr = ip
}
if host != "" {
addr = host
}
if addr == "" {
continue
}
// Return address
watch.Stop()
}
if addr == "" {
err = errors.New("IP and Hostname values were empty")
}
return addr, err
}
func (cl *Client) WaitForPod(namespace, name string, timeoutSeconds int64) error {
// Get watch handler to observe changes to pods
watch, err := cl.CoreV1().Pods(namespace).Watch(context.Background(), metav1.ListOptions{TimeoutSeconds: &timeoutSeconds})
if err != nil {
return err
}
// Wait for pod events
for event := range watch.ResultChan() {
if event.Type == k8swatch.Error || event.Type == k8swatch.Deleted {
return errors.New(getErrorMsg("pod", namespace, name, string(event.Type)))
}
// Get the pod
pod, ok := event.Object.(*corev1.Pod)
if !ok {
return errors.New(getErrorMsg("pod", namespace, name, string(event.Type)))
}
// Check pod is in running state
splitName := strings.Split(pod.Name, "-")
if len(splitName) <= 2 {
continue
}
splitName = splitName[0 : len(splitName)-2]
joinName := strings.Join(splitName, "-")
if joinName != name {
continue
}
if pod.Status.Phase == corev1.PodRunning {
ready := true
for _, cond := range pod.Status.Conditions {
if cond.Status != corev1.ConditionTrue {
ready = false
break
}
}
if ready {
watch.Stop()
}
}
}
return nil
}