This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubeconfig.go
123 lines (111 loc) · 2.61 KB
/
kubeconfig.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package fetchkube
import (
"fmt"
"io/ioutil"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
func Encode(conf *api.Config) ([]byte, error) {
return clientcmd.Write(*conf)
}
func Decode(conf []byte) (*api.Config, error) {
return clientcmd.Load(conf)
}
func StartingConfig() (*api.Config, error) {
return clientcmd.DefaultClientConfig.ConfigAccess().GetStartingConfig()
}
func Contexts(conf *api.Config) ([]string, error) {
ctxs := make([]string, 0, len(conf.Contexts))
for ctx := range conf.Contexts {
ctxs = append(ctxs, ctx)
}
return ctxs, nil
}
func FetchOnlyDefault(conf *api.Config) (*api.Config, error) {
newConf := api.NewConfig()
if conf.CurrentContext == "" {
return nil, fmt.Errorf("context cannot be empty")
}
ctx, ok := conf.Contexts[conf.CurrentContext]
if !ok {
return nil, fmt.Errorf("not found context %q", conf.CurrentContext)
}
newConf.Contexts[conf.CurrentContext] = ctx
cluster, ok := conf.Clusters[ctx.Cluster]
if !ok {
return nil, fmt.Errorf("not found cluster %q", ctx.Cluster)
}
newConf.Clusters[ctx.Cluster] = cluster
auth, ok := conf.AuthInfos[ctx.AuthInfo]
if !ok {
return nil, fmt.Errorf("not found users %q", ctx.AuthInfo)
}
newConf.AuthInfos[ctx.AuthInfo] = auth
newConf.CurrentContext = conf.CurrentContext
return newConf, nil
}
func ResolveLocalPaths(conf *api.Config) (*api.Config, error) {
err := clientcmd.ResolveLocalPaths(conf)
if err != nil {
return nil, err
}
return conf, nil
}
func InlineData(conf *api.Config) (*api.Config, error) {
for _, cluster := range conf.Clusters {
err := loadFileBytes(&cluster.CertificateAuthorityData, &cluster.CertificateAuthority)
if err != nil {
return nil, err
}
}
for _, auth := range conf.AuthInfos {
err := loadFileBytes(&auth.ClientCertificateData, &auth.ClientCertificate)
if err != nil {
return nil, err
}
err = loadFileBytes(&auth.ClientKeyData, &auth.ClientKey)
if err != nil {
return nil, err
}
err = loadFileString(&auth.Token, &auth.TokenFile)
if err != nil {
return nil, err
}
}
return conf, nil
}
func loadFileBytes(data *[]byte, filepath *string) error {
if *filepath == "" {
return nil
}
defer func() {
*filepath = ""
}()
var err error
if len(*data) != 0 {
return nil
}
*data, err = ioutil.ReadFile(*filepath)
if err != nil {
return err
}
return nil
}
func loadFileString(data *string, filepath *string) error {
if *filepath == "" {
return nil
}
defer func() {
*filepath = ""
}()
var err error
if len(*data) != 0 {
return nil
}
d, err := ioutil.ReadFile(*filepath)
if err != nil {
return err
}
*data = string(d)
return nil
}