github.com/someshkoli/terratest@v0.41.1/modules/k8s/secret.go (about)

     1  package k8s
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/gruntwork-io/terratest/modules/logger"
     9  	"github.com/gruntwork-io/terratest/modules/retry"
    10  	"github.com/gruntwork-io/terratest/modules/testing"
    11  	"github.com/stretchr/testify/require"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  )
    15  
    16  // GetSecret returns a Kubernetes secret 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 GetSecret(t testing.TestingT, options *KubectlOptions, secretName string) *corev1.Secret {
    19  	secret, err := GetSecretE(t, options, secretName)
    20  	require.NoError(t, err)
    21  	return secret
    22  }
    23  
    24  // GetSecretE returns a Kubernetes secret resource in the provided namespace with the given name. The namespace used
    25  // is the one provided in the KubectlOptions.
    26  func GetSecretE(t testing.TestingT, options *KubectlOptions, secretName string) (*corev1.Secret, error) {
    27  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return clientset.CoreV1().Secrets(options.Namespace).Get(context.Background(), secretName, metav1.GetOptions{})
    32  }
    33  
    34  // WaitUntilSecretAvailable waits until the secret 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 WaitUntilSecretAvailable(t testing.TestingT, options *KubectlOptions, secretName string, retries int, sleepBetweenRetries time.Duration) {
    37  	statusMsg := fmt.Sprintf("Wait for secret %s to be provisioned.", secretName)
    38  	message := retry.DoWithRetry(
    39  		t,
    40  		statusMsg,
    41  		retries,
    42  		sleepBetweenRetries,
    43  		func() (string, error) {
    44  			_, err := GetSecretE(t, options, secretName)
    45  			if err != nil {
    46  				return "", err
    47  			}
    48  
    49  			return "Secret is now available", nil
    50  		},
    51  	)
    52  	logger.Logf(t, message)
    53  }