github.com/mponton/terratest@v0.44.0/modules/k8s/networkpolicy.go (about) 1 package k8s 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/mponton/terratest/modules/logger" 9 "github.com/mponton/terratest/modules/retry" 10 "github.com/mponton/terratest/modules/testing" 11 "github.com/stretchr/testify/require" 12 networkingv1 "k8s.io/api/networking/v1" 13 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 14 ) 15 16 // GetNetworkPolicy returns a Kubernetes networkpolicy resource in the provided namespace with the given name. The namespace used 17 // is the one provided in the KubectlOptions. This will fail the test if there is an error. 18 func GetNetworkPolicy(t testing.TestingT, options *KubectlOptions, networkPolicyName string) *networkingv1.NetworkPolicy { 19 networkPolicy, err := GetNetworkPolicyE(t, options, networkPolicyName) 20 require.NoError(t, err) 21 return networkPolicy 22 } 23 24 // GetNetworkPolicyE returns a Kubernetes networkpolicy resource in the provided namespace with the given name. The namespace used 25 // is the one provided in the KubectlOptions. 26 func GetNetworkPolicyE(t testing.TestingT, options *KubectlOptions, networkPolicyName string) (*networkingv1.NetworkPolicy, error) { 27 clientset, err := GetKubernetesClientFromOptionsE(t, options) 28 if err != nil { 29 return nil, err 30 } 31 return clientset.NetworkingV1().NetworkPolicies(options.Namespace).Get(context.Background(), networkPolicyName, metav1.GetOptions{}) 32 } 33 34 // WaitUntilNetworkPolicyAvailable waits until the networkpolicy is present on the cluster in cases where it is not immediately 35 // available (for example, when using ClusterIssuer to request a certificate). 36 func WaitUntilNetworkPolicyAvailable(t testing.TestingT, options *KubectlOptions, networkPolicyName string, retries int, sleepBetweenRetries time.Duration) { 37 statusMsg := fmt.Sprintf("Wait for networkpolicy %s to be provisioned.", networkPolicyName) 38 message := retry.DoWithRetry( 39 t, 40 statusMsg, 41 retries, 42 sleepBetweenRetries, 43 func() (string, error) { 44 _, err := GetNetworkPolicyE(t, options, networkPolicyName) 45 if err != nil { 46 return "", err 47 } 48 49 return "networkpolicy is now available", nil 50 }, 51 ) 52 logger.Logf(t, message) 53 }