-
Notifications
You must be signed in to change notification settings - Fork 160
/
rolling_upgrade_policy.go
249 lines (211 loc) · 7.16 KB
/
rolling_upgrade_policy.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
/*
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 configuration
import (
"context"
"fmt"
"os"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/configuration/util"
"github.com/apecloud/kubeblocks/pkg/constant"
podutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)
const (
// StatefulSetSpec.Spec.MinReadySeconds
// units: s
defaultMinReadySeconds = 10
)
type rollingUpgradePolicy struct {
}
func init() {
RegisterPolicy(appsv1alpha1.RollingPolicy, &rollingUpgradePolicy{})
if err := viper.BindEnv(constant.PodMinReadySecondsEnv); err != nil {
os.Exit(-1)
}
viper.SetDefault(constant.PodMinReadySecondsEnv, defaultMinReadySeconds)
}
func (r *rollingUpgradePolicy) Upgrade(params reconfigureParams) (ReturnedStatus, error) {
var funcs RollingUpgradeFuncs
switch params.WorkloadType() {
case appsv1alpha1.Consensus:
funcs = GetConsensusRollingUpgradeFuncs()
case appsv1alpha1.Replication:
funcs = GetReplicationRollingUpgradeFuncs()
case appsv1alpha1.Stateful:
funcs = GetStatefulSetRollingUpgradeFuncs()
default:
return makeReturnedStatus(ESNotSupport), cfgcore.MakeError("not supported component workload type[%s]", params.WorkloadType())
}
return performRollingUpgrade(params, funcs)
}
func (r *rollingUpgradePolicy) GetPolicyName() string {
return string(appsv1alpha1.RollingPolicy)
}
func canPerformUpgrade(pods []corev1.Pod, params reconfigureParams) bool {
target := params.getTargetReplicas()
if len(pods) == target {
return true
}
if params.WorkloadType() == appsv1alpha1.Consensus {
params.Ctx.Log.Info(fmt.Sprintf("wait for consensus component is ready, %d pods are ready, and the expected replicas is %d.", len(pods), target))
return false
}
if len(pods) < target {
params.Ctx.Log.Info(fmt.Sprintf("component pods are not all ready, %d pods are ready, which is less than the expected replicas(%d).", len(pods), target))
return false
}
return true
}
func performRollingUpgrade(params reconfigureParams, funcs RollingUpgradeFuncs) (ReturnedStatus, error) {
pods, err := funcs.GetPodsFunc(params)
if err != nil {
return makeReturnedStatus(ESFailedAndRetry), err
}
var (
rollingReplicas = params.maxRollingReplicas()
configKey = params.getConfigKey()
configVersion = params.getTargetVersionHash()
)
if !canPerformUpgrade(pods, params) {
return makeReturnedStatus(ESRetry), nil
}
podStats := staticPodStats(pods, params.getTargetReplicas(), params.podMinReadySeconds())
podWins := markDynamicCursor(pods, podStats, configKey, configVersion, rollingReplicas)
if !validPodState(podWins) {
params.Ctx.Log.Info("wait for pod stat ready.")
return makeReturnedStatus(ESRetry), nil
}
waitRollingPods := podWins.getWaitRollingPods()
if len(waitRollingPods) == 0 {
return makeReturnedStatus(ESNone, withSucceed(int32(podStats.targetReplica)), withExpected(int32(podStats.targetReplica))), nil
}
for _, pod := range waitRollingPods {
if podStats.isUpdating(&pod) {
params.Ctx.Log.Info("pod is in rolling update.", "pod name", pod.Name)
continue
}
if err := funcs.RestartContainerFunc(&pod, params.Ctx.Ctx, params.ContainerNames, params.ReconfigureClientFactory); err != nil {
return makeReturnedStatus(ESFailedAndRetry), err
}
if err := updatePodLabelsWithConfigVersion(&pod, configKey, configVersion, params.Client, params.Ctx.Ctx); err != nil {
return makeReturnedStatus(ESFailedAndRetry), err
}
}
return makeReturnedStatus(ESRetry,
withExpected(int32(podStats.targetReplica)),
withSucceed(int32(len(podStats.updated)+len(podStats.updating)))), nil
}
func validPodState(wind switchWindow) bool {
for i := 0; i < wind.begin; i++ {
pod := &wind.pods[i]
if !wind.isReady(pod) {
return false
}
}
return true
}
func markDynamicCursor(pods []corev1.Pod, podsStats *componentPodStats, configKey, currentVersion string, rollingReplicas int32) switchWindow {
podWindows := switchWindow{
end: 0,
begin: len(pods),
pods: pods,
componentPodStats: podsStats,
}
// find update last
for i := podsStats.targetReplica - 1; i >= 0; i-- {
pod := &pods[i]
if !podutil.IsMatchConfigVersion(pod, configKey, currentVersion) {
podWindows.end = i + 1
break
}
if !podsStats.isAvailable(pod) {
podsStats.updating[pod.Name] = pod
podWindows.end = i + 1
break
}
podsStats.updated[pod.Name] = pod
}
podWindows.begin = util.Max[int](podWindows.end-int(rollingReplicas), 0)
for i := podWindows.begin; i < podWindows.end; i++ {
pod := &pods[i]
if podutil.IsMatchConfigVersion(pod, configKey, currentVersion) {
podsStats.updating[pod.Name] = pod
}
}
return podWindows
}
func staticPodStats(pods []corev1.Pod, targetReplicas int, minReadySeconds int32) *componentPodStats {
podsStats := &componentPodStats{
updated: make(map[string]*corev1.Pod),
updating: make(map[string]*corev1.Pod),
available: make(map[string]*corev1.Pod),
ready: make(map[string]*corev1.Pod),
targetReplica: targetReplicas,
}
for i := 0; i < len(pods); i++ {
pod := &pods[i]
switch {
case podutil.IsAvailable(pod, minReadySeconds):
podsStats.available[pod.Name] = pod
case podutil.PodIsReady(pod):
podsStats.ready[pod.Name] = pod
default:
}
}
return podsStats
}
type componentPodStats struct {
// failed to start pod
ready map[string]*corev1.Pod
available map[string]*corev1.Pod
// updated pod count
updated map[string]*corev1.Pod
updating map[string]*corev1.Pod
// expected pod
targetReplica int
}
func (s *componentPodStats) isAvailable(pod *corev1.Pod) bool {
_, ok := s.available[pod.Name]
return ok
}
func (s *componentPodStats) isReady(pod *corev1.Pod) bool {
_, ok := s.ready[pod.Name]
return ok || s.isAvailable(pod)
}
func (s *componentPodStats) isUpdating(pod *corev1.Pod) bool {
_, ok := s.updating[pod.Name]
return ok
}
type switchWindow struct {
begin int
end int
pods []corev1.Pod
*componentPodStats
}
func (w *switchWindow) getWaitRollingPods() []corev1.Pod {
return w.pods[w.begin:w.end]
}
func updatePodLabelsWithConfigVersion(pod *corev1.Pod, labelKey, configVersion string, cli client.Client, ctx context.Context) error {
patch := client.MergeFrom(pod.DeepCopy())
if pod.Labels == nil {
pod.Labels = make(map[string]string, 1)
}
pod.Labels[labelKey] = configVersion
return cli.Patch(ctx, pod, patch)
}