-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathload_state.go
More file actions
294 lines (254 loc) · 7.09 KB
/
load_state.go
File metadata and controls
294 lines (254 loc) · 7.09 KB
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
package controller
import (
"context"
"reflect"
emperror "emperror.dev/errors"
crd "github.com/emqx/emqx-operator/api/v3beta1"
util "github.com/emqx/emqx-operator/internal/controller/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
roleCore = "core"
roleReplicant = "replicant"
)
type reconcileState struct {
coreSets []*appsv1.StatefulSet
replicantSets []*appsv1.ReplicaSet
pods []*corev1.Pod
}
type reconcileStatePodFilter interface {
passes(pod *corev1.Pod) bool
}
type podsManagedBy struct {
manager metav1.Object
}
func (self podsManagedBy) passes(pod *corev1.Pod) bool {
if self.manager == nil && reflect.ValueOf(self.manager).IsNil() {
return false
}
return util.IsPodManagedBy(pod, self.manager)
}
type podsWithRole struct {
string
}
func (self podsWithRole) passes(pod *corev1.Pod) bool {
return pod.Labels[crd.LabelDBRole] == self.string
}
type podsAlive struct{}
func (self podsAlive) passes(pod *corev1.Pod) bool {
return pod.DeletionTimestamp == nil
}
func (r *reconcileState) podWithName(name string) *corev1.Pod {
for _, pod := range r.pods {
if pod.Name == name {
return pod
}
}
return nil
}
func (r *reconcileState) podsManagedBy(object metav1.Object) []*corev1.Pod {
return r.listPods(podsManagedBy{object})
}
func (r *reconcileState) listPods(filters ...reconcileStatePodFilter) []*corev1.Pod {
list := []*corev1.Pod{}
for _, pod := range r.pods {
passes := true
for _, f := range filters {
passes = passes && f.passes(pod)
}
if passes {
list = append(list, pod)
}
}
return list
}
// coreSet returns the single core StatefulSet, or nil if none exists.
// With the rolling update model, there is always at most one core StatefulSet.
func (r *reconcileState) coreSet() *appsv1.StatefulSet {
if len(r.coreSets) > 0 {
return r.coreSets[0]
}
return nil
}
func (r *reconcileState) partOfCoreSet(pod *corev1.Pod) bool {
coreSet := r.coreSet()
if coreSet == nil {
return false
}
return util.IsPodManagedBy(pod, coreSet)
}
// partOfCoreSetRevision checks if a core pod's StatefulSet-assigned revision matches the specified revision.
func (r *reconcileState) partOfCoreSetRevision(pod *corev1.Pod, revision string) bool {
coreSet := r.coreSet()
if coreSet == nil {
return false
}
if !util.IsPodManagedBy(pod, coreSet) {
return false
}
podRevision := pod.Labels[appsv1.ControllerRevisionHashLabelKey]
return podRevision == revision
}
// numCoresRevision counts number of core pods running specified StatefulSet revision.
func (r *reconcileState) numCoresRevision(revision string) int {
coreSet := r.coreSet()
if coreSet == nil {
return 0
}
num := 0
for _, pod := range r.podsManagedBy(coreSet) {
podRevision := pod.Labels[appsv1.ControllerRevisionHashLabelKey]
if podRevision == revision {
num++
}
}
return num
}
// Returns ReplicaSet representing current set of replicant nodes.
// Current set is considered outdated if CurrentRevision != UpdateRevision.
func (r *reconcileState) currentReplicantSet(instance *crd.EMQX) *appsv1.ReplicaSet {
for _, rs := range r.replicantSets {
hash := rs.Labels[crd.LabelPodTemplateHash]
if hash == instance.Status.ReplicantNodesStatus.CurrentRevision {
return rs
}
}
return nil
}
// Returns ReplicaSet representing newest set of replicant nodes.
// Same as current if CurrentRevision == UpdateRevision.
func (r *reconcileState) updateReplicantSet(instance *crd.EMQX) *appsv1.ReplicaSet {
for _, rs := range r.replicantSets {
hash := rs.Labels[crd.LabelPodTemplateHash]
if hash == instance.Status.ReplicantNodesStatus.UpdateRevision {
return rs
}
}
return nil
}
// outdatedReplicantReplicaSets returns all replicant ReplicaSets except the update revision set,
// sorted by creation timestamp (oldest first).
func (r *reconcileState) outdatedReplicantSets(instance *crd.EMQX) []*appsv1.ReplicaSet {
updateRs := r.updateReplicantSet(instance)
if updateRs == nil {
return nil
}
out := []*appsv1.ReplicaSet{}
for _, rs := range r.replicantSets {
if rs.UID == updateRs.UID {
continue
}
out = append(out, rs)
}
sortByCreationTimestamp(out)
return out
}
// outdatedReplicantPodsSorted lists pods owned by any outdated replicant ReplicaSet (all except the
// update revision), de-duplicated by name and sorted by name for deterministic drain order.
func (r *reconcileState) outdatedReplicantPods(instance *crd.EMQX) []*corev1.Pod {
out := []*corev1.Pod{}
outdatedRs := r.outdatedReplicantSets(instance)
if outdatedRs == nil {
return nil
}
for _, rs := range outdatedRs {
outdatedPods := r.podsManagedBy(rs)
sortByName(outdatedPods)
out = append(out, outdatedPods...)
}
return out
}
func (r *reconcileState) numReplicants() int32 {
out := int32(0)
for _, rs := range r.replicantSets {
out += rs.Status.Replicas
}
return out
}
func (r *reconcileState) numReadyReplicants() int32 {
out := int32(0)
for _, rs := range r.replicantSets {
out += rs.Status.ReadyReplicas
}
return out
}
func (r *reconcileState) numAvailableReplicants() int32 {
out := int32(0)
for _, rs := range r.replicantSets {
out += rs.Status.AvailableReplicas
}
return out
}
type loadState struct {
*EMQXReconciler
}
func (l *loadState) reconcile(r *reconcileRound, instance *crd.EMQX) subResult {
state, err := loadReconcileState(r.ctx, l.Client, instance)
if err != nil {
return subResult{err: emperror.Wrap(err, "failed to load reconcile round state")}
}
r.state = state
return subResult{}
}
func reloadReconcileState(r *reconcileRound, client k8s.Client, instance *crd.EMQX) error {
state, err := loadReconcileState(r.ctx, client, instance)
if err != nil {
return err
}
r.state = state
return nil
}
func loadReconcileState(
ctx context.Context,
client k8s.Client,
instance *crd.EMQX,
) (*reconcileState, error) {
var err error
state := &reconcileState{}
stsList := &appsv1.StatefulSetList{}
err = client.List(ctx, stsList,
k8s.InNamespace(instance.Namespace),
k8s.MatchingLabels(instance.DefaultLabelsWith(crd.CoreLabels())),
)
if err != nil {
return nil, err
}
for _, sts := range stsList.Items {
state.coreSets = append(state.coreSets, sts.DeepCopy())
}
sortByCreationTimestamp(state.coreSets)
rsList := &appsv1.ReplicaSetList{}
err = client.List(ctx, rsList,
k8s.InNamespace(instance.Namespace),
k8s.MatchingLabels(instance.DefaultLabelsWith(crd.ReplicantLabels())),
)
if err != nil {
return nil, err
}
for _, rs := range rsList.Items {
state.replicantSets = append(state.replicantSets, rs.DeepCopy())
}
sortByCreationTimestamp(state.replicantSets)
podList := &corev1.PodList{}
err = client.List(ctx, podList,
k8s.InNamespace(instance.Namespace),
k8s.MatchingLabels(instance.DefaultLabels()),
)
if err != nil {
return nil, err
}
for _, pod := range podList.Items {
// Disregard pods that are not controlled by any controller.
controllerRef := metav1.GetControllerOf(&pod)
if controllerRef == nil {
continue
}
// Add the pod to the list of pods.
pod := pod.DeepCopy()
state.pods = append(state.pods, pod)
}
return state, nil
}