-
Notifications
You must be signed in to change notification settings - Fork 160
/
policy_util.go
254 lines (220 loc) · 8.05 KB
/
policy_util.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
/*
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"
"net"
"sort"
"strconv"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
"github.com/apecloud/kubeblocks/controllers/apps/components"
"github.com/apecloud/kubeblocks/pkg/common"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
cfgproto "github.com/apecloud/kubeblocks/pkg/configuration/proto"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/rsm"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
"github.com/apecloud/kubeblocks/pkg/generics"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)
func getDeploymentRollingPods(params reconfigureParams) ([]corev1.Pod, error) {
// util.GetComponentPodList supports deployment
return getReplicationSetPods(params)
}
func getReplicationSetPods(params reconfigureParams) ([]corev1.Pod, error) {
var ctx = params.Ctx
var cluster = params.Cluster
podList, err := components.GetComponentPodList(ctx.Ctx, params.Client, *cluster, params.ClusterComponent.Name)
if err != nil {
return nil, err
}
return podList.Items, nil
}
// GetComponentPods gets all pods of the component.
func GetComponentPods(params reconfigureParams) ([]corev1.Pod, error) {
componentPods := make([]corev1.Pod, 0)
for i := range params.ComponentUnits {
pods, err := common.GetPodListByStatefulSet(params.Ctx.Ctx, params.Client, ¶ms.ComponentUnits[i])
if err != nil {
return nil, err
}
componentPods = append(componentPods, pods...)
}
return componentPods, nil
}
// CheckReconfigureUpdateProgress checks pods of the component is ready.
func CheckReconfigureUpdateProgress(pods []corev1.Pod, configKey, version string) int32 {
var (
readyPods int32 = 0
cfgAnnotationKey = core.GenerateUniqKeyWithConfig(constant.UpgradeRestartAnnotationKey, configKey)
)
for _, pod := range pods {
annotations := pod.Annotations
if len(annotations) != 0 && annotations[cfgAnnotationKey] == version && intctrlutil.PodIsReady(&pod) {
readyPods++
}
}
return readyPods
}
func getStatefulSetPods(params reconfigureParams) ([]corev1.Pod, error) {
if len(params.ComponentUnits) != 1 {
return nil, core.MakeError("statefulSet component require only one statefulset, actual %d components", len(params.ComponentUnits))
}
pods, err := GetComponentPods(params)
if err != nil {
return nil, err
}
sort.SliceStable(pods, func(i, j int) bool {
_, ordinal1 := intctrlutil.GetParentNameAndOrdinal(&pods[i])
_, ordinal2 := intctrlutil.GetParentNameAndOrdinal(&pods[j])
return ordinal1 < ordinal2
})
return pods, nil
}
func getConsensusPods(params reconfigureParams) ([]corev1.Pod, error) {
if len(params.ComponentUnits) > 1 {
return nil, core.MakeError("consensus component require only one statefulset, actual %d components", len(params.ComponentUnits))
}
if len(params.ComponentUnits) == 0 {
return nil, nil
}
pods, err := GetComponentPods(params)
// stsObj := ¶ms.ComponentUnits[0]
// pods, err := components.GetPodListByStatefulSetWithSelector(params.Ctx.Ctx, params.Client, stsObj, client.MatchingLabels{
// constant.KBAppComponentLabelKey: params.ClusterComponent.Name,
// constant.AppInstanceLabelKey: params.Cluster.Name,
// })
if err != nil {
return nil, err
}
// TODO: should resolve the dependency on consensus module
if params.Component.RSMSpec != nil {
rsm.SortPods(pods, rsm.ComposeRolePriorityMap(params.Component.RSMSpec.Roles), true)
}
return pods, nil
}
// TODO commonOnlineUpdateWithPod migrate to sql command pipeline
func commonOnlineUpdateWithPod(pod *corev1.Pod, ctx context.Context, createClient createReconfigureClient, configSpec string, updatedParams map[string]string) error {
address, err := cfgManagerGrpcURL(pod)
if err != nil {
return err
}
client, err := createClient(address)
if err != nil {
return err
}
response, err := client.OnlineUpgradeParams(ctx, &cfgproto.OnlineUpgradeParamsRequest{
ConfigSpec: configSpec,
Params: updatedParams,
})
if err != nil {
return err
}
errMessage := response.GetErrMessage()
if errMessage != "" {
return core.MakeError(errMessage)
}
return nil
}
func commonStopContainerWithPod(pod *corev1.Pod, ctx context.Context, containerNames []string, createClient createReconfigureClient) error {
containerIDs := make([]string, 0, len(containerNames))
for _, name := range containerNames {
containerID := intctrlutil.GetContainerID(pod, name)
if containerID == "" {
return core.MakeError("failed to find container in pod[%s], name=%s", name, pod.Name)
}
containerIDs = append(containerIDs, containerID)
}
address, err := cfgManagerGrpcURL(pod)
if err != nil {
return err
}
// stop container
client, err := createClient(address)
if err != nil {
return err
}
response, err := client.StopContainer(ctx, &cfgproto.StopContainerRequest{
ContainerIDs: containerIDs,
})
if err != nil {
return err
}
errMessage := response.GetErrMessage()
if errMessage != "" {
return core.MakeError(errMessage)
}
return nil
}
func cfgManagerGrpcURL(pod *corev1.Pod) (string, error) {
podPort := viper.GetInt(constant.ConfigManagerGPRCPortEnv)
return getURLFromPod(pod, podPort)
}
func getURLFromPod(pod *corev1.Pod, portPort int) (string, error) {
ip := net.ParseIP(pod.Status.PodIP)
if ip == nil {
return "", core.MakeError("%s is not a valid IP", pod.Status.PodIP)
}
// Sanity check PodIP
if ip.To4() == nil && ip.To16() == nil {
return "", fmt.Errorf("%s is not a valid IPv4/IPv6 address", pod.Status.PodIP)
}
return net.JoinHostPort(ip.String(), strconv.Itoa(portPort)), nil
}
func restartWorkloadComponent[T generics.Object, PT generics.PObject[T], L generics.ObjList[T], PL generics.PObjList[T, L]](cli client.Client, ctx context.Context, annotationKey, annotationValue string, obj PT, _ func(T, PT, L, PL)) error {
template := transformPodTemplate(obj)
if updatedVersion(template, annotationKey, annotationValue) {
return nil
}
patch := client.MergeFrom(PT(obj.DeepCopy()))
if template.Annotations == nil {
template.Annotations = map[string]string{}
}
template.Annotations[annotationKey] = annotationValue
if err := cli.Patch(ctx, obj, patch); err != nil {
return err
}
return nil
}
func restartComponent(cli client.Client, ctx intctrlutil.RequestCtx, configKey string, newVersion string, objs []client.Object, recordEvent func(obj client.Object)) (client.Object, error) {
var err error
cfgAnnotationKey := core.GenerateUniqKeyWithConfig(constant.UpgradeRestartAnnotationKey, configKey)
for _, obj := range objs {
switch w := obj.(type) {
case *appv1.StatefulSet:
err = restartWorkloadComponent(cli, ctx.Ctx, cfgAnnotationKey, newVersion, w, generics.StatefulSetSignature)
case *appv1.Deployment:
err = restartWorkloadComponent(cli, ctx.Ctx, cfgAnnotationKey, newVersion, w, generics.DeploymentSignature)
case *workloads.ReplicatedStateMachine:
err = restartWorkloadComponent(cli, ctx.Ctx, cfgAnnotationKey, newVersion, w, generics.RSMSignature)
default:
// ignore other types workload
}
if err != nil {
return obj, err
}
if recordEvent != nil {
recordEvent(obj)
}
}
return nil, nil
}
func updatedVersion(podTemplate *corev1.PodTemplateSpec, keyPath, expectedVersion string) bool {
return podTemplate.Annotations != nil && podTemplate.Annotations[keyPath] == expectedVersion
}