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_health.go
67 lines (62 loc) · 1.81 KB
/
cluster_health.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
package job
import (
"sync"
"github.com/KubeOperator/KubeOperator/pkg/constant"
"github.com/KubeOperator/KubeOperator/pkg/db"
"github.com/KubeOperator/KubeOperator/pkg/logger"
"github.com/KubeOperator/KubeOperator/pkg/service"
kubeUtil "github.com/KubeOperator/KubeOperator/pkg/util/cluster"
)
type ClusterHealthCheck struct {
clusterService service.ClusterService
}
func NewClusterHealthCheck() *ClusterHealthCheck {
return &ClusterHealthCheck{
clusterService: service.NewClusterService(),
}
}
func (c *ClusterHealthCheck) Run() {
cs, err := c.clusterService.List()
if err != nil {
logger.Log.Errorf("list clusters error %s", err.Error())
return
}
var wg sync.WaitGroup
sem := make(chan struct{}, 5) // 信号量
for i := range cs {
if cs[i].Status != constant.StatusRunning && cs[i].Status != constant.StatusLost {
continue
}
wg.Add(1)
i := i
go func() {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
logger.Log.Infof("test cluster %s api ", cs[i].Name)
client, err := kubeUtil.NewClusterClient(&cs[i].Cluster)
if err != nil {
logger.Log.Errorf("get cluster %s api client error %+v", cs[i].Name, err)
return
}
_, err = client.ServerVersion()
if err != nil {
logger.Log.Errorf("ping cluster %s api error %s", cs[i].Name, err.Error())
cs[i].Cluster.Status = constant.StatusLost
if err := db.DB.Save(&cs[i].Cluster.Status).Error; err != nil {
logger.Log.Errorf("save cluster %s status error %s", cs[i].Name, err.Error())
return
}
return
}
if cs[i].Cluster.Status == constant.StatusLost {
cs[i].Cluster.Status = constant.StatusRunning
if err := db.DB.Save(&cs[i].Cluster.Status).Error; err != nil {
logger.Log.Errorf("save cluster %s status error %s", cs[i].Name, err.Error())
return
}
}
}()
}
wg.Wait()
}