forked from gruntwork-io/terratest
/
deployment.go
113 lines (102 loc) · 4.07 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
107
108
109
110
111
112
113
package k8s
import (
"context"
"fmt"
"time"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/wahlfeld/terratest/modules/logger"
"github.com/wahlfeld/terratest/modules/retry"
"github.com/wahlfeld/terratest/modules/testing"
)
// ListDeployments will look for deployments in the given namespace that match the given filters and return them. This will
// fail the test if there is an error.
func ListDeployments(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []appsv1.Deployment {
deployment, err := ListDeploymentsE(t, options, filters)
require.NoError(t, err)
return deployment
}
// ListDeploymentsE will look for deployments in the given namespace that match the given filters and return them.
func ListDeploymentsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]appsv1.Deployment, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
deployments, err := clientset.AppsV1().Deployments(options.Namespace).List(context.Background(), filters)
if err != nil {
return nil, err
}
return deployments.Items, nil
}
// GetDeployment returns a Kubernetes deployment resource in the provided namespace with the given name. This will
// fail the test if there is an error.
func GetDeployment(t testing.TestingT, options *KubectlOptions, deploymentName string) *appsv1.Deployment {
deployment, err := GetDeploymentE(t, options, deploymentName)
require.NoError(t, err)
return deployment
}
// GetDeploymentE returns a Kubernetes deployment resource in the provided namespace with the given name.
func GetDeploymentE(t testing.TestingT, options *KubectlOptions, deploymentName string) (*appsv1.Deployment, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
return clientset.AppsV1().Deployments(options.Namespace).Get(context.Background(), deploymentName, metav1.GetOptions{})
}
// WaitUntilDeploymentAvailableE waits until all pods within the deployment are ready and started,
// retrying the check for the specified amount of times, sleeping
// for the provided duration between each try.
// This will fail the test if there is an error.
func WaitUntilDeploymentAvailable(t testing.TestingT, options *KubectlOptions, deploymentName string, retries int, sleepBetweenRetries time.Duration) {
require.NoError(t, WaitUntilDeploymentAvailableE(t, options, deploymentName, retries, sleepBetweenRetries))
}
// WaitUntilDeploymentAvailableE waits until all pods within the deployment are ready and started,
// retrying the check for the specified amount of times, sleeping
// for the provided duration between each try.
func WaitUntilDeploymentAvailableE(
t testing.TestingT,
options *KubectlOptions,
deploymentName string,
retries int,
sleepBetweenRetries time.Duration,
) error {
statusMsg := fmt.Sprintf("Wait for deployment %s to be provisioned.", deploymentName)
message, err := retry.DoWithRetryE(
t,
statusMsg,
retries,
sleepBetweenRetries,
func() (string, error) {
deployment, err := GetDeploymentE(t, options, deploymentName)
if err != nil {
return "", err
}
if !IsDeploymentAvailable(deployment) {
return "", NewDeploymentNotAvailableError(deployment)
}
return "Deployment is now available", nil
},
)
if err != nil {
logger.Logf(t, "Timedout waiting for Deployment to be provisioned: %s", err)
return err
}
logger.Logf(t, message)
return nil
}
// IsDeploymentAvailable returns true if all pods within the deployment are ready and started
func IsDeploymentAvailable(deploy *appsv1.Deployment) bool {
dc := getDeploymentCondition(deploy, appsv1.DeploymentProgressing)
return dc != nil && dc.Status == v1.ConditionTrue && dc.Reason == "NewReplicaSetAvailable"
}
func getDeploymentCondition(deploy *appsv1.Deployment, cType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for idx := range deploy.Status.Conditions {
dc := &deploy.Status.Conditions[idx]
if dc.Type == cType {
return dc
}
}
return nil
}