This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 875
/
cluster_import.go
551 lines (512 loc) · 17.6 KB
/
cluster_import.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package service
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/KubeOperator/KubeOperator/pkg/constant"
"github.com/KubeOperator/KubeOperator/pkg/db"
"github.com/KubeOperator/KubeOperator/pkg/dto"
"github.com/KubeOperator/KubeOperator/pkg/logger"
"github.com/KubeOperator/KubeOperator/pkg/model"
"github.com/KubeOperator/KubeOperator/pkg/repository"
clusterUtil "github.com/KubeOperator/KubeOperator/pkg/util/cluster"
"github.com/icza/dyno"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
type ClusterImportService interface {
Import(clusterImport dto.ClusterImport) error
LoadClusterInfo(loadInfo *dto.ClusterLoad) (dto.ClusterLoadInfo, error)
}
type clusterImportService struct {
clusterRepo repository.ClusterRepository
projectRepository repository.ProjectRepository
projectResourceRepository repository.ProjectResourceRepository
msgService MsgService
componentService ComponentService
}
func NewClusterImportService() *clusterImportService {
return &clusterImportService{
clusterRepo: repository.NewClusterRepository(),
projectRepository: repository.NewProjectRepository(),
projectResourceRepository: repository.NewProjectResourceRepository(),
msgService: NewMsgService(),
componentService: NewComponentService(),
}
}
func (c clusterImportService) Import(clusterImport dto.ClusterImport) error {
loginfo, _ := json.Marshal(clusterImport)
logger.Log.WithFields(logrus.Fields{"cluster_import_info": string(loginfo)}).Debugf("start to import the cluster %s", clusterImport.Name)
project, err := c.projectRepository.Get(clusterImport.ProjectName)
if err != nil {
return err
}
cluster, err := clusterImport.ClusterImportDto2Mo()
if err != nil {
return err
}
cluster.ProjectID = project.ID
tx := db.DB.Begin()
if err := tx.Create(&cluster.Secret).Error; err != nil {
tx.Rollback()
return fmt.Errorf("can not create cluster secret %s", err.Error())
}
cluster.SecretID = cluster.Secret.ID
if err := tx.Create(&cluster).Error; err != nil {
tx.Rollback()
return fmt.Errorf("can not create cluster secret %s", err.Error())
}
var synchosts []dto.HostSync
tools := cluster.PrepareTools()
if clusterImport.IsKoCluster {
masterNum := 0
workerNum := 0
for _, node := range clusterImport.KoClusterInfo.Nodes {
host := model.Host{
Name: node.Name,
Ip: node.Ip,
Port: node.Port,
CredentialID: node.CredentialID,
ClusterID: cluster.ID,
Status: constant.StatusInitializing,
Architecture: node.Architecture,
}
switch clusterImport.KoClusterInfo.NodeNameRule {
case constant.NodeNameRuleIP, constant.NodeNameRuleHostName:
host.Name = node.Name
case constant.NodeNameRuleDefault:
no := 0
if node.Role == constant.NodeRoleNameMaster {
masterNum++
no = masterNum
} else {
workerNum++
no = workerNum
}
host.Name = fmt.Sprintf("%s-%s-%d", cluster.Name, node.Role, no)
}
if err := tx.Create(&host).Error; err != nil {
c.handlerImportError(tx, cluster, err)
return err
}
synchosts = append(synchosts, dto.HostSync{HostName: node.Name, HostStatus: constant.StatusRunning})
node := model.ClusterNode{
Name: node.Name,
HostID: host.ID,
ClusterID: cluster.ID,
Role: node.Role,
Status: constant.StatusRunning,
}
if err := tx.Create(&node).Error; err != nil {
c.handlerImportError(tx, cluster, err)
return err
}
clusterResource := model.ClusterResource{
ResourceType: constant.ResourceHost,
ResourceID: host.ID,
ClusterID: cluster.ID,
}
if err := tx.Create(&clusterResource).Error; err != nil {
c.handlerImportError(tx, cluster, err)
return err
}
projectResource := model.ProjectResource{
ResourceType: constant.ResourceHost,
ResourceID: host.ID,
ProjectID: project.ID,
}
if err := tx.Create(&projectResource).Error; err != nil {
c.handlerImportError(tx, cluster, err)
return err
}
}
} else {
if err := gatherClusterInfo(cluster); err != nil {
c.handlerImportError(tx, cluster, err)
return err
}
for _, node := range cluster.Nodes {
node.ClusterID = cluster.ID
if err := tx.Create(&node).Error; err != nil {
c.handlerImportError(tx, cluster, err)
return fmt.Errorf("can not save node %s", err.Error())
}
}
}
cluster.SpecConf.ClusterID = cluster.ID
if err := tx.Create(&cluster.SpecConf).Error; err != nil {
tx.Rollback()
return err
}
cluster.SpecRuntime.ClusterID = cluster.ID
if err := tx.Create(&cluster.SpecRuntime).Error; err != nil {
tx.Rollback()
return err
}
cluster.SpecNetwork.ClusterID = cluster.ID
if err := tx.Create(&cluster.SpecNetwork).Error; err != nil {
tx.Rollback()
return err
}
if err := tx.Save(&cluster).Error; err != nil {
tx.Rollback()
return fmt.Errorf("can not save cluster version %s", err.Error())
}
var (
manifest model.ClusterManifest
toolVars []model.VersionHelp
)
if err := tx.Where("version = ? OR name = ?", cluster.Version, cluster.Version).Order("created_at ASC").First(&manifest).Error; err != nil {
logger.Log.Infof("can not find manifest version: %s", err.Error())
}
if manifest.ID != "" {
if err := json.Unmarshal([]byte(manifest.ToolVars), &toolVars); err != nil {
c.handlerImportError(tx, cluster, err)
return fmt.Errorf("unmarshal manifest.toolvar error %s", err.Error())
}
for i := 0; i < len(tools); i++ {
for _, item := range toolVars {
if tools[i].Name == item.Name {
tools[i].Version = item.Version
break
}
}
}
}
for _, tool := range tools {
tool.ClusterID = cluster.ID
if err := tx.Create(&tool).Error; err != nil {
c.handlerImportError(tx, cluster, err)
return fmt.Errorf("can not save tool %s", err.Error())
}
}
subscribe := model.NewMsgSubscribe(constant.ClusterOperator, constant.Cluster, cluster.ID)
if err := tx.Create(&subscribe).Error; err != nil {
c.handlerImportError(tx, cluster, err)
}
var user model.User
_ = db.DB.Model(&model.User{}).Where("name = 'admin'").First(&user).Error
if user.ID != "" {
subscribeUser := model.MsgSubscribeUser{
SubscribeID: subscribe.ID,
UserID: user.ID,
}
if err := tx.Create(&subscribeUser).Error; err != nil {
c.handlerImportError(tx, cluster, err)
}
}
if err := c.projectResourceRepository.Create(model.ProjectResource{
ResourceID: cluster.ID,
ProjectID: project.ID,
ResourceType: constant.ResourceCluster,
}); err != nil {
c.handlerImportError(tx, cluster, err)
return fmt.Errorf("can not create project resource %s", err.Error())
}
tx.Commit()
_ = c.msgService.SendMsg(constant.ClusterImport, constant.System, cluster, true, map[string]string{"detailName": cluster.Name})
hostService := NewHostService()
go func() {
_ = hostService.SyncList(synchosts)
}()
go func() {
syncList := []string{"metrics-server", "traefik", "ingress-nginx", "gpu", "dns-cache", "istio", "npd"}
syncDTO := &dto.ComponentSync{ClusterName: cluster.Name, Names: syncList}
_ = c.componentService.Sync(syncDTO)
}()
return nil
}
func (c clusterImportService) handlerImportError(tx *gorm.DB, cluster *model.Cluster, err error) {
tx.Rollback()
_ = c.msgService.SendMsg(constant.ClusterImport, constant.Cluster, cluster, false, map[string]string{"errMsg": err.Error(), "detailName": cluster.Name})
}
func gatherClusterInfo(cluster *model.Cluster) error {
c, err := clusterUtil.NewClusterClient(cluster)
if err != nil {
return err
}
cluster.Version, err = getServerVersion(c, false)
if err != nil {
return err
}
var nodesFromK8s []dto.NodesFromK8s
nodesFromK8s, cluster.SpecRuntime.RuntimeType, _, err = getKubeNodes(false, c)
if err != nil {
return err
}
for _, n := range nodesFromK8s {
cluster.Nodes = append(cluster.Nodes, model.ClusterNode{
Name: n.Name,
Role: n.Role,
Status: constant.StatusRunning,
})
}
cluster.SpecNetwork.NetworkType, err = getInfoFromDaemonset(c)
if err != nil {
return err
}
return nil
}
func getServerVersion(client *kubernetes.Clientset, isKoCluster bool) (string, error) {
v, err := client.ServerVersion()
if err != nil {
return "", fmt.Errorf("get version from cluster failed: %v", err.Error())
}
if !isKoCluster {
return v.GitVersion, nil
}
var manifest model.ClusterManifest
if err := db.DB.Where("version = ?", v.GitVersion).Order("created_at ASC").First(&manifest).Error; err != nil {
return "", fmt.Errorf("get manifest %s from db failed: %v", v.GitVersion, err.Error())
}
return manifest.Name, nil
}
func getKubeNodes(isKoImport bool, client *kubernetes.Clientset) ([]dto.NodesFromK8s, string, string, error) {
var (
k8sNodes []dto.NodesFromK8s
runtimeType string
clusterName string
)
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return k8sNodes, runtimeType, clusterName, fmt.Errorf("get nodes from cluster failed: %v", err)
}
for i, node := range nodes.Items {
if i == 0 {
if strings.Contains(node.Status.NodeInfo.ContainerRuntimeVersion, "docker") {
runtimeType = "docker"
} else {
runtimeType = "containerd"
}
}
if isKoImport {
if i == 0 {
if strings.Contains(node.ObjectMeta.Name, "-master") {
clusterName = strings.Split(node.ObjectMeta.Name, "-master")[0]
} else if strings.Contains(node.ObjectMeta.Name, "-worker") {
clusterName = strings.Split(node.ObjectMeta.Name, "-worker")[0]
}
} else {
if strings.Contains(node.ObjectMeta.Name, "-master") {
if clusterName != strings.Split(node.ObjectMeta.Name, "-master")[0] {
clusterName = ""
}
} else if strings.Contains(node.ObjectMeta.Name, "-worker") {
if clusterName != strings.Split(node.ObjectMeta.Name, "-worker")[0] {
clusterName = ""
}
} else {
clusterName = ""
}
}
}
var item dto.NodesFromK8s
item.Name = node.ObjectMeta.Name
if _, ok := node.ObjectMeta.Labels["node-role.kubernetes.io/master"]; ok {
item.Role = "master"
} else {
item.Role = "worker"
}
item.Architecture = node.Status.NodeInfo.Architecture
item.Port = 22
for _, addr := range node.Status.Addresses {
if addr.Type == "InternalIP" {
item.Ip = addr.Address
}
}
k8sNodes = append(k8sNodes, item)
}
return k8sNodes, runtimeType, clusterName, nil
}
func getInfoFromDaemonset(client *kubernetes.Clientset) (string, error) {
var networkType string
daemonsets, err := client.AppsV1().DaemonSets("kube-system").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return networkType, fmt.Errorf("get daemonsets from cluster failed: %v", err.Error())
}
for _, daemonset := range daemonsets.Items {
if strings.Contains(daemonset.ObjectMeta.Name, "calico-node") {
networkType = "calico"
}
if strings.Contains(daemonset.ObjectMeta.Name, "kube-flannel-ds") {
networkType = "flannel"
}
if strings.Contains(daemonset.ObjectMeta.Name, "cilium") {
networkType = "cilium"
}
}
return networkType, nil
}
func (c clusterImportService) LoadClusterInfo(loadInfo *dto.ClusterLoad) (dto.ClusterLoadInfo, error) {
var clusterInfo dto.ClusterLoadInfo
if loadInfo.AuthenticationMode == constant.AuthenticationModeConfigFile {
server, err := dto.LoadApiserverFromConfig(loadInfo.ConfigContent)
if err != nil {
return clusterInfo, fmt.Errorf("load failed. Check the imported Config file again, err: %v", err)
}
loadInfo.ApiServer = server
}
if strings.HasSuffix(loadInfo.ApiServer, "/") {
loadInfo.ApiServer = strings.Replace(loadInfo.ApiServer, "/", "", -1)
}
loadInfo.ApiServer = strings.Replace(loadInfo.ApiServer, "http://", "", -1)
loadInfo.ApiServer = strings.Replace(loadInfo.ApiServer, "https://", "", -1)
if !strings.Contains(loadInfo.ApiServer, ":") {
return clusterInfo, fmt.Errorf("check whether apiserver(%s) has no ports", loadInfo.ApiServer)
}
clusterInfo.LbKubeApiserverIp = strings.Split(loadInfo.ApiServer, ":")[0]
clusterInfo.Architectures = loadInfo.Architectures
var connConf rest.Config
connConf.Insecure = true
switch loadInfo.AuthenticationMode {
case constant.AuthenticationModeBearer:
connConf.Host = loadInfo.ApiServer
connConf.BearerToken = loadInfo.Token
case constant.AuthenticationModeCertificate:
connConf.CertData = []byte(loadInfo.CertDataStr)
connConf.KeyData = []byte(loadInfo.KeyDataStr)
case constant.AuthenticationModeConfigFile:
apiConfig, err := clusterUtil.PauseConfigApi(&loadInfo.ConfigContent)
if err != nil {
return clusterInfo, err
}
getter := func() (*api.Config, error) {
return apiConfig, nil
}
itemConfig, err := clientcmd.BuildConfigFromKubeconfigGetter("", getter)
if err != nil {
return clusterInfo, err
}
connConf = *itemConfig
}
kubeClient, err := kubernetes.NewForConfig(&connConf)
if err != nil {
return clusterInfo, err
}
// load version
clusterInfo.Version, err = getServerVersion(kubeClient, true)
if err != nil {
return clusterInfo, err
}
//load nodes
clusterInfo.Nodes, clusterInfo.RuntimeType, clusterInfo.Name, err = getKubeNodes(true, kubeClient)
if err != nil {
return clusterInfo, err
}
if clusterInfo.Name == "" {
clusterInfo.Name = loadInfo.Name
}
// load kubeadm-config
kubeAdmMap, err := kubeClient.CoreV1().ConfigMaps("kube-system").Get(context.TODO(), "kubeadm-config", metav1.GetOptions{})
if err != nil {
return clusterInfo, fmt.Errorf("can not load kubeadm-config from cluster: %s", err.Error())
}
admConfig := kubeAdmMap.Data["ClusterConfiguration"]
admCfy := make(map[interface{}]interface{})
if err := yaml.Unmarshal([]byte(admConfig), &admCfy); err != nil {
return clusterInfo, fmt.Errorf("kubeadm-config yaml unmarshall failed: %s", err.Error())
}
admInterface := dyno.ConvertMapI2MapS(admCfy)
kk, err := json.Marshal(admInterface)
if err != nil {
return clusterInfo, fmt.Errorf("kubeadm-config json marshall failed: %s", err.Error())
}
var data admConfigStruct
if err := json.Unmarshal(kk, &data); err != nil {
return clusterInfo, fmt.Errorf("kubeadm-config json unmarshall failed: %s", err.Error())
}
if strings.Contains(data.ControlPlaneEndpoint, ":") {
apiServerInCM := strings.Split(data.ControlPlaneEndpoint, ":")[0]
if apiServerInCM == "127.0.0.1" {
clusterInfo.LbKubeApiserverIp = strings.Split(loadInfo.ApiServer, ":")[0]
clusterInfo.LbMode = constant.ClusterSourceInternal
} else {
clusterInfo.LbKubeApiserverIp = apiServerInCM
clusterInfo.LbMode = constant.ClusterSourceExternal
}
clusterInfo.KubeApiServerPort, _ = strconv.Atoi(strings.Split(data.ControlPlaneEndpoint, ":")[1])
} else {
return clusterInfo, fmt.Errorf("err controlPlaneEndpoint from cluster configmap")
}
clusterInfo.KubeServiceNodePortRange = data.ApiServer.ExtraArgs.ServiceNodePortRange
mask, _ := strconv.Atoi(data.Controller.ExtraArgs.NodeCidrMaskSize)
clusterInfo.KubeNetworkNodePrefix = mask
clusterInfo.KubeMaxPods = clusterUtil.MaxNodePodNumMap[mask]
clusterInfo.KubePodSubnet = data.Network.PodSubnet
clusterInfo.KubeDnsDomain = data.Network.DnsDomain
clusterInfo.MaxNodePodNum = 2 << (31 - mask)
if strings.Contains(clusterInfo.KubePodSubnet, "/") {
subnets := strings.Split(clusterInfo.KubePodSubnet, "/")
podMask, _ := strconv.Atoi(subnets[1])
clusterInfo.MaxNodeNum = (2 << (31 - podMask)) / clusterInfo.MaxNodePodNum
}
clusterInfo.KubeServiceSubnet = data.Network.ServiceSubnet
if len(data.ApiServer.ExtraArgs.AuditLogPath) == 0 {
clusterInfo.KubernetesAudit = "no"
} else {
clusterInfo.KubernetesAudit = "yes"
}
// load kube-proxy
kubeProxyMap, err := kubeClient.CoreV1().ConfigMaps("kube-system").Get(context.TODO(), "kube-proxy", metav1.GetOptions{})
if err != nil {
return clusterInfo, fmt.Errorf("can not load kube-proxy from cluster: %s", err.Error())
}
proxyConfig := kubeProxyMap.Data["config.conf"]
proxyCfy := make(map[interface{}]interface{})
if err := yaml.Unmarshal([]byte(proxyConfig), &proxyCfy); err != nil {
return clusterInfo, fmt.Errorf("kube-proxy yaml unmarshall failed: %s", err.Error())
}
proxyInterface := dyno.ConvertMapI2MapS(proxyCfy)
kk2, err := json.Marshal(proxyInterface)
if err != nil {
return clusterInfo, fmt.Errorf("kube-proxy json marshall failed: %s", err.Error())
}
var data2 proxyConfigStruct
if err := json.Unmarshal(kk2, &data2); err != nil {
return clusterInfo, fmt.Errorf("kube-proxy json unmarshall failed: %s", err.Error())
}
clusterInfo.KubeProxyMode = data2.Mode
if len(data2.NodePortAddresses) != 0 {
clusterInfo.NodeportAddress = data2.NodePortAddresses
}
// load network
clusterInfo.NetworkType, err = getInfoFromDaemonset(kubeClient)
if err != nil {
return clusterInfo, err
}
return clusterInfo, nil
}
type admConfigStruct struct {
ApiServer apiServerStruct `json:"apiServer"`
ControlPlaneEndpoint string `json:"controlPlaneEndpoint"`
Controller ControllerStruct `json:"controllerManager"`
Network networkStruct `json:"networking"`
}
type proxyConfigStruct struct {
Mode string `json:"mode"`
NodePortAddresses string `json:"nodePortAddresses"`
}
type apiServerStruct struct {
ExtraArgs extraArgsStruct `json:"extraArgs"`
}
type ControllerStruct struct {
ExtraArgs extraArgsStruct `json:"extraArgs"`
}
type networkStruct struct {
DnsDomain string `json:"dnsDomain"`
PodSubnet string `json:"podSubnet"`
ServiceSubnet string `json:"serviceSubnet"`
}
type extraArgsStruct struct {
ServiceNodePortRange string `json:"service-node-port-range"`
NodeCidrMaskSize string `json:"node-cidr-mask-size"`
AuditLogPath string `json:"audit-log-path"`
}