github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/client.go (about)

     1  package k8s
     2  
     3  import (
     4  	"k8s.io/client-go/kubernetes"
     5  
     6  	// The following line loads the gcp plugin which is required to authenticate against GKE clusters.
     7  	// See: https://github.com/kubernetes/client-go/issues/242
     8  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
     9  
    10  	"github.com/terraform-modules-krish/terratest/modules/logger"
    11  	"github.com/terraform-modules-krish/terratest/modules/testing"
    12  )
    13  
    14  // GetKubernetesClientE returns a Kubernetes API client that can be used to make requests.
    15  func GetKubernetesClientE(t testing.TestingT) (*kubernetes.Clientset, error) {
    16  	kubeConfigPath, err := GetKubeConfigPathE(t)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	options := NewKubectlOptions("", kubeConfigPath, "default")
    22  	return GetKubernetesClientFromOptionsE(t, options)
    23  }
    24  
    25  // GetKubernetesClientFromOptionsE returns a Kubernetes API client given a configured KubectlOptions object.
    26  func GetKubernetesClientFromOptionsE(t testing.TestingT, options *KubectlOptions) (*kubernetes.Clientset, error) {
    27  	var err error
    28  
    29  	kubeConfigPath, err := options.GetConfigPath(t)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	logger.Logf(t, "Configuring kubectl using config file %s with context %s", kubeConfigPath, options.ContextName)
    34  	// Load API config (instead of more low level ClientConfig)
    35  	config, err := LoadApiClientConfigE(kubeConfigPath, options.ContextName)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	clientset, err := kubernetes.NewForConfig(config)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return clientset, nil
    46  }