forked from shipengqi/kube
/
config.go
310 lines (258 loc) · 7.06 KB
/
config.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package kube
import (
"errors"
"fmt"
"sync"
"time"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
const (
// DefaultTimeout default request timeout.
DefaultTimeout = 10 * time.Second
// UsePersistentConfig caches client config to avoid reloads.
UsePersistentConfig = true
)
// Config represents a kubernetes configuration.
// references:
// - https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
// - https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
type Config struct {
flags *genericclioptions.ConfigFlags
mutex *sync.RWMutex
}
// NewConfig returns a new kube client configuration.
func NewConfig(f *genericclioptions.ConfigFlags) *Config {
return &Config{
flags: f,
mutex: &sync.RWMutex{},
}
}
// Timeout returns the request timeout if set or the default if not set.
func (c *Config) Timeout() time.Duration {
if !isset(c.flags.Timeout) {
return DefaultTimeout
}
dur, err := time.ParseDuration(*c.flags.Timeout)
if err != nil {
return DefaultTimeout
}
return dur
}
// RESTConfig returns a complete rest client config.
func (c *Config) RESTConfig() (*rest.Config, error) {
return c.clientConfig().ClientConfig()
}
// Flags returns configuration flags.
func (c *Config) Flags() *genericclioptions.ConfigFlags {
return c.flags
}
func (c *Config) RawConfig() (clientcmdapi.Config, error) {
return c.clientConfig().RawConfig()
}
func (c *Config) clientConfig() clientcmd.ClientConfig {
return c.flags.ToRawKubeConfigLoader()
}
// CurrentContextName returns the currently active config context.
func (c *Config) CurrentContextName() (string, error) {
if isset(c.flags.Context) {
return *c.flags.Context, nil
}
cfg, err := c.RawConfig()
if err != nil {
return "", err
}
return cfg.CurrentContext, nil
}
// GetContext returns a context with the given name.
func (c *Config) GetContext(n string) (*clientcmdapi.Context, error) {
cfg, err := c.RawConfig()
if err != nil {
return nil, err
}
if ctx, ok := cfg.Contexts[n]; ok {
return ctx, nil
}
return nil, fmt.Errorf("context: %s not found", n)
}
// Contexts returns all available contexts.
func (c *Config) Contexts() (map[string]*clientcmdapi.Context, error) {
cfg, err := c.RawConfig()
if err != nil {
return nil, err
}
return cfg.Contexts, nil
}
// UseContext changes the current context.
func (c *Config) UseContext(name string) error {
if _, err := c.GetContext(name); err != nil {
return fmt.Errorf("context %q not found", name)
}
return c.useContext(name)
}
// SetContext set k/v of the given context or add a new context if not exist.
func (c *Config) SetContext(name string, ctx *clientcmdapi.Context) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
cfg.Contexts[name] = ctx
return c.modify(cfg)
}
// RemoveContext remove the given context from the configuration.
func (c *Config) RemoveContext(n string) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
delete(cfg.Contexts, n)
return c.modify(cfg)
}
// ClusterNameFromContext returns the cluster associated with the given context.
func (c *Config) ClusterNameFromContext(context string) (string, error) {
cfg, err := c.RawConfig()
if err != nil {
return "", err
}
if ctx, ok := cfg.Contexts[context]; ok {
return ctx.Cluster, nil
}
return "", fmt.Errorf("cluster not found from context: %s", context)
}
// CurrentClusterName returns the active cluster name.
func (c *Config) CurrentClusterName() (string, error) {
if isset(c.flags.ClusterName) {
return *c.flags.ClusterName, nil
}
cfg, err := c.RawConfig()
if err != nil {
return "", err
}
context, err := c.CurrentContextName()
if err != nil {
context = cfg.CurrentContext
}
if ctx, ok := cfg.Contexts[context]; ok {
return ctx.Cluster, nil
}
return "", errors.New("current cluster not found")
}
// ClusterNames returns all kubeconfig defined clusters.
func (c *Config) ClusterNames() ([]string, error) {
cfg, err := c.RawConfig()
if err != nil {
return nil, err
}
cc := make([]string, 0)
for name := range cfg.Clusters {
cc = append(cc, name)
}
return cc, nil
}
// SetCluster set k/v of the given cluster or add a new cluster if not exist.
func (c *Config) SetCluster(name string, cluster *clientcmdapi.Cluster) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
cfg.Clusters[name] = cluster
return c.modify(cfg)
}
// RemoveCluster remove the given cluster from the configuration.
func (c *Config) RemoveCluster(name string) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
delete(cfg.Clusters, name)
return c.modify(cfg)
}
// GetCluster returns a cluster with the given name.
func (c *Config) GetCluster(name string) (*clientcmdapi.Cluster, error) {
cfg, err := c.RawConfig()
if err != nil {
return nil, err
}
if cluster, ok := cfg.Clusters[name]; ok {
return cluster, nil
}
return nil, fmt.Errorf("cluster: %s not found", name)
}
// CurrentUserName retrieves the active user name.
func (c *Config) CurrentUserName() (string, error) {
if isset(c.flags.Impersonate) {
return *c.flags.Impersonate, nil
}
if isset(c.flags.AuthInfoName) {
return *c.flags.AuthInfoName, nil
}
cfg, err := c.RawConfig()
if err != nil {
return "", err
}
current := cfg.CurrentContext
if isset(c.flags.Context) {
current = *c.flags.Context
}
if ctx, ok := cfg.Contexts[current]; ok {
return ctx.AuthInfo, nil
}
return "", errors.New("current user not found")
}
// SetCredential set k/v of the given credential or add a new credential if not exist.
func (c *Config) SetCredential(name string, auth *clientcmdapi.AuthInfo) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
cfg.AuthInfos[name] = auth
return c.modify(cfg)
}
// RemoveCredential remove the given credential from the configuration.
func (c *Config) RemoveCredential(name string) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
delete(cfg.AuthInfos, name)
return c.modify(cfg)
}
// GetCredential returns a credential with the given name.
func (c *Config) GetCredential(name string) (*clientcmdapi.AuthInfo, error) {
cfg, err := c.RawConfig()
if err != nil {
return nil, err
}
if auth, ok := cfg.AuthInfos[name]; ok {
return auth, nil
}
return nil, fmt.Errorf("credential: %s not found", name)
}
// CurrentNamespace retrieves the active namespace.
func (c *Config) CurrentNamespace() (string, error) {
ns, _, err := c.clientConfig().Namespace()
return ns, err
}
// ConfigAccess returns the current kubeconfig api server access configuration.
func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.clientConfig().ConfigAccess(), nil
}
func (c *Config) useContext(name string) error {
cfg, err := c.RawConfig()
if err != nil {
return err
}
cfg.CurrentContext = name
return c.modify(cfg)
}
func (c *Config) modify(cfg clientcmdapi.Config) error {
acc, err := c.ConfigAccess()
if err != nil {
return err
}
return clientcmd.ModifyConfig(acc, cfg, true)
}