forked from gruntwork-io/terratest
/
ingress.go
82 lines (72 loc) · 2.87 KB
/
ingress.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
package k8s
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/retry"
)
// ListIngresses will look for Ingress resources in the given namespace that match the given filters and return them.
// This will fail the test if there is an error.
func ListIngresses(t *testing.T, options *KubectlOptions, filters metav1.ListOptions) []extensionsv1beta1.Ingress {
ingresses, err := ListIngressesE(t, options, filters)
require.NoError(t, err)
return ingresses
}
// ListIngressesE will look for Ingress resources in the given namespace that match the given filters and return them.
func ListIngressesE(t *testing.T, options *KubectlOptions, filters metav1.ListOptions) ([]extensionsv1beta1.Ingress, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
resp, err := clientset.ExtensionsV1beta1().Ingresses(options.Namespace).List(filters)
if err != nil {
return nil, err
}
return resp.Items, nil
}
// GetIngress returns a Kubernetes Ingress resource in the provided namespace with the given name. This will fail the
// test if there is an error.
func GetIngress(t *testing.T, options *KubectlOptions, ingressName string) *extensionsv1beta1.Ingress {
ingress, err := GetIngressE(t, options, ingressName)
require.NoError(t, err)
return ingress
}
// GetIngressE returns a Kubernetes Ingress resource in the provided namespace with the given name.
func GetIngressE(t *testing.T, options *KubectlOptions, ingressName string) (*extensionsv1beta1.Ingress, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
return clientset.ExtensionsV1beta1().Ingresses(options.Namespace).Get(ingressName, metav1.GetOptions{})
}
// IsIngressAvailable returns true if the Ingress endpoint is provisioned and available.
func IsIngressAvailable(ingress *extensionsv1beta1.Ingress) bool {
// Ingress is ready if it has at least one endpoint
endpoints := ingress.Status.LoadBalancer.Ingress
return len(endpoints) > 0
}
// WaitUntilIngressAvailable waits until the Ingress resource has an endpoint provisioned for it.
func WaitUntilIngressAvailable(t *testing.T, options *KubectlOptions, ingressName string, retries int, sleepBetweenRetries time.Duration) {
statusMsg := fmt.Sprintf("Wait for ingress %s to be provisioned.", ingressName)
message := retry.DoWithRetry(
t,
statusMsg,
retries,
sleepBetweenRetries,
func() (string, error) {
ingress, err := GetIngressE(t, options, ingressName)
if err != nil {
return "", err
}
if !IsIngressAvailable(ingress) {
return "", IngressNotAvailable{ingress: ingress}
}
return "Ingress is now available", nil
},
)
logger.Logf(t, message)
}