From c9aad37929ceefae2c7bccfc3ca27b7c9972f0ae Mon Sep 17 00:00:00 2001 From: adohe Date: Tue, 4 Jun 2019 10:39:41 +0800 Subject: [PATCH] :sparkles: make UseExistingCluster a pointer to support explicitly opt-out in the code --- pkg/envtest/server.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/envtest/server.go b/pkg/envtest/server.go index 2cfedd1e06..eba45c072d 100644 --- a/pkg/envtest/server.go +++ b/pkg/envtest/server.go @@ -90,7 +90,7 @@ type Environment struct { // UseExisting indicates that this environments should use an // existing kubeconfig, instead of trying to stand up a new control plane. // This is useful in cases that need aggregated API servers and the like. - UseExistingCluster bool + UseExistingCluster *bool // ControlPlaneStartTimeout is the maximum duration each controlplane component // may take to start. It defaults to the KUBEBUILDER_CONTROLPLANE_START_TIMEOUT @@ -113,7 +113,7 @@ type Environment struct { // Stop stops a running server func (te *Environment) Stop() error { - if te.UseExistingCluster { + if te.useExistingCluster() { return nil } return te.ControlPlane.Stop() @@ -130,11 +130,7 @@ func (te Environment) getAPIServerFlags() []string { // Start starts a local Kubernetes server and updates te.ApiserverPort with the port it is listening on func (te *Environment) Start() (*rest.Config, error) { - if !te.UseExistingCluster { - // Check USE_EXISTING_CLUSTER env then - te.UseExistingCluster = strings.ToLower(os.Getenv(envUseExistingCluster)) == "true" - } - if te.UseExistingCluster { + if te.useExistingCluster() { log.V(1).Info("using existing cluster") if te.Config == nil { // we want to allow people to pass in their own config, so @@ -256,3 +252,10 @@ func (te *Environment) defaultTimeouts() error { } return nil } + +func (te *Environment) useExistingCluster() bool { + if te.UseExistingCluster == nil { + return strings.ToLower(os.Getenv(envUseExistingCluster)) == "true" + } + return *te.UseExistingCluster == true +}