@@ -19,7 +19,7 @@ package main
19
19
import (
20
20
"fmt"
21
21
"os"
22
- "path"
22
+ "path/filepath "
23
23
"strings"
24
24
"testing"
25
25
@@ -29,34 +29,87 @@ import (
29
29
"sigs.k8s.io/gateway-api/apis/v1beta1"
30
30
apisxv1alpha1 "sigs.k8s.io/gateway-api/apisx/v1alpha1"
31
31
32
+ corev1 "k8s.io/api/core/v1"
33
+ "k8s.io/apimachinery/pkg/runtime"
34
+ "k8s.io/client-go/rest"
32
35
"k8s.io/client-go/tools/clientcmd"
33
36
"sigs.k8s.io/controller-runtime/pkg/client"
37
+ "sigs.k8s.io/controller-runtime/pkg/envtest"
34
38
)
35
39
36
- var k8sClient client.Client
40
+ var (
41
+ k8sClient client.Client
42
+ )
37
43
38
44
func TestMain (m * testing.M ) {
45
+
46
+ scheme := runtime .NewScheme ()
47
+ var restConfig * rest.Config
48
+ var testEnv * envtest.Environment
49
+ var err error
50
+
51
+ v1alpha3 .Install (scheme )
52
+ v1alpha2 .Install (scheme )
53
+ v1beta1 .Install (scheme )
54
+ v1 .Install (scheme )
55
+ apisxv1alpha1 .Install (scheme )
56
+
57
+ // Add core APIs in case we refer secrets, services and configmaps
58
+ corev1 .AddToScheme (scheme )
59
+
60
+ // If one wants to use a local cluster, a KUBECONFIG envvar should be passed,
61
+ // otherwise testenv will be used
39
62
kubeconfig := os .Getenv ("KUBECONFIG" )
40
- if kubeconfig == "" {
41
- kubeconfig = path .Join (os .Getenv ("HOME" ), ".kube/config" )
63
+ if kubeconfig != "" {
64
+ restConfig , err = clientcmd .BuildConfigFromFlags ("" , kubeconfig )
65
+ if err != nil {
66
+ panic (fmt .Sprintf ("Failed to get restConfig from BuildConfigFromFlags: %v" , err ))
67
+ }
68
+ } else {
69
+ // The version used here MUST reflect the available versions at
70
+ // controller-runtime repo: https://raw.githubusercontent.com/kubernetes-sigs/controller-tools/HEAD/envtest-releases.yaml
71
+ // If the envvar is not passed, the latest GA will be used
72
+ k8sVersion := os .Getenv ("K8S_VERSION" )
73
+
74
+ crdChannel := "standard"
75
+ if requestedCRDChannel , ok := os .LookupEnv ("CRD_CHANNEL" ); ok {
76
+ crdChannel = requestedCRDChannel
77
+ }
78
+
79
+ testEnv = & envtest.Environment {
80
+ Scheme : scheme ,
81
+ ErrorIfCRDPathMissing : true ,
82
+ DownloadBinaryAssets : true ,
83
+ DownloadBinaryAssetsVersion : k8sVersion ,
84
+ CRDInstallOptions : envtest.CRDInstallOptions {
85
+ Paths : []string {
86
+ filepath .Join (".." , ".." , ".." , "config" , "crd" , crdChannel ),
87
+ },
88
+ CleanUpAfterUse : true ,
89
+ },
90
+ }
91
+
92
+ restConfig , err = testEnv .Start ()
93
+ if err != nil {
94
+ panic (fmt .Sprintf ("Error initializing test environment: %v" , err ))
95
+ }
42
96
}
43
97
44
- restConfig , err := clientcmd .BuildConfigFromFlags ("" , kubeconfig )
98
+ k8sClient , err = client .New (restConfig , client.Options {
99
+ Scheme : scheme ,
100
+ })
45
101
if err != nil {
46
- panic (fmt .Sprintf ("Failed to get restConfig from BuildConfigFromFlags : %v" , err ))
102
+ panic (fmt .Sprintf ("Error initializing Kubernetes client : %v" , err ))
47
103
}
48
104
49
- k8sClient , err = client .New (restConfig , client.Options {})
50
- if err != nil {
51
- panic (fmt .Sprintf ("Error initializing Kubernetes client: %v" , err ))
105
+ rc := m .Run ()
106
+ if testEnv != nil {
107
+ if err := testEnv .Stop (); err != nil {
108
+ panic (fmt .Sprintf ("error stopping test environment: %v" , err ))
109
+ }
52
110
}
53
- v1alpha3 .Install (k8sClient .Scheme ())
54
- v1alpha2 .Install (k8sClient .Scheme ())
55
- v1beta1 .Install (k8sClient .Scheme ())
56
- v1 .Install (k8sClient .Scheme ())
57
- apisxv1alpha1 .Install (k8sClient .Scheme ())
58
111
59
- os .Exit (m . Run () )
112
+ os .Exit (rc )
60
113
}
61
114
62
115
func ptrTo [T any ](a T ) * T {
0 commit comments