forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployments.go
110 lines (93 loc) · 2.86 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
package kube
import (
"fmt"
"sort"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/jx/cmd/log"
"k8s.io/api/apps/v1beta2"
"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.Clientset, ns string) (map[string]v1beta2.Deployment, error) {
answer := map[string]v1beta2.Deployment{}
deps, err := kubeClient.AppsV1beta2().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.Clientset, ns string, filter string) ([]string, error) {
names := []string{}
list, err := client.AppsV1beta2().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 IsDeploymentRunning(client *kubernetes.Clientset, 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.Clientset, namespace string, timeoutPerDeploy time.Duration) error {
deployList, err := client.ExtensionsV1beta1().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 namespase %s", d.Name, namespace)
}
}
return nil
}
// waits for the pods of a deployment to become ready
func WaitForDeploymentToBeReady(client *kubernetes.Clientset, name, namespace string, timeout time.Duration) error {
d, err := client.ExtensionsV1beta1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return err
}
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
return err
}
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
}