Use passed-in context in pluginutils config initializer

This commit is contained in:
Jonathan Berkhahn 2018-03-08 16:00:09 -08:00
parent ad5d307237
commit b8ef16af1e
3 changed files with 21 additions and 8 deletions

View File

@ -82,7 +82,16 @@ func clientFromConfig(path string) (*restclient.Config, string, error) {
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)
} }
cfg := clientcmd.NewDefaultClientConfig(*credentials, &clientcmd.ConfigOverrides{}) overrides := &clientcmd.ConfigOverrides{}
var cfg clientcmd.ClientConfig
context := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT")
if len(context) > 0 {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
cfg = clientcmd.NewNonInteractiveClientConfig(*credentials, context, overrides, rules)
} else {
cfg = clientcmd.NewDefaultClientConfig(*credentials, overrides)
}
config, err := cfg.ClientConfig() config, err := cfg.ClientConfig()
if err != nil { if err != nil {
return nil, "", fmt.Errorf("the provided credentials %q could not be used: %v", path, err) return nil, "", fmt.Errorf("the provided credentials %q could not be used: %v", path, err)
@ -132,11 +141,6 @@ func applyGlobalOptionsToConfig(config *restclient.Config) error {
// TODO(jvallejo): figure out how to override kubeconfig options // TODO(jvallejo): figure out how to override kubeconfig options
} }
context := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT")
if len(context) > 0 {
// TODO(jvallejo): figure out how to override kubeconfig options
}
user := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_USER") user := os.Getenv("KUBECTL_PLUGINS_GLOBAL_FLAG_USER")
if len(user) > 0 { if len(user) > 0 {
// TODO(jvallejo): figure out how to override kubeconfig options // TODO(jvallejo): figure out how to override kubeconfig options

View File

@ -42,12 +42,11 @@ var _ = Describe("InitConfig", func() {
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_USERNAME", "date") os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_USERNAME", "date")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_PASSWORD", "elderberry") os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_PASSWORD", "elderberry")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CLUSTER", "") os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CONTEXT", "california")
}) })
It("overrides the config settings with the passed in settings", func() { It("overrides the config settings with the passed in settings", func() {
config, err := pluginutils.InitConfig() config, err := pluginutils.InitConfig()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(config.Impersonate.UserName).To(Equal("apple")) Expect(config.Impersonate.UserName).To(Equal("apple"))
Expect(config.Impersonate.Groups).Should(ConsistOf("banana", "cherry")) Expect(config.Impersonate.Groups).Should(ConsistOf("banana", "cherry"))
@ -61,6 +60,8 @@ var _ = Describe("InitConfig", func() {
Expect(config.Username).To(Equal("date")) Expect(config.Username).To(Equal("date"))
Expect(config.Password).To(Equal("elderberry")) Expect(config.Password).To(Equal("elderberry"))
Expect(config.Host).To(Equal("https://notrealincalifornia.com:1234"))
}) })
}) })
}) })

View File

@ -4,11 +4,19 @@ clusters:
certificate-authority: certificate-authority:
server: https://notreal.com:1234 server: https://notreal.com:1234
name: local name: local
- cluster:
certificate-authority:
server: https://notrealincalifornia.com:1234
name: california
contexts: contexts:
- context: - context:
cluster: local cluster: local
user: myself user: myself
name: local name: local
- context:
cluster: california
user: myself
name: california
current-context: local current-context: local
kind: Config kind: Config
preferences: {} preferences: {}