-
Notifications
You must be signed in to change notification settings - Fork 106
/
helper.go
216 lines (189 loc) · 6.23 KB
/
helper.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2023 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package clusterprovider
import (
"fmt"
"strings"
"k8s.io/klog/v2"
kstonev1alpha2 "tkestack.io/kstone/pkg/apis/kstone/v1alpha2"
"tkestack.io/kstone/pkg/etcd"
versionClient "tkestack.io/kstone/pkg/etcd/client"
_ "tkestack.io/kstone/pkg/etcd/client/versions" // import etcd client including v2 and v3
)
type EtcdAlarm struct {
MemberID uint64
AlarmType string
}
// GetStorageMemberEndpoints get member of cluster status
func GetStorageMemberEndpoints(cluster *kstonev1alpha2.EtcdCluster) []string {
members := cluster.Status.Members
endpoints := make([]string, 0)
if len(members) == 0 {
return endpoints
}
for _, m := range members {
endpoints = append(endpoints, m.ExtensionClientUrl)
}
return endpoints
}
// populateExtensionCientURLMap generate extensionClientURLs map
func populateExtensionCientURLMap(extensionClientURLs string) (map[string]string, error) {
urlMap := make(map[string]string)
if extensionClientURLs == "" {
return nil, nil
}
items := strings.Split(extensionClientURLs, ",")
for i := 0; i < len(items); i++ {
eps := strings.Split(items[i], "->")
if len(eps) == 2 {
urlMap[eps[0]] = eps[1]
} else {
return urlMap, fmt.Errorf("invalid extensionClientURLs %s", items[i])
}
}
return urlMap, nil
}
// GetRuntimeEtcdMembers get members of etcd
func GetRuntimeEtcdMembers(
storageBackend string,
endpoints []string,
extensionClientURLs string,
config *etcd.ClientConfig) ([]kstonev1alpha2.MemberStatus, error) {
etcdMembers := make([]kstonev1alpha2.MemberStatus, 0)
config.Endpoints = endpoints
versioned, err := versionClient.GetEtcdClientProvider(kstonev1alpha2.EtcdStorageBackend(storageBackend),
&versionClient.VersionContext{Config: config})
if err != nil {
klog.Errorf("failed get etcd version, err is %v", err)
return etcdMembers, err
}
defer versioned.Close()
memberRsp, err := versioned.MemberList()
if err != nil {
klog.Errorf("failed to get member list, endpoints is %s,err is %v", endpoints, err)
return etcdMembers, err
}
extensionClientURLMap, err := populateExtensionCientURLMap(extensionClientURLs)
if err != nil {
klog.Errorf("failed to populate extension clientURL,err is %v", err)
return etcdMembers, err
}
for _, m := range memberRsp {
// parse url
if m.ClientURLs == nil {
continue
}
items := strings.Split(m.ClientURLs[0], ":")
endPoint := strings.TrimPrefix(items[1], "//")
extensionClientURL := m.ClientURLs[0]
if extensionClientURLs != "" {
var ep string
if strings.HasPrefix(m.ClientURLs[0], "https://") {
ep = strings.TrimPrefix(m.ClientURLs[0], "https://")
if _, ok := extensionClientURLMap[ep]; ok {
extensionClientURL = "https://" + extensionClientURLMap[ep]
}
} else {
ep = strings.TrimPrefix(m.ClientURLs[0], "http://")
if _, ok := extensionClientURLMap[ep]; ok {
extensionClientURL = "http://" + extensionClientURLMap[ep]
}
}
}
// default info
memberVersion, memberStatus, memberRole := "", kstonev1alpha2.MemberPhaseUnStarted, kstonev1alpha2.EtcdMemberUnKnown
var errors []string
statusRsp, err := versioned.Status(extensionClientURL)
if err == nil && statusRsp != nil {
memberStatus = kstonev1alpha2.MemberPhaseRunning
memberVersion = statusRsp.Version
if statusRsp.IsLearner {
memberRole = kstonev1alpha2.EtcdMemberLearner
} else if statusRsp.Leader == m.ID {
memberRole = kstonev1alpha2.EtcdMemberLeader
} else {
memberRole = kstonev1alpha2.EtcdMemberFollower
}
} else {
klog.Errorf("failed to get member %s status,err is %v", extensionClientURL, err)
errors = append(errors, err.Error())
}
etcdMembers = append(etcdMembers, kstonev1alpha2.MemberStatus{
Name: m.Name,
MemberId: m.ID,
ClientUrl: m.ClientURLs[0],
ExtensionClientUrl: extensionClientURL,
Role: memberRole,
Status: memberStatus,
Endpoint: endPoint,
Port: items[2],
Version: memberVersion,
Errors: errors,
})
}
return etcdMembers, nil
}
// GetEtcdClusterMemberStatus check healthy of cluster and member
func GetEtcdClusterMemberStatus(
members []kstonev1alpha2.MemberStatus,
config *etcd.ClientConfig) ([]kstonev1alpha2.MemberStatus, kstonev1alpha2.EtcdClusterPhase) {
clusterStatus := kstonev1alpha2.EtcdClusterRunning
newMembers := make([]kstonev1alpha2.MemberStatus, 0)
for _, m := range members {
healthy, err := etcd.MemberHealthy(m.ExtensionClientUrl, config)
if err != nil {
m.Status = kstonev1alpha2.MemberPhaseUnKnown
} else {
if healthy {
m.Status = kstonev1alpha2.MemberPhaseRunning
} else {
m.Status = kstonev1alpha2.MemberPhaseUnHealthy
}
}
if m.Status != kstonev1alpha2.MemberPhaseRunning && clusterStatus == kstonev1alpha2.EtcdClusterRunning {
clusterStatus = kstonev1alpha2.EtcdClusterUnhealthy
}
newMembers = append(newMembers, m)
}
return newMembers, clusterStatus
}
// GetEtcdAlarms get alarm list of etcd
func GetEtcdAlarms(
endpoints []string,
config *etcd.ClientConfig) ([]EtcdAlarm, error) {
etcdAlarms := make([]EtcdAlarm, 0)
config.Endpoints = endpoints
client, err := etcd.NewClientv3(config)
if err != nil {
klog.Errorf("failed to get new etcd clientv3, err is %v ", err)
return etcdAlarms, err
}
defer client.Close()
alarmRsp, err := etcd.AlarmList(client)
if err != nil {
klog.Errorf("failed to get alarm list, err is %v", err)
return etcdAlarms, err
}
for _, a := range alarmRsp.Alarms {
etcdAlarms = append(etcdAlarms, EtcdAlarm{
MemberID: a.MemberID,
AlarmType: a.Alarm.String(),
})
}
return etcdAlarms, nil
}