Skip to content

Commit c5e5439

Browse files
committed
Make it easier to set test assets.
Add a single environment variable that can be set to change the default search path for kubebuilder test assets, `KUBEBUILDER_ASSETS`. This prevents us from having to specify all three `TEST_ASSET_<thing>` environment variables manually if kubebuilder isn't installed in /usr/local/kubebuilder.
1 parent f5bd07e commit c5e5439

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

pkg/envtest/server.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package envtest
1818

1919
import (
2020
"os"
21+
"path/filepath"
2122

2223
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2324
"k8s.io/client-go/rest"
@@ -26,14 +27,24 @@ import (
2627

2728
// Default binary path for test framework
2829
const (
29-
envKubeAPIServerBin = "TEST_ASSET_KUBE_APISERVER"
30-
envEtcdBin = "TEST_ASSET_ETCD"
31-
defaultKubeAPIServerBin = "/usr/local/kubebuilder/bin/kube-apiserver"
32-
defaultEtcdBin = "/usr/local/kubebuilder/bin/etcd"
33-
StartTimeout = 60
34-
StopTimeout = 60
30+
envKubeAPIServerBin = "TEST_ASSET_KUBE_APISERVER"
31+
envEtcdBin = "TEST_ASSET_ETCD"
32+
envKubectlBin = "TEST_ASSET_KUBECTL"
33+
envKubebuilderPath = "KUBEBUILDER_ASSETS"
34+
defaultKubebuilderPath = "/usr/local/kubebuilder/bin"
35+
StartTimeout = 60
36+
StopTimeout = 60
3537
)
3638

39+
func defaultAssetPath(binary string) string {
40+
assetPath := os.Getenv(envKubebuilderPath)
41+
if assetPath == "" {
42+
assetPath = defaultKubebuilderPath
43+
}
44+
return filepath.Join(assetPath, binary)
45+
46+
}
47+
3748
// APIServerDefaultArgs are flags necessary to bring up apiserver.
3849
// TODO: create test framework interface to append flag to default flags.
3950
var defaultKubeAPIServerFlags = []string{
@@ -71,10 +82,16 @@ func (te *Environment) Start() (*rest.Config, error) {
7182
te.ControlPlane = integration.ControlPlane{}
7283
te.ControlPlane.APIServer = &integration.APIServer{Args: defaultKubeAPIServerFlags}
7384
if os.Getenv(envKubeAPIServerBin) == "" {
74-
te.ControlPlane.APIServer.Path = defaultKubeAPIServerBin
85+
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
7586
}
7687
if os.Getenv(envEtcdBin) == "" {
77-
te.ControlPlane.Etcd = &integration.Etcd{Path: defaultEtcdBin}
88+
te.ControlPlane.Etcd = &integration.Etcd{Path: defaultAssetPath("etcd")}
89+
}
90+
if os.Getenv(envKubectlBin) == "" {
91+
// we can't just set the path manually (it's behind a function), so set the environment variable instead
92+
if err := os.Setenv(envKubectlBin, defaultAssetPath("kubectl")); err != nil {
93+
return nil, err
94+
}
7895
}
7996

8097
// Start the control plane - retry if it fails

test.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ function setup_envs {
9090
header_text "setting up env vars"
9191

9292
# Setup env vars
93-
export TEST_ASSET_KUBECTL=$kb_root_dir/bin/kubectl
94-
export TEST_ASSET_KUBE_APISERVER=$kb_root_dir/bin/kube-apiserver
95-
export TEST_ASSET_ETCD=$kb_root_dir/bin/etcd
93+
export KUBEBUILDER_ASSETS=$kb_root_dir/bin
9694
}
9795

9896
header_text "using tools"

0 commit comments

Comments
 (0)