Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/envtest/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"

"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -682,6 +682,14 @@ var _ = Describe("Test", func() {
})
})

It("should set a working KubeConfig", func() {
kubeconfigRESTConfig, err := clientcmd.RESTConfigFromKubeConfig(env.KubeConfig)
Expect(err).ToNot(HaveOccurred())
kubeconfigClient, err := client.New(kubeconfigRESTConfig, client.Options{Scheme: s})
Expect(err).NotTo(HaveOccurred())
Expect(kubeconfigClient.List(context.Background(), &apiextensionsv1.CustomResourceDefinitionList{})).To(Succeed())
})

It("should update CRDs if already present in the cluster", func() {

// Install only the CRDv1 multi-version example
Expand Down
12 changes: 12 additions & 0 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ type Environment struct {
// loading.
Config *rest.Config

// KubeConfig provides []byte of a kubeconfig file to talk to the apiserver
// It's automatically populated if not set based on the `Config`
KubeConfig []byte

// CRDInstallOptions are the options for installing CRDs.
CRDInstallOptions CRDInstallOptions

Expand Down Expand Up @@ -291,6 +295,14 @@ func (te *Environment) Start() (*rest.Config, error) {
te.Config = adminUser.Config()
}

if len(te.KubeConfig) == 0 {
var err error
te.KubeConfig, err = controlplane.KubeConfigFromREST(te.Config)
if err != nil {
return nil, fmt.Errorf("unable to set KubeConfig field: %w", err)
}
}

// Set the default scheme if nil.
if te.Scheme == nil {
te.Scheme = scheme.Scheme
Expand Down
Loading