Skip to content

Commit

Permalink
✨ allow pass the assets path via the enviroment config
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila Macedo committed Oct 17, 2020
1 parent bd97e08 commit 8450022
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ It's possible to override some defaults, by setting the following environment va
KUBEBUILDER_CONTROLPLANE_START_TIMEOUT (string supported by time.ParseDuration): timeout for test control plane to start. Defaults to 20s.
KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT (string supported by time.ParseDuration): timeout for test control plane to start. Defaults to 20s.
KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT (boolean): if set to true, the control plane's stdout and stderr are attached to os.Stdout and os.Stderr
*/
const (
envUseExistingCluster = "USE_EXISTING_CLUSTER"
Expand All @@ -62,14 +61,19 @@ const (
defaultKubebuilderControlPlaneStopTimeout = 20 * time.Second
)

// Default binary path for test framework
func defaultAssetPath(binary string) string {
assetPath := os.Getenv(envKubebuilderPath)
if assetPath == "" {
assetPath = defaultKubebuilderPath
// getBinAssetPath will return the path for the binary informed or an error if not be possible.
// to find the bin.
func (te *Environment) getBinAssetPath(binary string) string {
if hasBinary(binary, te.BinaryDirectoryPath) {
return filepath.Join(te.BinaryDirectoryPath, binary)
}

valueFromEnvVar := os.Getenv(envKubebuilderPath)
if hasBinary(binary, valueFromEnvVar) {
return filepath.Join(valueFromEnvVar, binary)
}
return filepath.Join(assetPath, binary)

return filepath.Join(defaultKubebuilderPath, binary)
}

// ControlPlane is the re-exported ControlPlane type from the internal integration package
Expand Down Expand Up @@ -113,6 +117,10 @@ type Environment struct {
// values are merged.
CRDDirectoryPaths []string

// BinaryDirectoryPath is the path where the binaries required for the envtest are
// locate in the environment
BinaryDirectoryPath string

// 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.
Expand Down Expand Up @@ -217,14 +225,14 @@ func (te *Environment) Start() (*rest.Config, error) {
}

if os.Getenv(envKubeAPIServerBin) == "" {
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
te.ControlPlane.APIServer.Path = te.getBinAssetPath("kube-apiserver")
}
if os.Getenv(envEtcdBin) == "" {
te.ControlPlane.Etcd.Path = defaultAssetPath("etcd")
te.ControlPlane.Etcd.Path = te.getBinAssetPath("etcd")
}
if os.Getenv(envKubectlBin) == "" {
// we can't just set the path manually (it's behind a function), so set the environment variable instead
if err := os.Setenv(envKubectlBin, defaultAssetPath("kubectl")); err != nil {
if err := os.Setenv(envKubectlBin, te.getBinAssetPath("kubectl")); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -267,6 +275,14 @@ func (te *Environment) Start() (*rest.Config, error) {
return te.Config, err
}

// hasBinary will return true when the binary was found in the path
func hasBinary(bin, path string) bool {
if _, err := os.Stat(filepath.Join(path, bin)); os.IsNotExist(err) {
return false
}
return true
}

func (te *Environment) startControlPlane() error {
numTries, maxRetries := 0, 5
var err error
Expand Down

0 comments on commit 8450022

Please sign in to comment.