-
Notifications
You must be signed in to change notification settings - Fork 815
/
client.go
239 lines (213 loc) · 8.02 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright 2021 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"fmt"
"github.com/koderover/zadig/pkg/config"
"istio.io/client-go/pkg/clientset/versioned/typed/networking/v1alpha3"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/koderover/zadig/pkg/setting"
aslanClient "github.com/koderover/zadig/pkg/shared/client/aslan"
"github.com/koderover/zadig/pkg/tool/kube/multicluster"
)
func GetKubeClient(hubserverAddr, clusterID string) (client.Client, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetKubeClient(hubserverAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
if cluster == nil {
return nil, fmt.Errorf("cluster %s not found", clusterID)
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetKubeClient(hubserverAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetKubeClientFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetKubeClientSet(hubServerAddr, clusterID string) (*kubernetes.Clientset, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetKubeClientSet(hubServerAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetKubeClientSet(hubServerAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetKubeClientSetFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetKubeMetricsClient(hubserverAddr, clusterID string) (*v1beta1.MetricsV1beta1Client, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetKubeMetricsClient(hubserverAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetKubeMetricsClient(hubserverAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetKubeMetricsClientFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetDynamicKubeClient(hubserverAddr, clusterID string) (dynamic.Interface, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetDynamicKubeclient(hubserverAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetDynamicKubeclient(hubserverAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetDynamicKubeclientFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetIstioClientV1Alpha3Client(hubserverAddr, clusterID string) (*v1alpha3.NetworkingV1alpha3Client, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetIstioV1Alpha3Client(hubserverAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetIstioV1Alpha3Client(hubserverAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetIstioV1Alpha3ClientFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetKubeAPIReader(hubServerAddr, clusterID string) (client.Reader, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetKubeAPIReader(hubServerAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetKubeAPIReader(hubServerAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetKubeClientFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetRESTConfig(hubServerAddr, clusterID string) (*rest.Config, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetRESTConfig(hubServerAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
if cluster == nil {
return nil, fmt.Errorf("cluster %s not found", clusterID)
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetRESTConfig(hubServerAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetRestConfigFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetClientset(hubServerAddr, clusterID string) (kubernetes.Interface, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetClientset(hubServerAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetClientset(hubServerAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetClientSetFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create kubeclient: unknown cluster type: %s", cluster.Type)
}
}
func GetDiscoveryClient(hubServerAddr, clusterID string) (*discovery.DiscoveryClient, error) {
if clusterID == setting.LocalClusterID || clusterID == "" {
if clusterID == setting.LocalClusterID {
clusterID = ""
}
return multicluster.GetDiscoveryClient(hubServerAddr, clusterID)
}
cluster, err := aslanClient.New(config.AslanServiceAddress()).GetClusterInfo(clusterID)
if err != nil {
return nil, err
}
switch cluster.Type {
case setting.AgentClusterType, "":
return multicluster.GetDiscoveryClient(hubServerAddr, clusterID)
case setting.KubeConfigClusterType:
return multicluster.GetDiscoveryClientFromKubeConfig(clusterID, cluster.KubeConfig)
default:
return nil, fmt.Errorf("failed to create discovery client: unknown cluster type: %s", cluster.Type)
}
}