Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow passing the assets path bin via the env config #1214

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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