Skip to content

Commit f1741e3

Browse files
authored
Merge pull request #2278 from kaovilai/envtest-kubeconfig
✨ envtest: Add Environment.KubeConfig field
2 parents 53c0518 + 11240b7 commit f1741e3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/envtest/envtest_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/runtime"
2929
"k8s.io/apimachinery/pkg/types"
3030
"k8s.io/client-go/kubernetes/scheme"
31-
31+
"k8s.io/client-go/tools/clientcmd"
3232
"sigs.k8s.io/controller-runtime/pkg/client"
3333
)
3434

@@ -682,6 +682,14 @@ var _ = Describe("Test", func() {
682682
})
683683
})
684684

685+
It("should set a working KubeConfig", func() {
686+
kubeconfigRESTConfig, err := clientcmd.RESTConfigFromKubeConfig(env.KubeConfig)
687+
Expect(err).ToNot(HaveOccurred())
688+
kubeconfigClient, err := client.New(kubeconfigRESTConfig, client.Options{Scheme: s})
689+
Expect(err).NotTo(HaveOccurred())
690+
Expect(kubeconfigClient.List(context.Background(), &apiextensionsv1.CustomResourceDefinitionList{})).To(Succeed())
691+
})
692+
685693
It("should update CRDs if already present in the cluster", func() {
686694

687695
// Install only the CRDv1 multi-version example

pkg/envtest/server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ type Environment struct {
126126
// loading.
127127
Config *rest.Config
128128

129+
// KubeConfig provides []byte of a kubeconfig file to talk to the apiserver
130+
// It's automatically populated if not set based on the `Config`
131+
KubeConfig []byte
132+
129133
// CRDInstallOptions are the options for installing CRDs.
130134
CRDInstallOptions CRDInstallOptions
131135

@@ -291,6 +295,14 @@ func (te *Environment) Start() (*rest.Config, error) {
291295
te.Config = adminUser.Config()
292296
}
293297

298+
if len(te.KubeConfig) == 0 {
299+
var err error
300+
te.KubeConfig, err = controlplane.KubeConfigFromREST(te.Config)
301+
if err != nil {
302+
return nil, fmt.Errorf("unable to set KubeConfig field: %w", err)
303+
}
304+
}
305+
294306
// Set the default scheme if nil.
295307
if te.Scheme == nil {
296308
te.Scheme = scheme.Scheme

0 commit comments

Comments
 (0)