Skip to content

Commit 90c09c7

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#61287 from deads2k/client-03-clientconfig
Automatic merge from submit-queue (batch tested with PRs 61644, 61624, 61743, 61019, 61287). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. provide easy methods for direct kubeconfig loading from bytes Adds a `RESTConfigFromKubeConfig([]byte)` method for taking a kubeconfig and getting back the rest.Config. There are ways to do this now, but it takes a fair amount of wiring that is a pain. As kube starts dropping `--master` flags from its commands, it will be able to use this. For current consumers, this will be a big simplification. ```release-note NONE ```
2 parents af22cc8 + 8eec665 commit 90c09c7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

staging/src/k8s.io/client-go/tools/clientcmd/client_config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ func NewInteractiveClientConfig(config clientcmdapi.Config, contextName string,
100100
return &DirectClientConfig{config, contextName, overrides, fallbackReader, configAccess, promptedCredentials{}}
101101
}
102102

103+
// NewClientConfigFromBytes takes your kubeconfig and gives you back a ClientConfig
104+
func NewClientConfigFromBytes(configBytes []byte) (ClientConfig, error) {
105+
config, err := Load(configBytes)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
return &DirectClientConfig{*config, "", &ConfigOverrides{}, nil, nil, promptedCredentials{}}, nil
111+
}
112+
113+
// RESTConfigFromKubeConfig is a convenience method to give back a restconfig from your kubeconfig bytes.
114+
// For programmatic access, this is what you want 80% of the time
115+
func RESTConfigFromKubeConfig(configBytes []byte) (*restclient.Config, error) {
116+
clientConfig, err := NewClientConfigFromBytes(configBytes)
117+
if err != nil {
118+
return nil, err
119+
}
120+
return clientConfig.ClientConfig()
121+
}
122+
103123
func (config *DirectClientConfig) RawConfig() (clientcmdapi.Config, error) {
104124
return config.config, nil
105125
}

0 commit comments

Comments
 (0)