-
Notifications
You must be signed in to change notification settings - Fork 43
/
client.go
66 lines (59 loc) · 1.46 KB
/
client.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
package kube
import (
"github.com/baetyl/baetyl-go/v2/log"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/baetyl/baetyl-cloud/v2/common"
"github.com/baetyl/baetyl-cloud/v2/plugin"
clientset "github.com/baetyl/baetyl-cloud/v2/plugin/kube/client/clientset/versioned"
)
type client struct {
coreV1 corev1.CoreV1Interface
customClient clientset.Interface
cfg CloudConfig
aesKey []byte
log *log.Logger
}
// Close Close
func (c *client) Close() error {
return nil
}
func init() {
plugin.RegisterFactory("kube", New)
plugin.RegisterFactory("kubernetes", New)
}
// New New
func New() (plugin.Plugin, error) {
var cfg CloudConfig
if err := common.LoadConfig(&cfg); err != nil {
return nil, err
}
kubeConfig, err := func() (*rest.Config, error) {
if !cfg.Kube.OutCluster {
return rest.InClusterConfig()
}
return clientcmd.BuildConfigFromFlags(
"", cfg.Kube.ConfigPath)
}()
if err != nil {
return nil, err
}
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return nil, err
}
kubeClient.CoreV1()
customClient, err := clientset.NewForConfig(kubeConfig)
if err != nil {
return nil, err
}
return &client{
coreV1: kubeClient.CoreV1(),
customClient: customClient,
cfg: cfg,
aesKey: []byte(cfg.Kube.AES.Key),
log: log.With(log.Any("plugin", "kube")),
}, nil
}