-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathdeployment.go
69 lines (61 loc) · 1.97 KB
/
deployment.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
package kubeapi
import (
"fmt"
"time"
apps "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetDeployment method returns a deployment data structure for a
// given deployment in a given namespace.
func (k *KubeAPI) GetDeployment(namespace, deployment string) (*apps.Deployment, error) {
return k.Client.AppsV1().Deployments(namespace).Get(deployment, metav1.GetOptions{})
}
// GetDeploymentPods method returns the pods associated with a
// given deployment in a given namespace.
func (k *KubeAPI) GetDeploymentPods(namespace, deployment string) ([]string, error) {
d, err := k.Client.AppsV1().Deployments(namespace).Get(deployment, metav1.GetOptions{})
if err != nil {
return nil, err
}
var podList []string
label := createLabel(d.Labels)
pods, err := k.Client.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: label})
if err != nil {
return nil, err
}
for _, pod := range pods.Items {
podList = append(podList, pod.Name)
}
return podList, nil
}
func (k *KubeAPI) ListDeployments(namespace, label string) (*apps.DeploymentList, error) {
return k.Client.AppsV1().Deployments(namespace).List(
metav1.ListOptions{
LabelSelector: label,
})
}
// DeleteDeployment method deletes a given deployment in a given
// namespace.
func (k *KubeAPI) DeleteDeployment(namespace, deployment string) error {
return k.Client.AppsV1().Deployments(namespace).Delete(deployment, &metav1.DeleteOptions{})
}
// IsDeploymentReady method tests if a deployment is ready to be
// used.
func (k *KubeAPI) IsDeploymentReady(namespace, deployment string) (bool, error) {
timeout := time.After(k.Timeout)
tick := time.Tick(500 * time.Millisecond)
for {
select {
case <-timeout:
return false, fmt.Errorf("Timed out waiting for deployment to run: %s", deployment)
case <-tick:
d, err := k.GetDeployment(namespace, deployment)
if err != nil {
return false, err
}
if d.Status.Replicas == d.Status.ReadyReplicas {
return true, nil
}
}
}
}