Skip to content

Commit

Permalink
✨ Allow setting "UseExistingCluster" via environment
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed May 27, 2019
1 parent 13ee2bc commit 079570f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
Expand All @@ -34,6 +35,7 @@ var log = logf.RuntimeLog.WithName("test-env")

// Default binary path for test framework
const (
envUseExistingCluster = "USE_EXISTING_CLUSTER"
envKubeAPIServerBin = "TEST_ASSET_KUBE_APISERVER"
envEtcdBin = "TEST_ASSET_ETCD"
envKubectlBin = "TEST_ASSET_KUBECTL"
Expand Down Expand Up @@ -87,7 +89,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
Expand All @@ -105,7 +107,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()
Expand All @@ -122,7 +124,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 {
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
Expand Down Expand Up @@ -225,3 +228,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
}

0 comments on commit 079570f

Please sign in to comment.