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

     1  package k8s
     2  
     3  import (
     4  	"github.com/mponton/terratest/modules/testing"
     5  )
     6  
     7  // KubectlOptions represents common options necessary to specify for all Kubectl calls
     8  type KubectlOptions struct {
     9  	ContextName   string
    10  	ConfigPath    string
    11  	Namespace     string
    12  	Env           map[string]string
    13  	InClusterAuth bool
    14  }
    15  
    16  // NewKubectlOptions will return a pointer to new instance of KubectlOptions with the configured options
    17  func NewKubectlOptions(contextName string, configPath string, namespace string) *KubectlOptions {
    18  	return &KubectlOptions{
    19  		ContextName: contextName,
    20  		ConfigPath:  configPath,
    21  		Namespace:   namespace,
    22  		Env:         map[string]string{},
    23  	}
    24  }
    25  
    26  // NewKubectlOptionsWithInClusterAuth will return a pointer to a new instance of KubectlOptions with the InClusterAuth field set to true
    27  func NewKubectlOptionsWithInClusterAuth() *KubectlOptions {
    28  	return &KubectlOptions{
    29  		InClusterAuth: true,
    30  	}
    31  }
    32  
    33  // GetConfigPath will return a sensible default if the config path is not set on the options.
    34  func (kubectlOptions *KubectlOptions) GetConfigPath(t testing.TestingT) (string, error) {
    35  	// We predeclare `err` here so that we can update `kubeConfigPath` in the if block below. Otherwise, go complains
    36  	// saying `err` is undefined.
    37  	var err error
    38  
    39  	kubeConfigPath := kubectlOptions.ConfigPath
    40  	if kubeConfigPath == "" {
    41  		kubeConfigPath, err = GetKubeConfigPathE(t)
    42  		if err != nil {
    43  			return "", err
    44  		}
    45  	}
    46  	return kubeConfigPath, nil
    47  }