Skip to content

Commit 21c6666

Browse files
✨ allow pass the assets path via the enviroment config
1 parent 08cb9ee commit 21c6666

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

pkg/envtest/server.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,22 @@ const (
6262
defaultKubebuilderControlPlaneStopTimeout = 20 * time.Second
6363
)
6464

65-
// Default binary path for test framework
66-
func defaultAssetPath(binary string) string {
67-
assetPath := os.Getenv(envKubebuilderPath)
68-
if assetPath == "" {
69-
assetPath = defaultKubebuilderPath
65+
// getBinAssetPath returns a path for binary from the following list of locations,
66+
// ordered by precedence:
67+
// 0. KUBEBUILDER_ASSETS
68+
// 1. Environment.BinaryAssetsDirectory
69+
// 2. The default path, "/usr/local/kubebuilder/bin"
70+
func (te *Environment) getBinAssetPath(binary string) string {
71+
valueFromEnvVar := os.Getenv(envKubebuilderPath)
72+
if valueFromEnvVar != "" {
73+
return filepath.Join(valueFromEnvVar, binary)
7074
}
71-
return filepath.Join(assetPath, binary)
7275

76+
if te.BinaryAssetsDirectory != "" {
77+
return filepath.Join(te.BinaryAssetsDirectory, binary)
78+
}
79+
80+
return filepath.Join(defaultKubebuilderPath, binary)
7381
}
7482

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

124+
// BinaryAssetsDirectory is the path where the binaries required for the envtest are
125+
// located in the local environment. This field can be overridden by setting KUBEBUILDER_ASSETS.
126+
BinaryAssetsDirectory string
127+
116128
// UseExisting indicates that this environments should use an
117129
// existing kubeconfig, instead of trying to stand up a new control plane.
118130
// This is useful in cases that need aggregated API servers and the like.
@@ -217,14 +229,14 @@ func (te *Environment) Start() (*rest.Config, error) {
217229
}
218230

219231
if os.Getenv(envKubeAPIServerBin) == "" {
220-
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
232+
te.ControlPlane.APIServer.Path = te.getBinAssetPath("kube-apiserver")
221233
}
222234
if os.Getenv(envEtcdBin) == "" {
223-
te.ControlPlane.Etcd.Path = defaultAssetPath("etcd")
235+
te.ControlPlane.Etcd.Path = te.getBinAssetPath("etcd")
224236
}
225237
if os.Getenv(envKubectlBin) == "" {
226238
// we can't just set the path manually (it's behind a function), so set the environment variable instead
227-
if err := os.Setenv(envKubectlBin, defaultAssetPath("kubectl")); err != nil {
239+
if err := os.Setenv(envKubectlBin, te.getBinAssetPath("kubectl")); err != nil {
228240
return nil, err
229241
}
230242
}

0 commit comments

Comments
 (0)