From 7005c819d67b17d34aee05f88c500e1f24fb7b62 Mon Sep 17 00:00:00 2001 From: adohe Date: Sun, 2 Jun 2019 15:51:31 +0800 Subject: [PATCH] :sparkles: make UseExistingCluster a pointer to support explicitly opt-out in the code --- pkg/envtest/server.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/envtest/server.go b/pkg/envtest/server.go index 2cfedd1e06..f1e98cf2c5 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 != nil && *te.UseExistingCluster { return nil } return te.ControlPlane.Stop() @@ -130,11 +130,8 @@ 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 { + useExistingCluster := te.useExistingCluster() + if 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 +253,11 @@ func (te *Environment) defaultTimeouts() error { } return nil } + +func (te *Environment) useExistingCluster() bool { + if te.UseExistingCluster == nil { + useExistingCluster := strings.ToLower(os.Getenv(envUseExistingCluster)) == "true" + te.UseExistingCluster = &useExistingCluster + } + return *te.UseExistingCluster == true +} \ No newline at end of file