Skip to content

Commit 8450022

Browse files
✨ allow pass the assets path via the enviroment config
1 parent bd97e08 commit 8450022

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

pkg/envtest/server.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ It's possible to override some defaults, by setting the following environment va
4343
KUBEBUILDER_CONTROLPLANE_START_TIMEOUT (string supported by time.ParseDuration): timeout for test control plane to start. Defaults to 20s.
4444
KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT (string supported by time.ParseDuration): timeout for test control plane to start. Defaults to 20s.
4545
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
46-
4746
*/
4847
const (
4948
envUseExistingCluster = "USE_EXISTING_CLUSTER"
@@ -62,14 +61,19 @@ const (
6261
defaultKubebuilderControlPlaneStopTimeout = 20 * time.Second
6362
)
6463

65-
// Default binary path for test framework
66-
func defaultAssetPath(binary string) string {
67-
assetPath := os.Getenv(envKubebuilderPath)
68-
if assetPath == "" {
69-
assetPath = defaultKubebuilderPath
64+
// getBinAssetPath will return the path for the binary informed or an error if not be possible.
65+
// to find the bin.
66+
func (te *Environment) getBinAssetPath(binary string) string {
67+
if hasBinary(binary, te.BinaryDirectoryPath) {
68+
return filepath.Join(te.BinaryDirectoryPath, binary)
69+
}
70+
71+
valueFromEnvVar := os.Getenv(envKubebuilderPath)
72+
if hasBinary(binary, valueFromEnvVar) {
73+
return filepath.Join(valueFromEnvVar, binary)
7074
}
71-
return filepath.Join(assetPath, binary)
7275

76+
return filepath.Join(defaultKubebuilderPath, binary)
7377
}
7478

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

120+
// BinaryDirectoryPath is the path where the binaries required for the envtest are
121+
// locate in the environment
122+
BinaryDirectoryPath string
123+
116124
// UseExisting indicates that this environments should use an
117125
// existing kubeconfig, instead of trying to stand up a new control plane.
118126
// This is useful in cases that need aggregated API servers and the like.
@@ -217,14 +225,14 @@ func (te *Environment) Start() (*rest.Config, error) {
217225
}
218226

219227
if os.Getenv(envKubeAPIServerBin) == "" {
220-
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
228+
te.ControlPlane.APIServer.Path = te.getBinAssetPath("kube-apiserver")
221229
}
222230
if os.Getenv(envEtcdBin) == "" {
223-
te.ControlPlane.Etcd.Path = defaultAssetPath("etcd")
231+
te.ControlPlane.Etcd.Path = te.getBinAssetPath("etcd")
224232
}
225233
if os.Getenv(envKubectlBin) == "" {
226234
// 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 {
235+
if err := os.Setenv(envKubectlBin, te.getBinAssetPath("kubectl")); err != nil {
228236
return nil, err
229237
}
230238
}
@@ -267,6 +275,14 @@ func (te *Environment) Start() (*rest.Config, error) {
267275
return te.Config, err
268276
}
269277

278+
// hasBinary will return true when the binary was found in the path
279+
func hasBinary(bin, path string) bool {
280+
if _, err := os.Stat(filepath.Join(path, bin)); os.IsNotExist(err) {
281+
return false
282+
}
283+
return true
284+
}
285+
270286
func (te *Environment) startControlPlane() error {
271287
numTries, maxRetries := 0, 5
272288
var err error

0 commit comments

Comments
 (0)