Return rest config and regular config from pluginutil Init method

-add namespace overriding based on kubectl plugin env var
This commit is contained in:
Jonathan Berkhahn 2018-03-28 14:10:24 -07:00
parent 315679d1c5
commit b212714c99
2 changed files with 54 additions and 39 deletions

View File

@ -27,12 +27,13 @@ import (
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// InitConfig uses the KUBECONFIG environment variable to create a new config
// object based on the existing kubectl config and options passed from the
// calling plugin framework
func InitConfig() (*restclient.Config, error) {
// InitClientAndConfig uses the KUBECONFIG environment variable to create
// a new rest client and config object based on the existing kubectl config
// and options passed from the plugin framework via environment variables
func InitClientAndConfig() (*restclient.Config, clientcmd.ClientConfig, error) {
// resolve kubeconfig location, prioritizing the --config global flag,
// then the value of the KUBECONFIG env var (if any), and defaulting
// to ~/.kube/config as a last resort.
@ -59,30 +60,39 @@ func InitConfig() (*restclient.Config, error) {
}
if len(kubeconfig) == 0 {
return nil, fmt.Errorf("error initializing config. The KUBECONFIG environment variable must be defined.")
return nil, nil, fmt.Errorf("error initializing config. The KUBECONFIG environment variable must be defined.")
}
clientConfig, _, err := clientFromConfig(kubeconfig)
config, err := configFromPath(kubeconfig)
if err != nil {
return nil, fmt.Errorf("error obtaining kubectl config: %v", err)
return nil, nil, fmt.Errorf("error obtaining kubectl config: %v", err)
}
err = applyGlobalOptionsToConfig(clientConfig)
client, err := config.ClientConfig()
if err != nil {
return nil, fmt.Errorf("error processing global plugin options: %v", err)
return nil, nil, fmt.Errorf("the provided credentials %q could not be used: %v", kubeconfig, err)
}
return clientConfig, nil
err = applyGlobalOptionsToConfig(client)
if err != nil {
return nil, nil, fmt.Errorf("error processing global plugin options: %v", err)
}
return client, config, nil
}
func clientFromConfig(path string) (*restclient.Config, string, error) {
func configFromPath(path string) (clientcmd.ClientConfig, error) {
rules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: path}
credentials, err := rules.Load()
if err != nil {
return nil, "", fmt.Errorf("the provided credentials %q could not be loaded: %v", path, err)
return nil, fmt.Errorf("the provided credentials %q could not be loaded: %v", path, err)
}
overrides := &clientcmd.ConfigOverrides{
Context: clientcmdapi.Context{
Namespace: os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_NAMESPACE"),
},
}
overrides := &clientcmd.ConfigOverrides{}
var cfg clientcmd.ClientConfig
context := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT")
if len(context) > 0 {
@ -92,13 +102,7 @@ func clientFromConfig(path string) (*restclient.Config, string, error) {
cfg = clientcmd.NewDefaultClientConfig(*credentials, overrides)
}
config, err := cfg.ClientConfig()
if err != nil {
return nil, "", fmt.Errorf("the provided credentials %q could not be used: %v", path, err)
}
namespace, _, _ := cfg.Namespace()
return config, namespace, nil
return cfg, nil
}
func applyGlobalOptionsToConfig(config *restclient.Config) error {

View File

@ -10,20 +10,25 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("InitConfig", func() {
var _ = Describe("plugin client", func() {
BeforeEach(func() {
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_KUBECONFIG", "testdata/config")
})
Describe("InitConfig", func() {
Describe("InitClientAndConfig", func() {
Context("When nothing is overridden by the calling framework", func() {
It("finds and parses the preexisting config", func() {
config, err := pluginutils.InitConfig()
client, config, err := pluginutils.InitClientAndConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.Host).To(Equal("https://notreal.com:1234"))
Expect(config.Username).To(Equal("foo"))
Expect(config.Password).To(Equal("bar"))
Expect(client.Host).To(Equal("https://notreal.com:1234"))
Expect(client.Username).To(Equal("foo"))
Expect(client.Password).To(Equal("bar"))
namespace, overridden, err := config.Namespace()
Expect(err).NotTo(HaveOccurred())
Expect(namespace).To(Equal("default"))
Expect(overridden).To(BeFalse())
})
})
@ -43,25 +48,31 @@ var _ = Describe("InitConfig", func() {
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_PASSWORD", "elderberry")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT", "california")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_NAMESPACE", "catalog")
})
It("overrides the config settings with the passed in settings", func() {
config, err := pluginutils.InitConfig()
client, config, err := pluginutils.InitClientAndConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.Impersonate.UserName).To(Equal("apple"))
Expect(config.Impersonate.Groups).Should(ConsistOf("banana", "cherry"))
Expect(client.Impersonate.UserName).To(Equal("apple"))
Expect(client.Impersonate.Groups).Should(ConsistOf("banana", "cherry"))
Expect(config.CertFile).To(Equal("testdata/client.crt"))
Expect(config.KeyFile).To(Equal("testdata/client.key"))
Expect(config.CAFile).To(Equal("testdata/apiserver_ca.crt"))
Expect(client.CertFile).To(Equal("testdata/client.crt"))
Expect(client.KeyFile).To(Equal("testdata/client.key"))
Expect(client.CAFile).To(Equal("testdata/apiserver_ca.crt"))
Expect(config.Timeout).To(Equal(45 * time.Second))
Expect(config.ServerName).To(Equal("some-other-server.com"))
Expect(config.BearerToken).To(Equal("bearer notreal"))
Expect(client.Timeout).To(Equal(45 * time.Second))
Expect(client.ServerName).To(Equal("some-other-server.com"))
Expect(client.BearerToken).To(Equal("bearer notreal"))
Expect(config.Username).To(Equal("date"))
Expect(config.Password).To(Equal("elderberry"))
Expect(client.Username).To(Equal("date"))
Expect(client.Password).To(Equal("elderberry"))
Expect(config.Host).To(Equal("https://notrealincalifornia.com:1234"))
Expect(client.Host).To(Equal("https://notrealincalifornia.com:1234"))
namespace, overridden, err := config.Namespace()
Expect(err).NotTo(HaveOccurred())
Expect(namespace).To(Equal("catalog"))
Expect(overridden).To(BeTrue())
})
})
})