-
Notifications
You must be signed in to change notification settings - Fork 155
/
switchover.go
288 lines (255 loc) · 12.5 KB
/
switchover.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
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package operations
import (
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/controllers/apps/components"
intctrlutil "github.com/apecloud/kubeblocks/internal/controllerutil"
)
type switchoverOpsHandler struct{}
var _ OpsHandler = switchoverOpsHandler{}
// SwitchoverMessage is the OpsRequest.Status.Condition.Message for switchover.
type SwitchoverMessage struct {
appsv1alpha1.Switchover
OldPrimary string
Cluster string
}
func init() {
switchoverBehaviour := OpsBehaviour{
FromClusterPhases: appsv1alpha1.GetClusterUpRunningPhases(),
ToClusterPhase: appsv1alpha1.SpecReconcilingClusterPhase,
OpsHandler: switchoverOpsHandler{},
MaintainClusterPhaseBySelf: true,
ProcessingReasonInClusterCondition: ProcessingReasonSwitchovering,
}
opsMgr := GetOpsManager()
opsMgr.RegisterOps(appsv1alpha1.SwitchoverType, switchoverBehaviour)
}
// ActionStartedCondition the started condition when handle the switchover request.
func (r switchoverOpsHandler) ActionStartedCondition(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (*metav1.Condition, error) {
switchoverMessageMap := make(map[string]SwitchoverMessage)
for _, switchover := range opsRes.OpsRequest.Spec.SwitchoverList {
pod, err := getPrimaryOrLeaderPod(reqCtx.Ctx, cli, *opsRes.Cluster, switchover.ComponentName, opsRes.Cluster.Spec.GetComponentDefRefName(switchover.ComponentName))
if err != nil {
return nil, err
}
switchoverMessageMap[switchover.ComponentName] = SwitchoverMessage{
Switchover: switchover,
OldPrimary: pod.Name,
Cluster: opsRes.Cluster.Name,
}
}
msg, err := json.Marshal(switchoverMessageMap)
if err != nil {
return nil, err
}
return appsv1alpha1.NewSwitchoveringCondition(opsRes.Cluster.Generation, string(msg)), nil
}
// Action to do the switchover operation.
func (r switchoverOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
return doSwitchoverComponents(reqCtx, cli, opsRes, opsRes.OpsRequest.Spec.SwitchoverList)
}
// ReconcileAction will be performed when action is done and loops till OpsRequest.status.phase is Succeed/Failed.
// the Reconcile function for switchover opsRequest.
func (r switchoverOpsHandler) ReconcileAction(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (appsv1alpha1.OpsPhase, time.Duration, error) {
var (
opsRequestPhase = appsv1alpha1.OpsRunningPhase
)
expectCount, actualCount, err := handleSwitchoverProgress(reqCtx, cli, opsRes)
if err != nil {
return "", 0, err
}
if expectCount == actualCount {
opsRequestPhase = appsv1alpha1.OpsSucceedPhase
}
return opsRequestPhase, time.Second, nil
}
// GetRealAffectedComponentMap gets the real affected component map for the operation
func (r switchoverOpsHandler) GetRealAffectedComponentMap(opsRequest *appsv1alpha1.OpsRequest) realAffectedComponentMap {
return realAffectedComponentMap(opsRequest.Spec.GetSwitchoverComponentNameSet())
}
// SaveLastConfiguration this operation only restart the pods of the component, no changes for Cluster.spec.
// empty implementation here.
func (r switchoverOpsHandler) SaveLastConfiguration(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
return nil
}
// doSwitchoverComponents creates the switchover job for each component.
func doSwitchoverComponents(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource, switchoverList []appsv1alpha1.Switchover) error {
var (
opsRequest = opsRes.OpsRequest
oldOpsRequestStatus = opsRequest.Status.DeepCopy()
)
patch := client.MergeFrom(opsRequest.DeepCopy())
if opsRequest.Status.Components == nil {
opsRequest.Status.Components = make(map[string]appsv1alpha1.OpsRequestComponentStatus)
}
for _, switchover := range switchoverList {
compDef, err := components.GetComponentDefByCluster(reqCtx.Ctx, cli, *opsRes.Cluster, switchover.ComponentName)
if err != nil {
return err
}
needSwitchover, err := needDoSwitchover(reqCtx.Ctx, cli, opsRes.Cluster, opsRes.Cluster.Spec.GetComponentByName(switchover.ComponentName), &switchover)
if err != nil {
return err
}
if !needSwitchover {
opsRequest.Status.Components[switchover.ComponentName] = appsv1alpha1.OpsRequestComponentStatus{
Phase: appsv1alpha1.RunningClusterCompPhase,
Reason: OpsReasonForSkipSwitchover,
Message: fmt.Sprintf("This component %s is already in the expected state, skip the switchover operation", switchover.ComponentName),
ProgressDetails: []appsv1alpha1.ProgressStatusDetail{},
}
continue
} else {
opsRequest.Status.Components[switchover.ComponentName] = appsv1alpha1.OpsRequestComponentStatus{
Phase: appsv1alpha1.SpecReconcilingClusterCompPhase,
ProgressDetails: []appsv1alpha1.ProgressStatusDetail{},
}
}
if err := createSwitchoverJob(reqCtx, cli, opsRes.Cluster, opsRes.Cluster.Spec.GetComponentByName(switchover.ComponentName), compDef, &switchover); err != nil {
return err
}
}
if !reflect.DeepEqual(*oldOpsRequestStatus, opsRequest.Status) {
if err := cli.Status().Patch(reqCtx.Ctx, opsRequest, patch); err != nil {
return err
}
}
return nil
}
// handleSwitchoverProgress handles the component progressDetails during switchover.
// Returns:
// - expectCount: the expected count of switchover operations
// - completedCount: the number of completed switchover operations
// - error: any error that occurred during the handling
func handleSwitchoverProgress(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (int32, int32, error) {
var (
expectCount = int32(len(opsRes.OpsRequest.Spec.SwitchoverList))
completedCount int32
opsRequest = opsRes.OpsRequest
oldOpsRequestStatus = opsRequest.Status.DeepCopy()
compDef *appsv1alpha1.ClusterComponentDefinition
consistency bool
err error
)
patch := client.MergeFrom(opsRequest.DeepCopy())
succeedJobs := make([]string, 0, len(opsRes.OpsRequest.Spec.SwitchoverList))
for _, switchover := range opsRequest.Spec.SwitchoverList {
switchoverCondition := meta.FindStatusCondition(opsRes.OpsRequest.Status.Conditions, appsv1alpha1.ConditionTypeSwitchover)
if switchoverCondition == nil {
err = errors.New("switchover condition is nil")
break
}
// if the component do not need switchover, skip it
reason := opsRequest.Status.Components[switchover.ComponentName].Reason
if reason == OpsReasonForSkipSwitchover {
completedCount += 1
continue
}
// check the current component switchoverJob whether succeed
jobName := genSwitchoverJobName(opsRes.Cluster.Name, switchover.ComponentName, switchoverCondition.ObservedGeneration)
checkJobProcessDetail := appsv1alpha1.ProgressStatusDetail{
ObjectKey: getProgressObjectKey(SwitchoverCheckJobKey, jobName),
Status: appsv1alpha1.ProcessingProgressStatus,
}
if err = checkJobSucceed(reqCtx.Ctx, cli, opsRes.Cluster, jobName); err != nil {
checkJobProcessDetail.Message = fmt.Sprintf("switchover job %s is not succeed", jobName)
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.SpecReconcilingClusterCompPhase, checkJobProcessDetail, switchover.ComponentName)
continue
} else {
checkJobProcessDetail.Message = fmt.Sprintf("switchover job %s is succeed", jobName)
checkJobProcessDetail.Status = appsv1alpha1.SucceedProgressStatus
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.SpecReconcilingClusterCompPhase, checkJobProcessDetail, switchover.ComponentName)
}
// check the current component pod role label whether correct
checkRoleLabelProcessDetail := appsv1alpha1.ProgressStatusDetail{
ObjectKey: getProgressObjectKey(SwitchoverCheckRoleLabelKey, switchover.ComponentName),
Status: appsv1alpha1.ProcessingProgressStatus,
Message: fmt.Sprintf("waiting for component %s pod role label consistency after switchover", switchover.ComponentName),
}
compDef, err = components.GetComponentDefByCluster(reqCtx.Ctx, cli, *opsRes.Cluster, switchover.ComponentName)
if err != nil {
checkRoleLabelProcessDetail.Message = fmt.Sprintf("handleSwitchoverProgress get component %s definition failed", switchover.ComponentName)
checkRoleLabelProcessDetail.Status = appsv1alpha1.FailedProgressStatus
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.SpecReconcilingClusterCompPhase, checkRoleLabelProcessDetail, switchover.ComponentName)
continue
}
consistency, err = checkPodRoleLabelConsistency(reqCtx.Ctx, cli, opsRes.Cluster, opsRes.Cluster.Spec.GetComponentByName(switchover.ComponentName), compDef, &switchover, switchoverCondition)
if err != nil {
checkRoleLabelProcessDetail.Message = fmt.Sprintf("waiting for component %s pod role label consistency after switchover", switchover.ComponentName)
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.SpecReconcilingClusterCompPhase, checkRoleLabelProcessDetail, switchover.ComponentName)
continue
}
if !consistency {
err = intctrlutil.NewErrorf(intctrlutil.ErrorWaitCacheRefresh, "requeue to waiting for pod role label consistency.")
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.SpecReconcilingClusterCompPhase, checkRoleLabelProcessDetail, switchover.ComponentName)
continue
} else {
checkRoleLabelProcessDetail.Message = fmt.Sprintf("check component %s pod role label consistency after switchover is succeed", switchover.ComponentName)
checkRoleLabelProcessDetail.Status = appsv1alpha1.SucceedProgressStatus
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.SpecReconcilingClusterCompPhase, checkRoleLabelProcessDetail, switchover.ComponentName)
}
// component switchover is successful
completedCount += 1
succeedJobs = append(succeedJobs, jobName)
componentProcessDetail := appsv1alpha1.ProgressStatusDetail{
ObjectKey: switchover.ComponentName,
Message: fmt.Sprintf("switchover job %s is succeed", jobName),
Status: appsv1alpha1.SucceedProgressStatus,
}
setComponentSwitchoverProgressDetails(reqCtx.Recorder, opsRequest, appsv1alpha1.RunningClusterCompPhase, componentProcessDetail, switchover.ComponentName)
}
opsRequest.Status.Progress = fmt.Sprintf("%d/%d", completedCount, expectCount)
// patch OpsRequest.status.components
if !reflect.DeepEqual(*oldOpsRequestStatus, opsRequest.Status) {
if err := cli.Status().Patch(reqCtx.Ctx, opsRequest, patch); err != nil {
return expectCount, 0, err
}
}
if err != nil {
return expectCount, completedCount, err
}
if completedCount == expectCount {
for _, jobName := range succeedJobs {
if err := cleanJobByName(reqCtx.Ctx, cli, opsRes.Cluster, jobName); err != nil {
reqCtx.Log.Error(err, "clean switchover job failed", "jobName", jobName)
return expectCount, completedCount, err
}
}
}
return expectCount, completedCount, nil
}
// setComponentSwitchoverProgressDetails sets component switchover progress details.
func setComponentSwitchoverProgressDetails(recorder record.EventRecorder,
opsRequest *appsv1alpha1.OpsRequest,
phase appsv1alpha1.ClusterComponentPhase,
processDetail appsv1alpha1.ProgressStatusDetail,
componentName string) {
componentProcessDetails := opsRequest.Status.Components[componentName].ProgressDetails
setComponentStatusProgressDetail(recorder, opsRequest, &componentProcessDetails, processDetail)
opsRequest.Status.Components[componentName] = appsv1alpha1.OpsRequestComponentStatus{
Phase: phase,
ProgressDetails: componentProcessDetails,
}
}