Skip to content

Commit 3b86f6a

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

File tree

1 file changed

+68
-29
lines changed

1 file changed

+68
-29
lines changed

pkg/envtest/server.go

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,48 @@ 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 (
49-
envUseExistingCluster = "USE_EXISTING_CLUSTER"
50-
envKubeAPIServerBin = "TEST_ASSET_KUBE_APISERVER"
51-
envEtcdBin = "TEST_ASSET_ETCD"
52-
envKubectlBin = "TEST_ASSET_KUBECTL"
53-
envKubebuilderPath = "KUBEBUILDER_ASSETS"
54-
envStartTimeout = "KUBEBUILDER_CONTROLPLANE_START_TIMEOUT"
55-
envStopTimeout = "KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT"
56-
envAttachOutput = "KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT"
57-
defaultKubebuilderPath = "/usr/local/kubebuilder/bin"
58-
StartTimeout = 60
59-
StopTimeout = 60
60-
48+
envUseExistingClusterEnvVar = "USE_EXISTING_CLUSTER"
49+
envKubeAPIServerBinEnvVar = "TEST_ASSET_KUBE_APISERVER"
50+
envEtcdBinEnvVar = "TEST_ASSET_ETCD"
51+
envKubectlBinEnvVar = "TEST_ASSET_KUBECTL"
52+
envKubebuilderPathEnvVar = "KUBEBUILDER_ASSETS"
53+
envStartTimeoutEnvVar = "KUBEBUILDER_CONTROLPLANE_START_TIMEOUT"
54+
envStopTimeoutEnvVar = "KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT"
55+
envAttachOutputEnvVar = "KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT"
56+
57+
// Default values
58+
defaultKubebuilderPath = "/usr/local/kubebuilder/bin"
59+
StartTimeout = 60
60+
StopTimeout = 60
6161
defaultKubebuilderControlPlaneStartTimeout = 20 * time.Second
6262
defaultKubebuilderControlPlaneStopTimeout = 20 * time.Second
63+
64+
// required binaries to run env test
65+
kubeApiserverBinName = "kube-apiserver"
66+
etcdBinName = "etcd"
67+
kubectlBinName = "kubectl"
6368
)
6469

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

82+
if hasBinary(binary, defaultKubebuilderPath) {
83+
return filepath.Join(defaultKubebuilderPath, binary), nil
84+
}
85+
86+
return "", fmt.Errorf("unable to found the required binary: %v",
87+
filepath.Join(defaultKubebuilderPath, binary))
7388
}
7489

7590
// ControlPlane is the re-exported ControlPlane type from the internal integration package
@@ -113,6 +128,10 @@ type Environment struct {
113128
// values are merged.
114129
CRDDirectoryPaths []string
115130

131+
// BinaryDirectoryPath is the path where the binaries required for the envtest are
132+
// locate in the environment
133+
BinaryDirectoryPath string
134+
116135
// UseExisting indicates that this environments should use an
117136
// existing kubeconfig, instead of trying to stand up a new control plane.
118137
// This is useful in cases that need aggregated API servers and the like.
@@ -200,7 +219,7 @@ func (te *Environment) Start() (*rest.Config, error) {
200219
te.ControlPlane.Etcd = &integration.Etcd{}
201220
}
202221

203-
if os.Getenv(envAttachOutput) == "true" {
222+
if os.Getenv(envAttachOutputEnvVar) == "true" {
204223
te.AttachControlPlaneOutput = true
205224
}
206225
if te.ControlPlane.APIServer.Out == nil && te.AttachControlPlaneOutput {
@@ -216,15 +235,27 @@ func (te *Environment) Start() (*rest.Config, error) {
216235
te.ControlPlane.Etcd.Err = os.Stderr
217236
}
218237

219-
if os.Getenv(envKubeAPIServerBin) == "" {
220-
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
238+
// set binaries location and then, if the bin not found throw issue
239+
var err error
240+
if os.Getenv(envKubeAPIServerBinEnvVar) == "" {
241+
te.ControlPlane.APIServer.Path, err = te.getBinAssetPath(kubeApiserverBinName)
242+
if err != nil {
243+
return nil, err
244+
}
221245
}
222-
if os.Getenv(envEtcdBin) == "" {
223-
te.ControlPlane.Etcd.Path = defaultAssetPath("etcd")
246+
if os.Getenv(envEtcdBinEnvVar) == "" {
247+
te.ControlPlane.Etcd.Path, err = te.getBinAssetPath(etcdBinName)
248+
if err != nil {
249+
return nil, err
250+
}
224251
}
225-
if os.Getenv(envKubectlBin) == "" {
252+
if os.Getenv(envKubectlBinEnvVar) == "" {
253+
kubectBinPath, err := te.getBinAssetPath(kubectlBinName)
254+
if err != nil {
255+
return nil, err
256+
}
226257
// 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 {
258+
if err := os.Setenv(envKubectlBinEnvVar, kubectBinPath); err != nil {
228259
return nil, err
229260
}
230261
}
@@ -267,6 +298,14 @@ func (te *Environment) Start() (*rest.Config, error) {
267298
return te.Config, err
268299
}
269300

301+
// hasBinary will return true when the binary was found in the path
302+
func hasBinary(bin, path string) bool {
303+
if _, err := os.Stat(filepath.Join(path, bin)); os.IsNotExist(err) {
304+
return false
305+
}
306+
return true
307+
}
308+
270309
func (te *Environment) startControlPlane() error {
271310
numTries, maxRetries := 0, 5
272311
var err error
@@ -287,7 +326,7 @@ func (te *Environment) startControlPlane() error {
287326
func (te *Environment) defaultTimeouts() error {
288327
var err error
289328
if te.ControlPlaneStartTimeout == 0 {
290-
if envVal := os.Getenv(envStartTimeout); envVal != "" {
329+
if envVal := os.Getenv(envStartTimeoutEnvVar); envVal != "" {
291330
te.ControlPlaneStartTimeout, err = time.ParseDuration(envVal)
292331
if err != nil {
293332
return err
@@ -298,7 +337,7 @@ func (te *Environment) defaultTimeouts() error {
298337
}
299338

300339
if te.ControlPlaneStopTimeout == 0 {
301-
if envVal := os.Getenv(envStopTimeout); envVal != "" {
340+
if envVal := os.Getenv(envStopTimeoutEnvVar); envVal != "" {
302341
te.ControlPlaneStopTimeout, err = time.ParseDuration(envVal)
303342
if err != nil {
304343
return err
@@ -312,7 +351,7 @@ func (te *Environment) defaultTimeouts() error {
312351

313352
func (te *Environment) useExistingCluster() bool {
314353
if te.UseExistingCluster == nil {
315-
return strings.ToLower(os.Getenv(envUseExistingCluster)) == "true"
354+
return strings.ToLower(os.Getenv(envUseExistingClusterEnvVar)) == "true"
316355
}
317356
return *te.UseExistingCluster
318357
}

0 commit comments

Comments
 (0)