/
proxyclient.go
114 lines (96 loc) · 3.34 KB
/
proxyclient.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
package client
import (
"errors"
"fmt"
multicluster "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/transport"
"github.com/symcn/api"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
rtclient "sigs.k8s.io/controller-runtime/pkg/client"
)
type proxyClient struct {
clusterCfg api.ClusterCfgInfo
scheme *runtime.Scheme
kubeRestConfig *rest.Config
kubeInterface kubernetes.Interface
dynamicInterface dynamic.Interface
runtimeInterface rtclient.Client
}
func NewProxyGatewayMingleClient(clusterCfg api.ClusterCfgInfo, scheme *runtime.Scheme) (api.MingleProxyClient, error) {
pcli := &proxyClient{
clusterCfg: clusterCfg,
scheme: scheme,
}
// 1. pre check
if err := pcli.preCheck(); err != nil {
return nil, err
}
// 3. initialization
if err := pcli.initialization(); err != nil {
return nil, err
}
return pcli, nil
}
func (pc *proxyClient) preCheck() error {
// clusterconfig and cluster name must not empty
if pc.clusterCfg == nil || pc.clusterCfg.GetName() == "" {
return errors.New("proxy cluster info is empty or cluster name is empty")
}
return nil
}
func (pc *proxyClient) initialization() error {
var err error
// Step 1. build restconfig
pc.kubeRestConfig, err = buildClientCmd(pc.clusterCfg, nil)
if err != nil {
return fmt.Errorf("proxy cluster %s build kubernetes failed %+v", pc.clusterCfg.GetName(), err)
}
pc.kubeRestConfig.Wrap(multicluster.NewProxyPathPrependingClusterGatewayRoundTripper(pc.clusterCfg.GetName()).NewRoundTripper)
// Step 2. build kubernetes interface
pc.kubeInterface, err = kubernetes.NewForConfig(pc.kubeRestConfig)
if err != nil {
return fmt.Errorf("proxy cluster %s build kubernetes interface failed %+v", pc.clusterCfg.GetName(), err)
}
// Step 3. build dynamic interface
pc.dynamicInterface, err = dynamic.NewForConfig(pc.kubeRestConfig)
if err != nil {
return fmt.Errorf("proxy cluster %s build dynamic interface failed %+v", pc.clusterCfg.GetName(), err)
}
// // Step 4. build runtime client use lazy load
// pc.runtimeInterface, err = rtclient.New(pc.kubeRestConfig, rtclient.Options{})
// if err != nil {
// return fmt.Errorf("proxy cluster %s build runtime client failed %+v", pc.clusterCfg.GetName(), err)
// }
return nil
}
// GetRestConfig return Kubernetes rest Config
func (pc *proxyClient) GetKubeRestConfig() *rest.Config {
return pc.kubeRestConfig
}
// GetKubeInterface return Kubernetes Interface.
// kubernetes.ClientSet impl kubernetes.Interface
func (pc *proxyClient) GetKubeInterface() kubernetes.Interface {
return pc.kubeInterface
}
// GetDynamicInterface return dynamic Interface.
// dynamic.ClientSet impl dynamic.Interface
func (pc *proxyClient) GetDynamicInterface() dynamic.Interface {
return pc.dynamicInterface
}
// GetRuntimeClient() return controller runtime client
func (pc *proxyClient) GetRuntimeClient() rtclient.Client {
if pc.runtimeInterface == nil {
var err error
pc.runtimeInterface, err = rtclient.New(pc.kubeRestConfig, rtclient.Options{Scheme: pc.scheme})
if err != nil {
panic(fmt.Errorf("proxy cluster %s build runtime client failed %+v", pc.clusterCfg.GetName(), err))
}
}
return pc.runtimeInterface
}
// GetClusterCfgInfo returns cluster configuration info
func (pc *proxyClient) GetClusterCfgInfo() api.ClusterCfgInfo {
return pc.clusterCfg
}