-
Notifications
You must be signed in to change notification settings - Fork 15
/
get_status.go
92 lines (81 loc) · 1.96 KB
/
get_status.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
package server
import (
"context"
"fmt"
"sync"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/cke/op"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
)
// GetClusterStatus consults the whole cluster and constructs *ClusterStatus.
func (c Controller) GetClusterStatus(ctx context.Context, cluster *cke.Cluster, inf cke.Infrastructure) (*cke.ClusterStatus, error) {
var mu sync.Mutex
statuses := make(map[string]*cke.NodeStatus)
env := well.NewEnvironment(ctx)
for _, n := range cluster.Nodes {
n := n
env.Go(func(ctx context.Context) error {
ns, err := op.GetNodeStatus(ctx, inf, n, cluster)
if err != nil {
return fmt.Errorf("%s: %v", n.Address, err)
}
mu.Lock()
statuses[n.Address] = ns
mu.Unlock()
return nil
})
}
env.Stop()
err := env.Wait()
if err != nil {
return nil, err
}
cs := new(cke.ClusterStatus)
version, err := inf.Storage().GetConfigVersion(ctx)
if err != nil {
return nil, err
}
cs.ConfigVersion = version
cs.NodeStatuses = statuses
var etcdRunning bool
for _, n := range cke.ControlPlanes(cluster.Nodes) {
ns := statuses[n.Address]
if ns.Etcd.IsAddedMember {
etcdRunning = true
break
}
}
if !etcdRunning {
return cs, nil
}
ecs, err := op.GetEtcdClusterStatus(ctx, inf, cluster.Nodes)
if err != nil {
log.Warn("failed to get etcd cluster status", map[string]interface{}{
log.FnError: err,
})
// lint:ignore nilerr Proceed with as much cluster statuses as we can gather.
return cs, nil
}
cs.Etcd = ecs
var livingMaster *cke.Node
for _, n := range cke.ControlPlanes(cluster.Nodes) {
ns := statuses[n.Address]
if ns.APIServer.Running {
livingMaster = n
break
}
}
if livingMaster == nil {
return cs, nil
}
kcs, err := op.GetKubernetesClusterStatus(ctx, inf, livingMaster, cluster)
if err != nil {
log.Error("failed to get kubernetes cluster status", map[string]interface{}{
log.FnError: err,
})
return nil, err
}
cs.Kubernetes = kcs
return cs, nil
}