generated from datewu/project-abc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
52 lines (45 loc) · 1.15 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
package k8s
import (
"path/filepath"
"github.com/datewu/gtea/jsonlog"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
// Conf ...
type Conf struct {
Mode, ConfFile string
}
var classicalClientSet *kubernetes.Clientset
// InitClientSet ...
func InitClientSet(c *Conf) error {
restConfig := loadRestConfig(c)
cli, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
classicalClientSet = cli
return nil
}
func loadRestConfig(conf *Conf) *rest.Config {
var c *rest.Config
var err error
if conf.Mode == "development" {
home := homedir.HomeDir()
conf.ConfFile = filepath.Join(home, ".kube", "config")
}
switch conf.ConfFile {
case "in-cluster":
jsonlog.Debug("using in-cluster configuration", nil)
c, err = rest.InClusterConfig()
default:
jsonlog.Debug("using outer cluster configuration", map[string]any{"configPath": conf.ConfFile})
c, err = clientcmd.BuildConfigFromFlags("", conf.ConfFile)
}
if err != nil {
jsonlog.Err(err, map[string]any{"configPath": conf.ConfFile, "msg": "cannot load kube-client config info"})
panic(err)
}
return c
}