github.com/mponton/terratest@v0.44.0/modules/k8s/deployment.go (about) 1 package k8s 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/stretchr/testify/require" 9 appsv1 "k8s.io/api/apps/v1" 10 v1 "k8s.io/api/core/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 13 "github.com/mponton/terratest/modules/logger" 14 "github.com/mponton/terratest/modules/retry" 15 "github.com/mponton/terratest/modules/testing" 16 ) 17 18 // ListDeployments will look for deployments in the given namespace that match the given filters and return them. This will 19 // fail the test if there is an error. 20 func ListDeployments(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []appsv1.Deployment { 21 deployment, err := ListDeploymentsE(t, options, filters) 22 require.NoError(t, err) 23 return deployment 24 } 25 26 // ListDeploymentsE will look for deployments in the given namespace that match the given filters and return them. 27 func ListDeploymentsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]appsv1.Deployment, error) { 28 clientset, err := GetKubernetesClientFromOptionsE(t, options) 29 if err != nil { 30 return nil, err 31 } 32 deployments, err := clientset.AppsV1().Deployments(options.Namespace).List(context.Background(), filters) 33 if err != nil { 34 return nil, err 35 } 36 return deployments.Items, nil 37 } 38 39 // GetDeployment returns a Kubernetes deployment resource in the provided namespace with the given name. This will 40 // fail the test if there is an error. 41 func GetDeployment(t testing.TestingT, options *KubectlOptions, deploymentName string) *appsv1.Deployment { 42 deployment, err := GetDeploymentE(t, options, deploymentName) 43 require.NoError(t, err) 44 return deployment 45 } 46 47 // GetDeploymentE returns a Kubernetes deployment resource in the provided namespace with the given name. 48 func GetDeploymentE(t testing.TestingT, options *KubectlOptions, deploymentName string) (*appsv1.Deployment, error) { 49 clientset, err := GetKubernetesClientFromOptionsE(t, options) 50 if err != nil { 51 return nil, err 52 } 53 return clientset.AppsV1().Deployments(options.Namespace).Get(context.Background(), deploymentName, metav1.GetOptions{}) 54 } 55 56 // WaitUntilDeploymentAvailableE waits until all pods within the deployment are ready and started, 57 // retrying the check for the specified amount of times, sleeping 58 // for the provided duration between each try. 59 // This will fail the test if there is an error. 60 func WaitUntilDeploymentAvailable(t testing.TestingT, options *KubectlOptions, deploymentName string, retries int, sleepBetweenRetries time.Duration) { 61 require.NoError(t, WaitUntilDeploymentAvailableE(t, options, deploymentName, retries, sleepBetweenRetries)) 62 } 63 64 // WaitUntilDeploymentAvailableE waits until all pods within the deployment are ready and started, 65 // retrying the check for the specified amount of times, sleeping 66 // for the provided duration between each try. 67 func WaitUntilDeploymentAvailableE( 68 t testing.TestingT, 69 options *KubectlOptions, 70 deploymentName string, 71 retries int, 72 sleepBetweenRetries time.Duration, 73 ) error { 74 statusMsg := fmt.Sprintf("Wait for deployment %s to be provisioned.", deploymentName) 75 message, err := retry.DoWithRetryE( 76 t, 77 statusMsg, 78 retries, 79 sleepBetweenRetries, 80 func() (string, error) { 81 deployment, err := GetDeploymentE(t, options, deploymentName) 82 if err != nil { 83 return "", err 84 } 85 if !IsDeploymentAvailable(deployment) { 86 return "", NewDeploymentNotAvailableError(deployment) 87 } 88 return "Deployment is now available", nil 89 }, 90 ) 91 if err != nil { 92 logger.Logf(t, "Timedout waiting for Deployment to be provisioned: %s", err) 93 return err 94 } 95 logger.Logf(t, message) 96 return nil 97 } 98 99 // IsDeploymentAvailable returns true if all pods within the deployment are ready and started 100 func IsDeploymentAvailable(deploy *appsv1.Deployment) bool { 101 for _, dc := range deploy.Status.Conditions { 102 if dc.Type == appsv1.DeploymentProgressing && 103 dc.Status == v1.ConditionTrue && 104 dc.Reason == "NewReplicaSetAvailable" { 105 return true 106 } 107 } 108 109 return false 110 }