/
deployment.go
106 lines (84 loc) · 3.29 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
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
package globalhelper
import (
"context"
"fmt"
"time"
"github.com/golang/glog"
appsv1 "k8s.io/api/apps/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
typedappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
. "github.com/onsi/gomega"
)
// IsDeploymentReady checks if a deployment is ready.
func IsDeploymentReady(client typedappsv1.AppsV1Interface, namespace, deploymentName string) (bool, error) {
testDeployment, err := client.Deployments(namespace).Get(
context.TODO(),
deploymentName,
metav1.GetOptions{},
)
if err != nil {
return false, err
}
// Ensure the number of ready replicas matches the desired number of replicas.
if testDeployment.Status.AvailableReplicas == *testDeployment.Spec.Replicas &&
testDeployment.Status.ReadyReplicas == *testDeployment.Spec.Replicas {
return true, nil
}
return false, nil
}
// CreateAndWaitUntilDeploymentIsReady creates deployment and wait until all deployment replicas are up and running.
func CreateAndWaitUntilDeploymentIsReady(deployment *appsv1.Deployment,
timeout time.Duration) error {
return createAndWaitUntilDeploymentIsReady(GetAPIClient().K8sClient.AppsV1(), deployment, timeout)
}
// createAndWaitUntilDeploymentIsReady creates deployment and wait until all deployment replicas are up and running.
func createAndWaitUntilDeploymentIsReady(client typedappsv1.AppsV1Interface, deployment *appsv1.Deployment,
timeout time.Duration) error {
runningDeployment, err := client.Deployments(deployment.Namespace).Create(
context.TODO(),
deployment,
metav1.CreateOptions{})
if k8serrors.IsAlreadyExists(err) {
glog.V(5).Info(fmt.Sprintf("deployment %s already exists", deployment.Name))
return nil
} else if err != nil {
return fmt.Errorf("failed to create deployment %q (ns %s): %w", deployment.Name, deployment.Namespace, err)
}
Eventually(func() bool {
status, err := IsDeploymentReady(client, runningDeployment.Namespace, runningDeployment.Name)
if err != nil {
glog.V(5).Info(fmt.Sprintf(
"deployment %s is not ready, retry in 5 seconds", runningDeployment.Name))
return false
}
return status
}, timeout, retryInterval*time.Second).Should(Equal(true), "Deployment is not ready")
return nil
}
// GetRunningDeployment returns a running deployment.
func GetRunningDeployment(namespace, deploymentName string) (*appsv1.Deployment, error) {
return getRunningDeployment(GetAPIClient().K8sClient.AppsV1(), namespace, deploymentName)
}
func getRunningDeployment(client typedappsv1.AppsV1Interface, namespace, deploymentName string) (*appsv1.Deployment, error) {
runningDeployment, err := client.Deployments(namespace).Get(
context.TODO(),
deploymentName,
metav1.GetOptions{},
)
if err != nil {
return nil, fmt.Errorf("failed to get deployment %q (ns %s): %w", deploymentName, namespace, err)
}
return runningDeployment, nil
}
func DeleteDeployment(name, namespace string) error {
return deleteDeployment(GetAPIClient().K8sClient.AppsV1(), name, namespace)
}
func deleteDeployment(client typedappsv1.AppsV1Interface, name, namespace string) error {
err := client.Deployments(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
if k8serrors.IsNotFound(err) {
glog.V(5).Info(fmt.Sprintf("deployment %s is not found", name))
return nil
}
return err
}