/
resource_k8s_namespace.go
57 lines (45 loc) · 1.58 KB
/
resource_k8s_namespace.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
package testkit
import "testing"
// KubernetesNamespaceProvider is a provider that can provision an Kubernetes namespace.
// Any provider that can create an S3 bucket should implement this interface.
type KubernetesNamespaceProvider interface {
KubernetesNamespace(opts ...KubernetesNamespaceOption) (*KubernetesNamespace, error)
}
type KubernetesNamespace struct {
Name string
}
type KubernetesNamespaceConfig struct {
ID string
KubeconfigPath string
}
type KubernetesNamespaceOption func(*KubernetesNamespaceConfig)
func KubeconfigPath(path string) KubernetesNamespaceOption {
return func(c *KubernetesNamespaceConfig) {
c.KubeconfigPath = path
}
}
// KubernetesNamespace returns a KubernetesNamespace.
// It does so by iterating over the available providers and calling the KubernetesNamespace method on each provider.
// If no provider implements KubernetesNamespace, it fails the test.
// If multiple providers implement KubernetesNamespace, it returns the first successful one.
// If multiple providers implement KubernetesNamespace and all of them fail, it fails the test.
func (tk *TestKit) KubernetesNamespace(t *testing.T, opts ...KubernetesNamespaceOption) *KubernetesNamespace {
t.Helper()
var cp KubernetesNamespaceProvider
for _, p := range tk.availableProviders {
var ok bool
cp, ok = p.(KubernetesNamespaceProvider)
if ok {
ns, err := cp.KubernetesNamespace(opts...)
if err != nil {
t.Logf("unable to get s3 bucket: %v", err)
continue
}
return ns
}
}
if cp == nil {
t.Fatal("no KubernetesNamespaceProvider found")
}
return nil
}