Skip to content

✨ Add support for multi-value KUBECONFIG #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
37 changes: 20 additions & 17 deletions pkg/client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -95,35 +93,40 @@ func GetConfigWithContext(context string) (*rest.Config, error) {
return cfg, nil
}

// loadInClusterConfig is a function used to load the in-cluster
// Kubernetes client config. This variable makes is possible to
// test the precedence of loading the config.
var loadInClusterConfig = rest.InClusterConfig

// loadConfig loads a REST Config as per the rules specified in GetConfig
func loadConfig(context string) (*rest.Config, error) {

// If a flag is specified with the config location, use that
if len(kubeconfig) > 0 {
return loadConfigWithContext(apiServerURL, kubeconfig, context)
}
// If an env variable is specified with the config location, use that
if len(os.Getenv("KUBECONFIG")) > 0 {
return loadConfigWithContext(apiServerURL, os.Getenv("KUBECONFIG"), context)
}
// If no explicit location, try the in-cluster config
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
return loadConfigWithContext(apiServerURL, &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}, context)
}
// If no in-cluster config, try the default location in the user's home directory
if usr, err := user.Current(); err == nil {
if c, err := loadConfigWithContext(apiServerURL, filepath.Join(usr.HomeDir, ".kube", "config"),
context); err == nil {

// If the recommended kubeconfig env variable is not specified,
// try the in-cluster config.
kubeconfigPath := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(kubeconfigPath) == 0 {
if c, err := loadInClusterConfig(); err == nil {
return c, nil
}
}

// If the recommended kubeconfig env variable is set, or there
// is no in-cluster config, try the default recommended locations.
if c, err := loadConfigWithContext(apiServerURL, clientcmd.NewDefaultClientConfigLoadingRules(), context); err == nil {
return c, nil
}

return nil, fmt.Errorf("could not locate a kubeconfig")
}

func loadConfigWithContext(apiServerURL, kubeconfig, context string) (*rest.Config, error) {
func loadConfigWithContext(apiServerURL string, loader clientcmd.ClientConfigLoader, context string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
loader,
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: apiServerURL,
Expand Down
39 changes: 39 additions & 0 deletions pkg/client/config/config_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t, "Client Config Test Suite", []Reporter{printer.NewlineReporter{}})
}

var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))

close(done)
}, 60)
243 changes: 243 additions & 0 deletions pkg/client/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

type testCase struct {
text string
apiServerURL string
context string
kubeconfigFlag string
kubeconfigEnv []string
wantHost string
}

var _ = Describe("Config", func() {

var dir string

origRecommendedHomeFile := clientcmd.RecommendedHomeFile

BeforeEach(func() {
// create temporary directory for test case
var err error
dir, err = ioutil.TempDir("", "cr-test")
Expect(err).NotTo(HaveOccurred())

// override $HOME/.kube/config
clientcmd.RecommendedHomeFile = filepath.Join(dir, ".kubeconfig")
})

AfterEach(func() {
os.Unsetenv(clientcmd.RecommendedConfigPathEnvVar)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a note about where this is set

kubeconfig = ""
apiServerURL = ""
clientcmd.RecommendedHomeFile = origRecommendedHomeFile

err := os.RemoveAll(dir)
Expect(err).NotTo(HaveOccurred())
})

Describe("GetConfigWithContext", func() {
defineTests := func(testCases []testCase) {
for _, testCase := range testCases {
tc := testCase
It(tc.text, func() {
// set global and environment configs
setConfigs(tc, dir)

// run the test
cfg, err := GetConfigWithContext(tc.context)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Host).To(Equal(tc.wantHost))
})
}
}

Context("when kubeconfig files don't exist", func() {
It("should fail", func() {
cfg, err := GetConfigWithContext("")
Expect(cfg).To(BeNil())
Expect(err).To(HaveOccurred())
})
})

Context("when in-cluster", func() {
kubeconfigFiles := map[string]string{
"kubeconfig-multi-context": genKubeconfig("from-multi-env-1", "from-multi-env-2"),
".kubeconfig": genKubeconfig("from-home"),
}
BeforeEach(func() {
err := createFiles(kubeconfigFiles, dir)
Expect(err).NotTo(HaveOccurred())

// override in-cluster config loader
loadInClusterConfig = func() (*rest.Config, error) {
return &rest.Config{Host: "from-in-cluster"}, nil
}
})
AfterEach(func() { loadInClusterConfig = rest.InClusterConfig })

testCases := []testCase{
{
text: "should prefer the envvar over the in-cluster config",
kubeconfigEnv: []string{"kubeconfig-multi-context"},
wantHost: "from-multi-env-1",
},
{
text: "should prefer in-cluster over the recommended home file",
wantHost: "from-in-cluster",
},
}
defineTests(testCases)
})

Context("when outside the cluster", func() {
kubeconfigFiles := map[string]string{
"kubeconfig-flag": genKubeconfig("from-flag"),
"kubeconfig-multi-context": genKubeconfig("from-multi-env-1", "from-multi-env-2"),
"kubeconfig-env-1": genKubeconfig("from-env-1"),
"kubeconfig-env-2": genKubeconfig("from-env-2"),
".kubeconfig": genKubeconfig("from-home"),
}
BeforeEach(func() {
err := createFiles(kubeconfigFiles, dir)
Expect(err).NotTo(HaveOccurred())
})
testCases := []testCase{
{
text: "should use the --kubeconfig flag",
kubeconfigFlag: "kubeconfig-flag",
wantHost: "from-flag",
},
{
text: "should use the envvar",
kubeconfigEnv: []string{"kubeconfig-multi-context"},
wantHost: "from-multi-env-1",
},
{
text: "should use the recommended home file",
wantHost: "from-home",
},
{
text: "should prefer the flag over the envvar",
kubeconfigFlag: "kubeconfig-flag",
kubeconfigEnv: []string{"kubeconfig-multi-context"},
wantHost: "from-flag",
},
{
text: "should prefer the envvar over the recommended home file",
kubeconfigEnv: []string{"kubeconfig-multi-context"},
wantHost: "from-multi-env-1",
},
{
text: "should allow overriding the API server URL",
apiServerURL: "override",
kubeconfigEnv: []string{"kubeconfig-multi-context"},
wantHost: "override",
},
{
text: "should allow overriding the context",
context: "from-multi-env-2",
kubeconfigEnv: []string{"kubeconfig-multi-context"},
wantHost: "from-multi-env-2",
},
{
text: "should support a multi-value envvar",
context: "from-env-2",
kubeconfigEnv: []string{"kubeconfig-env-1", "kubeconfig-env-2"},
wantHost: "from-env-2",
},
}
defineTests(testCases)
})
})
})

func setConfigs(tc testCase, dir string) {
// Set API Server URL
apiServerURL = tc.apiServerURL

// Set kubeconfig flag value
if len(tc.kubeconfigFlag) > 0 {
kubeconfig = filepath.Join(dir, tc.kubeconfigFlag)
}

// Set KUBECONFIG env value
if len(tc.kubeconfigEnv) > 0 {
kubeconfigEnvPaths := []string{}
for _, k := range tc.kubeconfigEnv {
kubeconfigEnvPaths = append(kubeconfigEnvPaths, filepath.Join(dir, k))
}
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, strings.Join(kubeconfigEnvPaths, ":"))
}
}

func createFiles(files map[string]string, dir string) error {
for path, data := range files {
if err := ioutil.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil {
return err
}
}
return nil
}

func genKubeconfig(contexts ...string) string {
var sb strings.Builder
sb.WriteString(`---
apiVersion: v1
kind: Config
clusters:
`)
for _, ctx := range contexts {
sb.WriteString(`- cluster:
server: ` + ctx + `
name: ` + ctx + `
`)
}
sb.WriteString("contexts:\n")
for _, ctx := range contexts {
sb.WriteString(`- context:
cluster: ` + ctx + `
user: ` + ctx + `
name: ` + ctx + `
`)
}

sb.WriteString("users:\n")
for _, ctx := range contexts {
sb.WriteString(`- name: ` + ctx + `
`)
}
sb.WriteString("preferences: {}\n")
if len(contexts) > 0 {
sb.WriteString("current-context: " + contexts[0] + "\n")
}

return sb.String()
}