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 Nov 23, 2020
1 parent 08cb9ee commit 21c6666
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@ 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 returns a path for binary from the following list of locations,
// ordered by precedence:
// 0. KUBEBUILDER_ASSETS
// 1. Environment.BinaryAssetsDirectory
// 2. The default path, "/usr/local/kubebuilder/bin"
func (te *Environment) getBinAssetPath(binary string) string {
valueFromEnvVar := os.Getenv(envKubebuilderPath)
if valueFromEnvVar != "" {
return filepath.Join(valueFromEnvVar, binary)
}
return filepath.Join(assetPath, binary)

if te.BinaryAssetsDirectory != "" {
return filepath.Join(te.BinaryAssetsDirectory, binary)
}

return filepath.Join(defaultKubebuilderPath, binary)
}

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

// BinaryAssetsDirectory is the path where the binaries required for the envtest are
// located in the local environment. This field can be overridden by setting KUBEBUILDER_ASSETS.
BinaryAssetsDirectory 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 +229,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

0 comments on commit 21c6666

Please sign in to comment.