Skip to content

Commit aa49d4f

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

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

pkg/envtest/server.go

Lines changed: 52 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"
@@ -58,18 +57,32 @@ const (
5857
StartTimeout = 60
5958
StopTimeout = 60
6059

60+
// rquired binaries to run env test
61+
kubeApiserverBin = "kube-apiserver"
62+
etcdBin = "etcd"
63+
kubectlBin = "kubectl"
64+
6165
defaultKubebuilderControlPlaneStartTimeout = 20 * time.Second
6266
defaultKubebuilderControlPlaneStopTimeout = 20 * time.Second
6367
)
6468

65-
// Default binary path for test framework
66-
func defaultAssetPath(binary string) string {
67-
assetPath := os.Getenv(envKubebuilderPath)
68-
if assetPath == "" {
69-
assetPath = defaultKubebuilderPath
69+
// getBinAssetPath will return the path for the required binaries
70+
// if the KUBEBUILDER_ASSETS is not set and the Directory is not informed in the
71+
// config as well. Then, a default fixed value will be returned
72+
func (te *Environment) getBinAssetPath() string {
73+
assetPath := defaultKubebuilderPath
74+
75+
valueFromEnvVar := os.Getenv(envKubebuilderPath)
76+
if strings.TrimSpace(valueFromEnvVar) != "" {
77+
assetPath = valueFromEnvVar
78+
}
79+
80+
valueFromEnvConfig := te.BinaryDirectoryPath
81+
if strings.TrimSpace(valueFromEnvConfig) != "" {
82+
assetPath = valueFromEnvConfig
7083
}
71-
return filepath.Join(assetPath, binary)
7284

85+
return assetPath
7386
}
7487

7588
// ControlPlane is the re-exported ControlPlane type from the internal integration package
@@ -113,6 +126,10 @@ type Environment struct {
113126
// values are merged.
114127
CRDDirectoryPaths []string
115128

129+
// BinaryDirectoryPath is the path where the binaries required for the envtest are
130+
// locate in the environment
131+
BinaryDirectoryPath string
132+
116133
// UseExisting indicates that this environments should use an
117134
// existing kubeconfig, instead of trying to stand up a new control plane.
118135
// This is useful in cases that need aggregated API servers and the like.
@@ -217,18 +234,24 @@ func (te *Environment) Start() (*rest.Config, error) {
217234
}
218235

219236
if os.Getenv(envKubeAPIServerBin) == "" {
220-
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
237+
te.ControlPlane.APIServer.Path = filepath.Join(te.getBinAssetPath(), kubeApiserverBin)
238+
221239
}
222240
if os.Getenv(envEtcdBin) == "" {
223-
te.ControlPlane.Etcd.Path = defaultAssetPath("etcd")
241+
te.ControlPlane.Etcd.Path = filepath.Join(te.getBinAssetPath(), etcdBin)
242+
224243
}
225244
if os.Getenv(envKubectlBin) == "" {
226245
// 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 {
246+
if err := os.Setenv(envKubectlBin, filepath.Join(te.getBinAssetPath(), kubectlBin)); err != nil {
228247
return nil, err
229248
}
230249
}
231250

251+
if err := te.validateRequiredBinaries(); err != nil {
252+
return nil, err
253+
}
254+
232255
if err := te.defaultTimeouts(); err != nil {
233256
return nil, fmt.Errorf("failed to default controlplane timeouts: %w", err)
234257
}
@@ -267,6 +290,25 @@ func (te *Environment) Start() (*rest.Config, error) {
267290
return te.Config, err
268291
}
269292

293+
// validateRequiredBinaries will return an error if the required binaries are not found
294+
func (te *Environment) validateRequiredBinaries() error {
295+
if _, err := os.Stat(te.ControlPlane.APIServer.Path); os.IsNotExist(err) {
296+
return fmt.Errorf("unable to find %v binary in the path %v "+
297+
"which is required to run the tests", kubeApiserverBin, te.ControlPlane.APIServer.Path)
298+
}
299+
300+
if _, err := os.Stat(te.ControlPlane.Etcd.Path); os.IsNotExist(err) {
301+
return fmt.Errorf("unable to find %v binary in the path %v "+
302+
"which is required to run the tests", etcdBin, te.ControlPlane.Etcd.Path)
303+
}
304+
305+
if _, err := os.Stat(os.Getenv(envKubectlBin)); os.IsNotExist(err) {
306+
return fmt.Errorf("unable to find %v binary in the path %v "+
307+
"which is required to run the tests", kubectlBin, os.Getenv(envKubectlBin))
308+
}
309+
return nil
310+
}
311+
270312
func (te *Environment) startControlPlane() error {
271313
numTries, maxRetries := 0, 5
272314
var err error

0 commit comments

Comments
 (0)