/
operate.go
271 lines (246 loc) · 7.41 KB
/
operate.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package manager
import (
"context"
"errors"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
rlsv2 "k8s.io/helm/pkg/proto/hapi/release"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// BaseCluster is used to shield the complexity of the underlying multi-cluster and single cluster.
type BaseCluster interface {
GetOriginKubeCli(clusterNames ...string) (kubernetes.Interface, error)
GetPod(opts types.NamespacedName, clusterNames ...string) (*corev1.Pod, error)
GetPods(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Pod, error)
GetNodes(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Node, error)
GetDeployment(opts *client.ListOptions, clusterNames ...string) ([]*appv1.Deployment, error)
GetStatefulsets(opts *client.ListOptions, clusternames ...string) ([]*appv1.StatefulSet, error)
GetService(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Service, error)
GetEndpoints(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Endpoints, error)
GetEvent(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Event, error)
DeletePods(opts *client.ListOptions, clusterNames ...string) error
DeletePod(opts types.NamespacedName, clusterNames ...string) error
}
// CustomeCluster extend the methods of the basic cluster, including some special business methods.
type CustomeCluster interface {
BaseCluster
GetHelmRelease(opts map[string]string, clusterNames ...string) ([]*rlsv2.Release, error)
}
// GetOriginKubeCli returns the kubecli of the master cluster client if len(clusterNames) == 0, otherwise
// returns the specific kubecli.
func (m *ClusterManager) GetOriginKubeCli(clusterNames ...string) (kubernetes.Interface, error) {
if len(clusterNames) > 0 {
if len(clusterNames) > 1 {
return nil, errors.New("too many clusterNames")
}
cluster, err := m.Get(clusterNames[0])
if err != nil {
return nil, err
}
return cluster.KubeCli, nil
}
return m.KubeCli, nil
}
// GetPod ...
func (m *ClusterManager) GetPod(opts types.NamespacedName, clusterNames ...string) (*corev1.Pod, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
pod := &corev1.Pod{}
for _, cluster := range clusters {
err := cluster.Client.Get(ctx, opts, pod)
if err != nil {
return nil, err
}
}
return pod, nil
}
// GetPods ...
func (m *ClusterManager) GetPods(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Pod, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*corev1.Pod, 0)
for _, cluster := range clusters {
podList := &corev1.PodList{}
err := cluster.Client.List(ctx, podList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range podList.Items {
pod := &podList.Items[i]
result = append(result, pod)
}
}
return result, nil
}
// GetNodes ...
func (m *ClusterManager) GetNodes(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Node, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*corev1.Node, 0)
for _, cluster := range clusters {
nodeList := &corev1.NodeList{}
err := cluster.Client.List(ctx, nodeList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range nodeList.Items {
node := &nodeList.Items[i]
result = append(result, node)
}
}
return result, nil
}
// GetDeployment ...
func (m *ClusterManager) GetDeployment(opts *client.ListOptions, clusterNames ...string) ([]*appv1.Deployment, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*appv1.Deployment, 0)
for _, cluster := range clusters {
deployList := &appv1.DeploymentList{}
err := cluster.Client.List(ctx, deployList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range deployList.Items {
deploy := &deployList.Items[i]
result = append(result, deploy)
}
}
return result, nil
}
// GetStatefulsets ...
func (m *ClusterManager) GetStatefulsets(opts *client.ListOptions, clusterNames ...string) ([]*appv1.StatefulSet, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*appv1.StatefulSet, 0)
for _, cluster := range clusters {
staList := &appv1.StatefulSetList{}
err := cluster.Client.List(ctx, staList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range staList.Items {
sta := &staList.Items[i]
result = append(result, sta)
}
}
return result, nil
}
// GetService ...
func (m *ClusterManager) GetService(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Service, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*corev1.Service, 0)
for _, cluster := range clusters {
serviceList := &corev1.ServiceList{}
err := cluster.Client.List(ctx, serviceList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range serviceList.Items {
service := &serviceList.Items[i]
result = append(result, service)
}
}
return result, nil
}
// GetEndpoints ...
func (m *ClusterManager) GetEndpoints(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Endpoints, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*corev1.Endpoints, 0)
for _, cluster := range clusters {
endpointsList := &corev1.EndpointsList{}
err := cluster.Client.List(ctx, endpointsList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range endpointsList.Items {
endpoints := &endpointsList.Items[i]
result = append(result, endpoints)
}
}
return result, nil
}
// GetEvent ...
func (m *ClusterManager) GetEvent(opts *client.ListOptions, clusterNames ...string) ([]*corev1.Event, error) {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
result := make([]*corev1.Event, 0)
for _, cluster := range clusters {
eventList := &corev1.EventList{}
err := cluster.Client.List(ctx, eventList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, err
}
for i := range eventList.Items {
event := &eventList.Items[i]
result = append(result, event)
}
}
return result, nil
}
// DeletePods ...
func (m *ClusterManager) DeletePods(opts *client.ListOptions, clusterNames ...string) error {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
for _, cluster := range clusters {
podList := &corev1.PodList{}
err := cluster.Client.List(ctx, podList, opts)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return err
}
for _, pod := range podList.Items {
err = cluster.Client.Delete(ctx, pod.DeepCopyObject())
if err != nil {
klog.Errorf("delete pod error: %v", err)
}
}
}
return nil
}
// DeletePod ...
func (m *ClusterManager) DeletePod(opts types.NamespacedName, clusterNames ...string) error {
clusters := m.GetAll(clusterNames...)
ctx := context.Background()
pod := &corev1.Pod{}
for _, cluster := range clusters {
err := cluster.Client.Get(ctx, opts, pod)
if err != nil {
return err
}
err = cluster.Client.Delete(ctx, pod)
if err != nil {
klog.Errorf("delete pod error: %v", err)
}
}
return nil
}