-
Notifications
You must be signed in to change notification settings - Fork 3
/
kube.go
52 lines (49 loc) · 1.75 KB
/
kube.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Package kube contains Kubernetes utilities.
package kube
import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// ConvertRestConfigToRawConfig converts a rest.Config to a clientcmdapi.Config.
func ConvertRestConfigToRawConfig(config *rest.Config) (*clientcmdapi.Config, error) {
raw, err := convertRestConfigToClientConfig(config).RawConfig()
return &raw, err
}
func convertRestConfigToClientConfig(config *rest.Config) clientcmd.ClientConfig {
contextName := "local"
kubeConfig := clientcmdapi.NewConfig()
kubeConfig.Contexts = map[string]*clientcmdapi.Context{
contextName: {
Cluster: contextName,
AuthInfo: contextName,
},
}
kubeConfig.Clusters = map[string]*clientcmdapi.Cluster{
contextName: {
Server: config.Host,
InsecureSkipTLSVerify: config.Insecure,
CertificateAuthorityData: config.CAData,
CertificateAuthority: config.CAFile,
},
}
kubeConfig.AuthInfos = map[string]*clientcmdapi.AuthInfo{
contextName: {
Token: config.BearerToken,
TokenFile: config.BearerTokenFile,
Impersonate: config.Impersonate.UserName,
ImpersonateGroups: config.Impersonate.Groups,
ImpersonateUserExtra: config.Impersonate.Extra,
ClientCertificate: config.CertFile,
ClientCertificateData: config.CertData,
ClientKey: config.KeyFile,
ClientKeyData: config.KeyData,
Username: config.Username,
Password: config.Password,
AuthProvider: config.AuthProvider,
Exec: config.ExecProvider,
},
}
kubeConfig.CurrentContext = contextName
return clientcmd.NewDefaultClientConfig(*kubeConfig, &clientcmd.ConfigOverrides{})
}