-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcluster_config.go
97 lines (84 loc) · 2.6 KB
/
cluster_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
package clusterconfig
import (
"fmt"
"os"
"path"
"github.com/bonnefoa/kubectl-fzf/v3/internal/k8s/resources"
"github.com/bonnefoa/kubectl-fzf/v3/internal/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type ClusterConfig struct {
clusterName string
destDir string
cacheDir string
apiConfig *clientcmdapi.Config
}
func NewClusterConfig(clusterConfigCli *ClusterConfigCli) ClusterConfig {
c := ClusterConfig{}
c.clusterName = clusterConfigCli.ClusterName
c.cacheDir = clusterConfigCli.CacheDir
c.destDir = path.Join(c.cacheDir, c.clusterName)
return c
}
func (c *ClusterConfig) LoadClusterConfig() (err error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
c.apiConfig, err = loadingRules.Load()
if err != nil {
return errors.Wrap(err, "error reading kubeconfig file")
}
c.clusterName = c.apiConfig.CurrentContext
if c.clusterName == "" {
logrus.Infof("Couldn't read kubeconfig file, assuming incluster")
c.clusterName = "incluster"
}
c.destDir = path.Join(c.cacheDir, c.clusterName)
logrus.Debugf("Cluster config set to target '%s'", c.destDir)
return nil
}
func (c *ClusterConfig) CreateDestDir() error {
if c.clusterName == "" {
return errors.New("clustername is empty, call LoadClusterConfig before")
}
logrus.Infof("Creating destination dir '%s'", c.destDir)
err := os.MkdirAll(c.destDir, os.ModePerm)
return err
}
func (c *ClusterConfig) GetResourceStorePath(r resources.ResourceType) string {
return path.Join(c.destDir, r.String())
}
func (c *ClusterConfig) FileStoreExists(r resources.ResourceType) bool {
p := c.GetResourceStorePath(r)
return util.FileExists(p)
}
func (c *ClusterConfig) GetClientset() (*kubernetes.Clientset, error) {
restConfig, err := c.GetClientConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(restConfig)
return clientset, err
}
func (c *ClusterConfig) GetNamespace() (string, error) {
contextStruct, ok := c.apiConfig.Contexts[c.apiConfig.CurrentContext]
if !ok {
return "", fmt.Errorf("context %s not found in config", c.apiConfig.CurrentContext)
}
return contextStruct.Namespace, nil
}
func (c *ClusterConfig) GetContext() string {
return c.clusterName
}
func (c *ClusterConfig) GetClientConfig() (*rest.Config, error) {
restConfig, err := rest.InClusterConfig()
if err == nil {
return restConfig, nil
}
cmdConfig := clientcmd.NewDefaultClientConfig(*c.apiConfig, nil)
restConfig, err = cmdConfig.ClientConfig()
return restConfig, err
}