forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployments.go
151 lines (128 loc) · 4.04 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
package kube
import (
"fmt"
"sort"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/log"
"k8s.io/api/apps/v1beta1"
"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"
)
func GetDeployments(kubeClient kubernetes.Interface, ns string) (map[string]v1beta1.Deployment, error) {
answer := map[string]v1beta1.Deployment{}
deps, err := kubeClient.AppsV1beta1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return answer, err
}
for _, d := range deps.Items {
answer[d.Name] = d
}
return answer, nil
}
func GetDeploymentNames(client kubernetes.Interface, ns string, filter string) ([]string, error) {
names := []string{}
list, err := client.AppsV1beta1().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
}
func GetDeploymentByRepo(client kubernetes.Interface, ns string, repoName string) (*v1beta1.Deployment, error) {
deps, err := client.AppsV1beta1().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)
}
func IsDeploymentRunning(client kubernetes.Interface, name, namespace string) (bool, error) {
options := metav1.GetOptions{}
d, err := client.ExtensionsV1beta1().Deployments(namespace).Get(name, options)
if err != nil {
return false, err
}
if d.Status.ReadyReplicas > 0 {
return true, nil
}
return false, nil
}
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.Warnf("deployment %s failed to become ready in namespace %s", d.Name, namespace)
}
}
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
}
_, err = watch.Until(timeout, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("deployment %s never became ready", name)
}
}
return nil
}
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
}
func GetDeploymentPods(client kubernetes.Interface, name, namespace string) ([]v1.Pod, error) {
d, err := client.ExtensionsV1beta1().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
}