Skip to content

Commit 3daf63b

Browse files
committed
Allow setting UseExistingCluster via environment
1 parent 85a065f commit 3daf63b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pkg/envtest/envtest_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var env *Environment
3333

3434
var _ = BeforeSuite(func(done Done) {
3535
logf.SetLogger(logf.ZapLoggerTo(GinkgoWriter, true))
36-
env = &Environment{}
36+
env = NewEnvironment()
3737
_, err := env.Start()
3838
Expect(err).NotTo(HaveOccurred())
3939

pkg/envtest/server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222
"path/filepath"
23+
"strings"
2324
"time"
2425

2526
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
@@ -30,6 +31,7 @@ import (
3031

3132
// Default binary path for test framework
3233
const (
34+
envUseExistingCluster = "USE_EXISTING_CLUSTER"
3335
envKubeAPIServerBin = "TEST_ASSET_KUBE_APISERVER"
3436
envEtcdBin = "TEST_ASSET_ETCD"
3537
envKubectlBin = "TEST_ASSET_KUBECTL"
@@ -97,6 +99,26 @@ type Environment struct {
9799
KubeAPIServerFlags []string
98100
}
99101

102+
// NewEnvironment creates a new instance of the Kubernetes test environment, and configures it
103+
// to use the existing cluster as defined in the current `~/.kube/config` profile if
104+
// the environment variable flag is set to `true`
105+
func NewEnvironment(crds ...string) *Environment {
106+
useExisting := strings.ToLower(os.Getenv(envUseExistingCluster)) == "true"
107+
108+
e := &Environment{
109+
UseExistingCluster: useExisting,
110+
}
111+
112+
// we do not load CRD's when using existing cluster
113+
if useExisting {
114+
return e
115+
}
116+
117+
// set CRD's paths
118+
e.CRDDirectoryPaths = crds
119+
return e
120+
}
121+
100122
// Stop stops a running server
101123
func (te *Environment) Stop() error {
102124
if te.UseExistingCluster {

0 commit comments

Comments
 (0)