@@ -43,7 +43,6 @@ It's possible to override some defaults, by setting the following environment va
43
43
KUBEBUILDER_CONTROLPLANE_START_TIMEOUT (string supported by time.ParseDuration): timeout for test control plane to start. Defaults to 20s.
44
44
KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT (string supported by time.ParseDuration): timeout for test control plane to start. Defaults to 20s.
45
45
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
-
47
46
*/
48
47
const (
49
48
envUseExistingCluster = "USE_EXISTING_CLUSTER"
@@ -58,18 +57,32 @@ const (
58
57
StartTimeout = 60
59
58
StopTimeout = 60
60
59
60
+ // rquired binaries to run env test
61
+ kubeApiserverBin = "kube-apiserver"
62
+ etcdBin = "etcd"
63
+ kubectlBin = "kubectl"
64
+
61
65
defaultKubebuilderControlPlaneStartTimeout = 20 * time .Second
62
66
defaultKubebuilderControlPlaneStopTimeout = 20 * time .Second
63
67
)
64
68
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
70
83
}
71
- return filepath .Join (assetPath , binary )
72
84
85
+ return assetPath
73
86
}
74
87
75
88
// ControlPlane is the re-exported ControlPlane type from the internal integration package
@@ -113,6 +126,10 @@ type Environment struct {
113
126
// values are merged.
114
127
CRDDirectoryPaths []string
115
128
129
+ // BinaryDirectoryPath is the path where the binaries required for the envtest are
130
+ // locate in the environment
131
+ BinaryDirectoryPath string
132
+
116
133
// UseExisting indicates that this environments should use an
117
134
// existing kubeconfig, instead of trying to stand up a new control plane.
118
135
// This is useful in cases that need aggregated API servers and the like.
@@ -217,18 +234,24 @@ func (te *Environment) Start() (*rest.Config, error) {
217
234
}
218
235
219
236
if os .Getenv (envKubeAPIServerBin ) == "" {
220
- te .ControlPlane .APIServer .Path = defaultAssetPath ("kube-apiserver" )
237
+ te .ControlPlane .APIServer .Path = filepath .Join (te .getBinAssetPath (), kubeApiserverBin )
238
+
221
239
}
222
240
if os .Getenv (envEtcdBin ) == "" {
223
- te .ControlPlane .Etcd .Path = defaultAssetPath ("etcd" )
241
+ te .ControlPlane .Etcd .Path = filepath .Join (te .getBinAssetPath (), etcdBin )
242
+
224
243
}
225
244
if os .Getenv (envKubectlBin ) == "" {
226
245
// 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 {
228
247
return nil , err
229
248
}
230
249
}
231
250
251
+ if err := te .validateRequiredBinaries (); err != nil {
252
+ return nil , err
253
+ }
254
+
232
255
if err := te .defaultTimeouts (); err != nil {
233
256
return nil , fmt .Errorf ("failed to default controlplane timeouts: %w" , err )
234
257
}
@@ -267,6 +290,25 @@ func (te *Environment) Start() (*rest.Config, error) {
267
290
return te .Config , err
268
291
}
269
292
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
+
270
312
func (te * Environment ) startControlPlane () error {
271
313
numTries , maxRetries := 0 , 5
272
314
var err error
0 commit comments