forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
48 lines (38 loc) · 1.31 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
48
package k8s
import (
"testing"
"k8s.io/client-go/kubernetes"
"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
// We have to give an actual configpath for LoadApiClientConfigE to work
kubeConfigPath := options.ConfigPath
if kubeConfigPath == "" {
kubeConfigPath, err = GetKubeConfigPathE(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
}