forked from gruntwork-io/terratest
/
kubectl_options.go
37 lines (32 loc) · 1.1 KB
/
kubectl_options.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
package k8s
import "testing"
// KubectlOptions represents common options necessary to specify for all Kubectl calls
type KubectlOptions struct {
ContextName string
ConfigPath string
Namespace string
Env map[string]string
}
// NewKubectlOptions will return a pointer to new instance of KubectlOptions with the configured options
func NewKubectlOptions(contextName string, configPath string, namespace string) *KubectlOptions {
return &KubectlOptions{
ContextName: contextName,
ConfigPath: configPath,
Namespace: namespace,
Env: map[string]string{},
}
}
// GetConfigPath will return a sensible default if the config path is not set on the options.
func (kubectlOptions *KubectlOptions) GetConfigPath(t *testing.T) (string, error) {
// We predeclare `err` here so that we can update `kubeConfigPath` in the if block below. Otherwise, go complains
// saying `err` is undefined.
var err error
kubeConfigPath := kubectlOptions.ConfigPath
if kubeConfigPath == "" {
kubeConfigPath, err = GetKubeConfigPathE(t)
if err != nil {
return "", err
}
}
return kubeConfigPath, nil
}