/
cluster.go
475 lines (390 loc) · 13.4 KB
/
cluster.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package api
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/apis"
)
// k8s cluster type
type ClusterType string
const (
// common k8s cluster with nodes
ClusterTypeDefault ClusterType = "default"
// nodeless k8s cluster
// ClusterTypeServerless ClusterType = "serverless"
)
type ModeType string
const (
// self build k8s cluster
ModeTypeSelfBuild ModeType = "customize"
// public cloud managed k8s cluster
// ModeTypeManaged ModeType = "managed"
// imported already exists k8s cluster
ModeTypeImport ModeType = "import"
)
type ProviderType string
const (
// system provider type means default v3 supervisor cluster
ProviderTypeSystem ProviderType = "system"
// default provider type by onecloud
ProviderTypeOnecloud ProviderType = "onecloud"
ProviderTypeOnecloudKvm ProviderType = "kvm"
// AWS provider
ProviderTypeAws ProviderType = "aws"
// Alibaba cloud provider
ProviderTypeAliyun ProviderType = "aliyun"
// Azure provider
ProviderTypeAzure ProviderType = "azure"
// Tencent cloud provider
ProviderTypeQcloud ProviderType = "qcloud"
// External provider type by import
ProviderTypeExternal ProviderType = "external"
)
const (
DefaultServiceCIDR string = "10.43.0.0/16"
DefaultServiceDomain string = "cluster.local"
DefaultPodCIDR string = "10.42.0.0/16"
)
type ClusterResourceType string
const (
ClusterResourceTypeHost = "host"
ClusterResourceTypeGuest = "guest"
ClusterResourceTypeUnknown = "unknown"
)
type MachineResourceType string
const (
MachineResourceTypeBaremetal = "baremetal"
MachineResourceTypeVm = "vm"
)
type RoleType string
const (
RoleTypeControlplane = "controlplane"
RoleTypeNode = "node"
)
const (
MachineStatusInit = "init"
MachineStatusCreating = "creating"
MachineStatusCreateFail = "create_fail"
MachineStatusPrepare = "prepare"
MachineStatusPrepareFail = "prepare_fail"
MachineStatusRunning = "running"
MachineStatusReady = "ready"
MachineStatusDeleting = "deleting"
MachineStatusDeleteFail = "delete_fail"
MachineStatusTerminating = "terminating"
MachineStatusTerminateFail = "terminate_fail"
ClusterStatusInit = "init"
ClusterStatusCreating = "creating"
ClusterStatusCreateFail = "create_fail"
ClusterStatusCreatingMachine = "creating_machine"
ClusterStatusCreateMachineFail = "create_machine_fail"
ClusterStatusDeploying = "deploying"
ClusterStatusDeployingFail = "deploy_fail"
ClusterStatusRunning = "running"
ClusterStatusLost = "lost"
ClusterStatusUnknown = "unknown"
ClusterStatusError = "error"
ClusterStatusDeleting = "deleting"
ClusterStatusDeleteFail = "delete_fail"
)
type ClusterDeployAction string
const (
ClusterDeployActionCreate ClusterDeployAction = "create"
ClusterDeployActionRun ClusterDeployAction = "run"
ClusterDeployActionScale ClusterDeployAction = "scale"
ClusterDeployActionUpgradeMasterConfig ClusterDeployAction = "upgrade-master-config"
ClusterDeployActionRemoveNode ClusterDeployAction = "remove-node"
)
type ClusterPreCheckResp struct {
Pass bool `json:"pass"`
ImageError string `json:"image_error"`
}
type ClusterCreateInput struct {
apis.StatusDomainLevelResourceCreateInput
IsSystem *bool `json:"is_system"`
ClusterType ClusterType `json:"cluster_type"`
ResourceType ClusterResourceType `json:"resource_type"`
Mode ModeType `json:"mode"`
Provider ProviderType `json:"provider"`
ServiceCidr string `json:"service_cidr"`
ServiceDomain string `json:"service_domain"`
PodCidr string `json:"pod_cidr"`
Version string `json:"version"`
HA bool `json:"ha"`
Machines []*CreateMachineData `json:"machines"`
ImageRepository *ImageRepository `json:"image_repository"`
CloudregionId string `json:"cloudregion_id"`
VpcId string `json:"vpc_id"`
ManagerId string `json:"manager_id"`
ExternalClusterId string `json:"external_cluster_id"`
ExternalCloudClusterId string `json:"external_cloud_cluster_id"`
// imported cluster data
ImportData *ImportClusterData `json:"import_data"`
// cluster addons config
AddonsConfig *ClusterAddonsManifestConfig `json:"addons_config"`
// cluster extra config
ExtraConfig *ClusterExtraConfig `json:"extra_config"`
}
type ImageRepository struct {
// url define cluster image repository url, e.g: registry.hub.docker.com/yunion
Url string `json:"url"`
// if insecure, the /etc/docker/daemon.json insecure-registries will add this registry
Insecure bool `json:"insecure"`
}
type K8sLabel struct {
Key string `json:"key"`
Value string `json:"value"`
}
type K8sNodeConfig struct {
Labels []*K8sLabel `json:"labels"`
}
type CreateMachineData struct {
Name string `json:"name"`
ClusterId string `json:"cluster_id"`
Role string `json:"role"`
Provider string `json:"provider"`
ResourceType string `json:"resource_type"`
ResourceId string `json:"resource_id"`
Address string `json:"address"`
FirstNode bool `json:"first_node"`
Config *MachineCreateConfig `json:"config"`
K8sNodeConfig *K8sNodeConfig `json:"k8s_node_config"`
ZoneId string `json:"zone_id"`
NetworkId string `json:"network_id"`
// CloudregionId will be inject by cluster
CloudregionId string `json:"-"`
VpcId string `json:"-"`
// ClusterDeployAction will be inject by background task
ClusterDeployAction ClusterDeployAction `json:"cluster_deploy_action"`
}
const (
ImportClusterDistributionK8s = "k8s"
ImportClusterDistributionOpenshift = "openshift"
)
type ImportClusterData struct {
Kubeconfig string `json:"kubeconfig"`
ApiServer string `json:"api_server"`
Distribution string `json:"distribution"`
// DistributionInfo should detect by import process
DistributionInfo ClusterDistributionInfo
}
type ClusterDistributionInfo struct {
Version string `json:"version"`
}
const (
ContainerSchedtag = "container"
DefaultCluster = "default"
)
type UsableInstance struct {
Id string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
type FederatedResourceUsedInput struct {
// federated resource keyword, e.g: federatednamespace
FederatedKeyword string `json:"federated_keyword"`
FederatedResourceId string `json:"federatedresource_id"`
// is used by specify federated resource
FederatedUsed *bool `json:"federated_used"`
}
func (input FederatedResourceUsedInput) ShouldDo() bool {
return input.FederatedKeyword != "" && input.FederatedResourceId != "" && input.FederatedUsed != nil
}
type ClusterListInput struct {
apis.StatusDomainLevelResourceListInput
FederatedResourceUsedInput
ManagerId []string `json:"manager_id"`
Manager []string `json:"manager" yunion-deprecated-by:"manager_id"`
ExternalClusterId string `json:"external_cluster_id"`
ExternalCloudClusterId string `json:"external_cloud_cluster_id"`
CloudregionId string `json:"cloudregion_id"`
Provider []string `json:"provider"`
Mode ModeType `json:"mode"`
}
type ClusterSyncInput struct {
// Force sync
Force bool `json:"force"`
}
type ClusterDeployInput struct {
// Force deploy
Force bool `json:"force"`
// Action can be 'run' or 'upgrade'
Action ClusterDeployAction `json:"action"`
SkipDownloads bool `json:"skip_downloads"`
}
type IClusterRemoteResource interface {
GetKey() string
}
type ClusterAPIGroupResource struct {
APIGroup string `json:"apiGroup,allowempty"`
APIResource metav1.APIResource `json:"apiResource"`
}
func (r ClusterAPIGroupResource) GetKey() string {
return fmt.Sprintf("%s/%s", r.APIGroup, r.APIResource.Name)
}
type IClusterRemoteResources interface {
GetByIndx(idx int) IClusterRemoteResource
Len() int
New() IClusterRemoteResources
Append(res IClusterRemoteResource) IClusterRemoteResources
Intersection(ors IClusterRemoteResources) IClusterRemoteResources
Unionset(ors IClusterRemoteResources) IClusterRemoteResources
}
func operateSet(rs IClusterRemoteResources, ors IClusterRemoteResources, isUnion bool) IClusterRemoteResources {
marks := make(map[string]IClusterRemoteResource, 0)
oMarks := make(map[string]IClusterRemoteResource, 0)
for i := 0; i < rs.Len(); i++ {
res := rs.GetByIndx(i)
key := res.GetKey()
marks[key] = res
}
for i := 0; i < ors.Len(); i++ {
res := ors.GetByIndx(i)
key := res.GetKey()
oMarks[key] = res
}
for key, res := range oMarks {
_, ok := marks[key]
if isUnion {
if ok {
continue
} else {
marks[key] = res
}
} else {
if ok {
continue
} else {
delete(marks, key)
}
}
}
ret := rs.New()
for _, res := range marks {
ret.Append(res)
}
return ret
}
type ClusterAPIGroupResources []ClusterAPIGroupResource
func (rs ClusterAPIGroupResources) New() IClusterRemoteResources {
ret := make([]ClusterAPIGroupResource, 0)
return ClusterAPIGroupResources(ret)
}
func (rs ClusterAPIGroupResources) Append(res IClusterRemoteResource) IClusterRemoteResources {
rs = append(rs, res.(ClusterAPIGroupResource))
return rs
}
func (rs ClusterAPIGroupResources) GetByIndx(idx int) IClusterRemoteResource {
return rs[idx]
}
func (rs ClusterAPIGroupResources) Len() int {
return len(rs)
}
func (rs ClusterAPIGroupResources) operate(ors IClusterRemoteResources, isUnion bool) IClusterRemoteResources {
return operateSet(rs, ors, isUnion)
}
func (rs ClusterAPIGroupResources) Intersection(ors IClusterRemoteResources) IClusterRemoteResources {
return rs.operate(ors, false)
}
func (rs ClusterAPIGroupResources) Unionset(ors IClusterRemoteResources) IClusterRemoteResources {
return rs.operate(ors, true)
}
type ClusterUser struct {
Name string `json:"name"`
// FullName is the full name of user
FullName string `json:"fullName"`
// Identities are the identities associated with this user
Identities []string `json:"identities"`
// Groups specifies group names this user is a member of.
// This field is deprecated and will be removed in a future release.
// Instead, create a Group object containing the name of this User.
Groups []string `json:"groups"`
}
func (user ClusterUser) GetKey() string {
return fmt.Sprintf("%s/%s", user.Name, user.FullName)
}
type ClusterUsers []ClusterUser
func (rs ClusterUsers) New() IClusterRemoteResources {
ret := make([]ClusterUser, 0)
return ClusterUsers(ret)
}
func (rs ClusterUsers) Append(res IClusterRemoteResource) IClusterRemoteResources {
rs = append(rs, res.(ClusterUser))
return rs
}
func (rs ClusterUsers) GetByIndx(idx int) IClusterRemoteResource {
return rs[idx]
}
func (rs ClusterUsers) Len() int {
return len(rs)
}
func (rs ClusterUsers) operate(ors IClusterRemoteResources, isUnion bool) IClusterRemoteResources {
return operateSet(rs, ors, isUnion)
}
func (rs ClusterUsers) Intersection(ors IClusterRemoteResources) IClusterRemoteResources {
return rs.operate(ors, false)
}
func (rs ClusterUsers) Unionset(ors IClusterRemoteResources) IClusterRemoteResources {
return rs.operate(ors, true)
}
type OptionalNames []string
func (t OptionalNames) String() string {
return fmt.Sprintf("%v", []string(t))
}
type ClusterUserGroup struct {
Name string `json:"name"`
// Users is the list of users in this group.
Users OptionalNames `json:"users"`
}
func (g ClusterUserGroup) GetKey() string {
return g.Name
}
type ClusterUserGroups []ClusterUserGroup
func (rs ClusterUserGroups) New() IClusterRemoteResources {
ret := make([]ClusterUserGroup, 0)
return ClusterUserGroups(ret)
}
func (rs ClusterUserGroups) Append(res IClusterRemoteResource) IClusterRemoteResources {
rs = append(rs, res.(ClusterUserGroup))
return rs
}
func (rs ClusterUserGroups) GetByIndx(idx int) IClusterRemoteResource {
return rs[idx]
}
func (rs ClusterUserGroups) Len() int {
return len(rs)
}
func (rs ClusterUserGroups) operate(ors IClusterRemoteResources, isUnion bool) IClusterRemoteResources {
return operateSet(rs, ors, isUnion)
}
func (rs ClusterUserGroups) Intersection(ors IClusterRemoteResources) IClusterRemoteResources {
return rs.operate(ors, false)
}
func (rs ClusterUserGroups) Unionset(ors IClusterRemoteResources) IClusterRemoteResources {
return rs.operate(ors, true)
}
type ClusterPurgeInput struct {
apis.Meta
Force bool `json:"force"`
}
type ClusterGetAddonsInput struct {
EnableNativeIPAlloc bool `json:"enable_native_ip_alloc"`
}
type ClusterAddonNetworkConfig struct {
EnableNativeIPAlloc bool `json:"enable_native_ip_alloc"`
}
type ClusterAddonsManifestConfig struct {
Network ClusterAddonNetworkConfig `json:"network"`
}
type ClusterKubesprayConfig struct {
InventoryContent string `json:"inventory_content"`
PrivateKey string `json:"private_key"`
Vars jsonutils.JSONObject `json:"vars"`
}
type ClusterExtraConfig struct {
DockerRegistryMirrors []string `json:"docker_registry_mirrors"`
DockerInsecureRegistries []string `json:"docker_insecure_registries"`
}
type ClusterSetKubeconfig struct {
Kubeconfig string `json:"kubeconfig"`
}