This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 388
/
environment.go
120 lines (94 loc) · 3.13 KB
/
environment.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package framework
import (
"fmt"
"testing"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/hashicorp/consul-helm/test/acceptance/helpers"
"github.com/stretchr/testify/require"
"k8s.io/client-go/kubernetes"
)
const (
DefaultContextName = "default"
SecondaryContextName = "secondary"
)
// TestEnvironment represents the infrastructure environment of the test,
// such as the kubernetes cluster(s) the test is running against
type TestEnvironment interface {
DefaultContext(t *testing.T) TestContext
Context(t *testing.T, name string) TestContext
}
// TestContext represents a specific context a test needs,
// for example, information about a specific Kubernetes cluster.
type TestContext interface {
KubectlOptions(t *testing.T) *k8s.KubectlOptions
KubernetesClient(t *testing.T) kubernetes.Interface
}
type kubernetesEnvironment struct {
contexts map[string]*kubernetesContext
}
func newKubernetesEnvironmentFromConfig(config *TestConfig) *kubernetesEnvironment {
defaultContext := NewContext(config.KubeNamespace, config.Kubeconfig, config.KubeContext)
// Create a kubernetes environment with default context.
kenv := &kubernetesEnvironment{
contexts: map[string]*kubernetesContext{
DefaultContextName: defaultContext,
},
}
// Add secondary context if multi cluster tests are enabled.
if config.EnableMultiCluster {
kenv.contexts[SecondaryContextName] = NewContext(config.SecondaryKubeNamespace, config.SecondaryKubeconfig, config.SecondaryKubeContext)
}
return kenv
}
func (k *kubernetesEnvironment) Context(t *testing.T, name string) TestContext {
ctx, ok := k.contexts[name]
require.Truef(t, ok, fmt.Sprintf("requested context %s not found", name))
return ctx
}
func (k *kubernetesEnvironment) DefaultContext(t *testing.T) TestContext {
ctx, ok := k.contexts[DefaultContextName]
require.Truef(t, ok, "default context not found")
return ctx
}
type kubernetesContext struct {
pathToKubeConfig string
kubeContextName string
namespace string
client kubernetes.Interface
options *k8s.KubectlOptions
logDirectory string
}
func (k kubernetesContext) KubectlOptions(t *testing.T) *k8s.KubectlOptions {
if k.options != nil {
return k.options
}
k.options = &k8s.KubectlOptions{
ContextName: k.kubeContextName,
ConfigPath: k.pathToKubeConfig,
Namespace: k.namespace,
}
if k.namespace == "" {
// Otherwise, get current context from config
configPath, err := k.options.GetConfigPath(t)
require.NoError(t, err)
rawConfig, err := k8s.LoadConfigFromPath(configPath).RawConfig()
require.NoError(t, err)
contextName := helpers.KubernetesContextFromOptions(t, k.options)
k.options.Namespace = rawConfig.Contexts[contextName].Namespace
}
return k.options
}
func (k kubernetesContext) KubernetesClient(t *testing.T) kubernetes.Interface {
if k.client != nil {
return k.client
}
k.client = helpers.KubernetesClientFromOptions(t, k.KubectlOptions(t))
return k.client
}
func NewContext(namespace, pathToKubeConfig, kubeContextName string) *kubernetesContext {
return &kubernetesContext{
namespace: namespace,
pathToKubeConfig: pathToKubeConfig,
kubeContextName: kubeContextName,
}
}