/
deployments.go
186 lines (157 loc) · 5.44 KB
/
deployments.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
package kube
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/jenkins-x/jx-logging/pkg/log"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
tools_watch "k8s.io/client-go/tools/watch"
)
// GetDeployments get deployments in the given namespace
func GetDeployments(kubeClient kubernetes.Interface, ns string) (map[string]appsv1.Deployment, error) {
answer := map[string]appsv1.Deployment{}
deps, err := kubeClient.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return answer, err
}
for _, d := range deps.Items {
answer[d.Name] = d
}
return answer, nil
}
// GetDeploymentNames get deployment names in the given namespace with filter
func GetDeploymentNames(client kubernetes.Interface, ns string, filter string) ([]string, error) {
names := []string{}
list, err := client.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return names, fmt.Errorf("Failed to load Deployments %s", err)
}
for _, d := range list.Items {
name := d.Name
if filter == "" || strings.Contains(name, filter) {
names = append(names, name)
}
}
sort.Strings(names)
return names, nil
}
// GetDeploymentByRepo get deployment in the given namespace with repo name
func GetDeploymentByRepo(client kubernetes.Interface, ns string, repoName string) (*appsv1.Deployment, error) {
deps, err := client.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, d := range deps.Items {
if strings.HasPrefix(d.Name, repoName) {
return &d, nil
}
}
return nil, fmt.Errorf("no deployment found for repository name '%s'", repoName)
}
// IsDeploymentRunning returns whether this deployment is running
func IsDeploymentRunning(client kubernetes.Interface, name, namespace string) (bool, error) {
options := metav1.GetOptions{}
d, err := client.AppsV1().Deployments(namespace).Get(name, options)
if err != nil {
return false, err
}
if d.Status.ReadyReplicas > 0 {
return true, nil
}
return false, nil
}
// WaitForAllDeploymentsToBeReady waits for the pods of all deployment to become ready in the given namespace
func WaitForAllDeploymentsToBeReady(client kubernetes.Interface, namespace string, timeoutPerDeploy time.Duration) error {
deployList, err := client.AppsV1().Deployments(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
if deployList == nil || len(deployList.Items) == 0 {
return fmt.Errorf("no deployments found in namespace %s", namespace)
}
for _, d := range deployList.Items {
err = WaitForDeploymentToBeReady(client, d.Name, namespace, timeoutPerDeploy)
if err != nil {
log.Logger().Warnf("deployment %s failed to become ready in namespace %s", d.Name, namespace)
}
}
return nil
}
// WaitForDeploymentToBeCreatedAndReady waits for the pods of a deployment created and to become ready
func WaitForDeploymentToBeCreatedAndReady(client kubernetes.Interface, name, namespace string, timeoutPerDeploy time.Duration) error {
options := metav1.ListOptions{FieldSelector: fmt.Sprintf("metadata.name=%s", name)}
w, err := client.AppsV1().Deployments(namespace).Watch(options)
if err != nil {
return err
}
defer w.Stop()
condition := func(event watch.Event) (bool, error) {
running, _ := IsDeploymentRunning(client, name, namespace)
return running, nil
}
ctx, _ := context.WithTimeout(context.Background(), timeoutPerDeploy)
_, err = tools_watch.UntilWithoutRetry(ctx, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("deployment %s never became ready", name)
}
return nil
}
// WaitForDeploymentToBeReady waits for the pods of a deployment to become ready
func WaitForDeploymentToBeReady(client kubernetes.Interface, name, namespace string, timeout time.Duration) error {
d, err := client.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return err
}
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
return err
}
// Skip watching if the deployment is ready
if d.Status.Replicas != d.Status.ReadyReplicas {
options := metav1.ListOptions{LabelSelector: selector.String()}
w, err := client.CoreV1().Pods(namespace).Watch(options)
if err != nil {
return err
}
defer w.Stop()
condition := func(event watch.Event) (bool, error) {
pod := event.Object.(*v1.Pod)
return IsPodReady(pod), nil
}
ctx, _ := context.WithTimeout(context.Background(), timeout)
_, err = tools_watch.UntilWithoutRetry(ctx, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("deployment %s never became ready", name)
}
}
return nil
}
// DeploymentPodCount returns pod counts of deployment
func DeploymentPodCount(client kubernetes.Interface, name, namespace string) (int, error) {
pods, err := GetDeploymentPods(client, name, namespace)
if err == nil {
return len(pods), err
}
return 0, err
}
// GetDeploymentPods returns pods of deployment
func GetDeploymentPods(client kubernetes.Interface, name, namespace string) ([]v1.Pod, error) {
d, err := client.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
return nil, err
}
options := metav1.ListOptions{LabelSelector: selector.String()}
pods, err := client.CoreV1().Pods(namespace).List(options)
return pods.Items, err
}