generated from replicatedhq/krew-plugin-template
/
common.go
37 lines (34 loc) · 938 Bytes
/
common.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
package fleet
import (
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
// csForContext returns a client for a given context
func csForContext(cfg api.Config, context string) (*kubernetes.Clientset, error) {
config, err := clientcmd.NewNonInteractiveClientConfig(
cfg,
context,
&clientcmd.ConfigOverrides{
CurrentContext: context,
},
nil).ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "Can't switch context")
}
cs, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "Can't create a client based on config and/or context provided")
}
return cs, nil
}
// contextOf returns the context name of a given cluster
func contextOf(cfg api.Config, clusterID string) string {
for name, context := range cfg.Contexts {
if clusterID == context.Cluster {
return name
}
}
return ""
}