github.com/mponton/terratest@v0.44.0/modules/k8s/configmap.go (about)

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