-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_status_members.go
307 lines (286 loc) · 10.1 KB
/
deployment_status_members.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
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
//
// 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
//
// http://www.apache.org/licenses/LICENSE-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 OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package v1
import (
"github.com/dalet-oss/arangodb-operator/pkg/util/errors"
)
// MemberStatusFunc is a callback which is used to traverse a specific group of servers and check their status.
type MemberStatusFunc func(group ServerGroup, list MemberStatusList) error
// DeploymentStatusMembers holds the member status of all server groups
type DeploymentStatusMembers struct {
Single MemberStatusList `json:"single,omitempty"`
Agents MemberStatusList `json:"agents,omitempty"`
DBServers MemberStatusList `json:"dbservers,omitempty"`
Coordinators MemberStatusList `json:"coordinators,omitempty"`
SyncMasters MemberStatusList `json:"syncmasters,omitempty"`
SyncWorkers MemberStatusList `json:"syncworkers,omitempty"`
}
// Equal checks for equality
func (ds DeploymentStatusMembers) Equal(other DeploymentStatusMembers) bool {
return ds.Single.Equal(other.Single) &&
ds.Agents.Equal(other.Agents) &&
ds.DBServers.Equal(other.DBServers) &&
ds.Coordinators.Equal(other.Coordinators) &&
ds.SyncMasters.Equal(other.SyncMasters) &&
ds.SyncWorkers.Equal(other.SyncWorkers)
}
// ContainsID returns true if the given set of members contains a member with given ID.
func (ds DeploymentStatusMembers) ContainsID(id string) bool {
return ds.Single.ContainsID(id) ||
ds.Agents.ContainsID(id) ||
ds.DBServers.ContainsID(id) ||
ds.Coordinators.ContainsID(id) ||
ds.SyncMasters.ContainsID(id) ||
ds.SyncWorkers.ContainsID(id)
}
// ElementByID returns the element in the given list that has the given ID and true.
// If no such element exists, false is returned.
func (ds DeploymentStatusMembers) ElementByID(id string) (MemberStatus, ServerGroup, bool) {
if result, found := ds.Single.ElementByID(id); found {
return result, ServerGroupSingle, true
}
if result, found := ds.Agents.ElementByID(id); found {
return result, ServerGroupAgents, true
}
if result, found := ds.DBServers.ElementByID(id); found {
return result, ServerGroupDBServers, true
}
if result, found := ds.Coordinators.ElementByID(id); found {
return result, ServerGroupCoordinators, true
}
if result, found := ds.SyncMasters.ElementByID(id); found {
return result, ServerGroupSyncMasters, true
}
if result, found := ds.SyncWorkers.ElementByID(id); found {
return result, ServerGroupSyncWorkers, true
}
return MemberStatus{}, 0, false
}
// ForeachServerGroup calls the given callback for all server groups.
// If the callback returns an error, this error is returned and the callback is
// not called for the remaining groups.
// Deprecated. Use AsList instead
func (ds DeploymentStatusMembers) ForeachServerGroup(cb MemberStatusFunc) error {
return ds.ForeachServerInGroups(cb, AllServerGroups...)
}
// ForeachServerInGroups calls the given callback for specified server groups.
// Deprecated. Use AsListInGroups instead
func (ds DeploymentStatusMembers) ForeachServerInGroups(cb MemberStatusFunc, groups ...ServerGroup) error {
for _, group := range groups {
if err := ds.ForServerGroup(cb, group); err != nil {
return err
}
}
return nil
}
// ForServerGroup calls the given callback for specified server group.
// Deprecated. Use AsListInGroup or MembersOfGroup
func (ds DeploymentStatusMembers) ForServerGroup(cb MemberStatusFunc, group ServerGroup) error {
switch group {
case ServerGroupSingle:
if err := cb(ServerGroupSingle, ds.Single); err != nil {
return errors.WithStack(err)
}
case ServerGroupAgents:
if err := cb(ServerGroupAgents, ds.Agents); err != nil {
return errors.WithStack(err)
}
case ServerGroupDBServers:
if err := cb(ServerGroupDBServers, ds.DBServers); err != nil {
return errors.WithStack(err)
}
case ServerGroupCoordinators:
if err := cb(ServerGroupCoordinators, ds.Coordinators); err != nil {
return errors.WithStack(err)
}
case ServerGroupSyncMasters:
if err := cb(ServerGroupSyncMasters, ds.SyncMasters); err != nil {
return errors.WithStack(err)
}
case ServerGroupSyncWorkers:
if err := cb(ServerGroupSyncWorkers, ds.SyncWorkers); err != nil {
return errors.WithStack(err)
}
}
return nil
}
// MemberStatusByPodName returns a reference to the element in the given set of lists that has the given pod name.
// Returns member status and group which the pod belong to.
// If no such element exists, false is returned.
func (ds DeploymentStatusMembers) MemberStatusByPodName(podName string) (MemberStatus, ServerGroup, bool) {
if result, found := ds.Single.ElementByPodName(podName); found {
return result, ServerGroupSingle, true
}
if result, found := ds.Agents.ElementByPodName(podName); found {
return result, ServerGroupAgents, true
}
if result, found := ds.DBServers.ElementByPodName(podName); found {
return result, ServerGroupDBServers, true
}
if result, found := ds.Coordinators.ElementByPodName(podName); found {
return result, ServerGroupCoordinators, true
}
if result, found := ds.SyncMasters.ElementByPodName(podName); found {
return result, ServerGroupSyncMasters, true
}
if result, found := ds.SyncWorkers.ElementByPodName(podName); found {
return result, ServerGroupSyncWorkers, true
}
return MemberStatus{}, 0, false
}
// MemberStatusByPVCName returns a reference to the element in the given set of lists that has the given PVC name.
// If no such element exists, nil is returned.
func (ds DeploymentStatusMembers) MemberStatusByPVCName(pvcName string) (MemberStatus, ServerGroup, bool) {
if result, found := ds.Single.ElementByPVCName(pvcName); found {
return result, ServerGroupSingle, true
}
if result, found := ds.Agents.ElementByPVCName(pvcName); found {
return result, ServerGroupAgents, true
}
if result, found := ds.DBServers.ElementByPVCName(pvcName); found {
return result, ServerGroupDBServers, true
}
// Note: Other server groups do not have PVC's so we can skip them.
return MemberStatus{}, 0, false
}
// Add adds the given status in the given group.
func (ds *DeploymentStatusMembers) Add(status MemberStatus, group ServerGroup) error {
var err error
switch group {
case ServerGroupSingle:
err = ds.Single.add(status)
case ServerGroupAgents:
err = ds.Agents.add(status)
case ServerGroupDBServers:
err = ds.DBServers.add(status)
case ServerGroupCoordinators:
err = ds.Coordinators.add(status)
case ServerGroupSyncMasters:
err = ds.SyncMasters.add(status)
case ServerGroupSyncWorkers:
err = ds.SyncWorkers.add(status)
default:
return errors.WithStack(errors.Wrapf(NotFoundError, "ServerGroup %d is not known", group))
}
if err != nil {
return errors.WithStack(err)
}
return nil
}
// Update updates the given status in the given group.
func (ds *DeploymentStatusMembers) Update(status MemberStatus, group ServerGroup) error {
var err error
switch group {
case ServerGroupSingle:
err = ds.Single.update(status)
case ServerGroupAgents:
err = ds.Agents.update(status)
case ServerGroupDBServers:
err = ds.DBServers.update(status)
case ServerGroupCoordinators:
err = ds.Coordinators.update(status)
case ServerGroupSyncMasters:
err = ds.SyncMasters.update(status)
case ServerGroupSyncWorkers:
err = ds.SyncWorkers.update(status)
default:
return errors.WithStack(errors.Wrapf(NotFoundError, "ServerGroup %d is not known", group))
}
if err != nil {
return errors.WithStack(err)
}
return nil
}
// RemoveByID a member with given ID from the given group.
// Returns a NotFoundError if the ID of the given member or group cannot be found.
func (ds *DeploymentStatusMembers) RemoveByID(id string, group ServerGroup) error {
var err error
switch group {
case ServerGroupSingle:
err = ds.Single.removeByID(id)
case ServerGroupAgents:
err = ds.Agents.removeByID(id)
case ServerGroupDBServers:
err = ds.DBServers.removeByID(id)
case ServerGroupCoordinators:
err = ds.Coordinators.removeByID(id)
case ServerGroupSyncMasters:
err = ds.SyncMasters.removeByID(id)
case ServerGroupSyncWorkers:
err = ds.SyncWorkers.removeByID(id)
default:
return errors.WithStack(errors.Wrapf(NotFoundError, "ServerGroup %d is not known", group))
}
if err != nil {
return errors.WithStack(err)
}
return nil
}
// AllMembersReady returns true when all members, that must be ready for the given mode, are in the Ready state.
func (ds DeploymentStatusMembers) AllMembersReady(mode DeploymentMode, syncEnabled bool) bool {
syncReady := func() bool {
if syncEnabled {
return ds.SyncMasters.AllMembersReady() && ds.SyncWorkers.AllMembersReady()
}
return true
}
switch mode {
case DeploymentModeSingle:
return ds.Single.MembersReady() > 0
case DeploymentModeActiveFailover:
return ds.Agents.AllMembersReady() && ds.Single.MembersReady() > 0
case DeploymentModeCluster:
return ds.Agents.AllMembersReady() &&
ds.DBServers.AllMembersReady() &&
ds.Coordinators.AllMembersReady() &&
syncReady()
default:
return false
}
}
// MembersOfGroup returns the member list of the given group
func (ds DeploymentStatusMembers) MembersOfGroup(group ServerGroup) MemberStatusList {
switch group {
case ServerGroupSingle:
return ds.Single
case ServerGroupAgents:
return ds.Agents
case ServerGroupDBServers:
return ds.DBServers
case ServerGroupCoordinators:
return ds.Coordinators
case ServerGroupSyncMasters:
return ds.SyncMasters
case ServerGroupSyncWorkers:
return ds.SyncWorkers
default:
return MemberStatusList{}
}
}
// PodNames returns all members pod names
func (ds DeploymentStatusMembers) PodNames() []string {
var n []string
for _, m := range ds.AsList() {
if name := m.Member.Pod.GetName(); name != "" {
n = append(n, name)
}
}
return n
}