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