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 10, 2020
1 parent bd97e08 commit 1ca5a31
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package envtest

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -43,7 +44,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 @@ -58,18 +58,32 @@ const (
StartTimeout = 60
StopTimeout = 60

// rquired binaries to run env test
kubeApiserverBin = "kube-apiserver"
etcdBin = "etcd"
kubectlBin = "kubectl"

defaultKubebuilderControlPlaneStartTimeout = 20 * time.Second
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 required binaries
// if the KUBEBUILDER_ASSETS is not set and the Directory is not informed in the
// config as well. Then, a default fixed value will be returned
func (te *Environment) getBinAssetPath() string {
assetPath := defaultKubebuilderPath

valueFromEnvVar := os.Getenv(envKubebuilderPath)
if strings.TrimSpace(valueFromEnvVar) != "" {
assetPath = valueFromEnvVar
}

valueFromEnvConfig := te.BinaryDirectoryPath
if strings.TrimSpace(valueFromEnvConfig) != "" {
assetPath = valueFromEnvConfig
}
return filepath.Join(assetPath, binary)

return assetPath
}

// ControlPlane is the re-exported ControlPlane type from the internal integration package
Expand Down Expand Up @@ -113,6 +127,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,18 +235,24 @@ func (te *Environment) Start() (*rest.Config, error) {
}

if os.Getenv(envKubeAPIServerBin) == "" {
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
te.ControlPlane.APIServer.Path = filepath.Join(te.getBinAssetPath(), kubeApiserverBin)

}
if os.Getenv(envEtcdBin) == "" {
te.ControlPlane.Etcd.Path = defaultAssetPath("etcd")
te.ControlPlane.Etcd.Path = filepath.Join(te.getBinAssetPath(), etcdBin)

}
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, filepath.Join(te.getBinAssetPath(), kubectlBin)); err != nil {
return nil, err
}
}

if err := te.validateRequiredBinaries(); err != nil {
return nil, err
}

if err := te.defaultTimeouts(); err != nil {
return nil, fmt.Errorf("failed to default controlplane timeouts: %w", err)
}
Expand Down Expand Up @@ -267,6 +291,22 @@ func (te *Environment) Start() (*rest.Config, error) {
return te.Config, err
}

// validateRequiredBinaries will return an error if the required binaries are not found
func (te *Environment) validateRequiredBinaries() error {
if _, err := os.Stat(te.ControlPlane.APIServer.Path); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("unable to find the %v", te.ControlPlane.APIServer.Path))
}

if _, err := os.Stat(te.ControlPlane.Etcd.Path); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("unable to find the %v", te.ControlPlane.Etcd.Path))
}

if _, err := os.Stat(os.Getenv(envKubectlBin)); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("unable to find the %v", os.Getenv(envKubectlBin)))
}
return nil
}

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

0 comments on commit 1ca5a31

Please sign in to comment.