-
Notifications
You must be signed in to change notification settings - Fork 155
/
reconfigure_util.go
363 lines (330 loc) · 10.6 KB
/
reconfigure_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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
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"
"github.com/spf13/cast"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/log"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
)
type reconfiguringResult struct {
failed bool
noFormatFilesUpdated bool
configPatch *core.ConfigPatchInfo
lastAppliedConfigs map[string]string
err error
}
// type updateReconfigureStatus func(params []core.ParamPairs, orinalData map[string]string, formatter *appsv1alpha1.FormatterConfig) error
// Deprecated: use NewPipeline instead
// updateConfigConfigmapResource merges parameters of the config into the configmap, and verifies final configuration file.
// func updateConfigConfigmapResource(config appsv1alpha1.ConfigurationItem,
// configSpec appsv1alpha1.ComponentConfigSpec,
// cmKey client.ObjectKey,
// ctx context.Context,
// cli client.Client,
// opsCrName string,
// updater updateReconfigureStatus) reconfiguringResult {
// var (
// cm = &corev1.ConfigMap{}
// cc = &appsv1alpha1.ConfigConstraint{}
//
// err error
// newCfg map[string]string
// )
//
// if err := cli.Get(ctx, cmKey, cm); err != nil {
// return makeReconfiguringResult(err)
// }
// if err := cli.Get(ctx, client.ObjectKey{
// Namespace: configSpec.Namespace,
// Name: configSpec.ConfigConstraintRef,
// }, cc); err != nil {
// return makeReconfiguringResult(err)
// }
//
// updatedFiles := make(map[string]string, len(config.Keys))
// updatedParams := make([]core.ParamPairs, 0, len(config.Keys))
// for _, key := range config.Keys {
// if key.FileContent != "" {
// updatedFiles[key.Key] = key.FileContent
// }
// if len(key.Parameters) > 0 {
// updatedParams = append(updatedParams, core.ParamPairs{
// Key: key.Key,
// UpdatedParams: fromKeyValuePair(key.Parameters),
// })
// }
// }
//
// if newCfg, err = mergeUpdatedParams(cm.Data, updatedFiles, updatedParams, cc, configSpec); err != nil {
// return makeReconfiguringResult(err, withFailed(true))
// }
// configPatch, restart, err := core.CreateConfigPatch(cm.Data, newCfg, cc.Spec.FormatterConfig.Format, configSpec.Keys, len(updatedFiles) != 0)
// if err != nil {
// return makeReconfiguringResult(err)
// }
// if !restart && !configPatch.IsModify {
// return makeReconfiguringResult(nil, withReturned(newCfg, configPatch))
// }
// if updater != nil {
// if err := updater(updatedParams, cm.Data, cc.Spec.FormatterConfig); err != nil {
// return makeReconfiguringResult(err)
// }
// }
//
// return makeReconfiguringResult(
// syncConfigmap(cm, newCfg, cli, ctx, opsCrName, configSpec, &cc.Spec, config.Policy),
// withReturned(newCfg, configPatch),
// withNoFormatFilesUpdated(restart))
// }
// func mergeUpdatedParams(base map[string]string,
// updatedFiles map[string]string,
// updatedParams []core.ParamPairs,
// cc *appsv1alpha1.ConfigConstraint,
// tpl appsv1alpha1.ComponentConfigSpec) (map[string]string, error) {
// updatedConfig := base
//
// // merge updated files into configmap
// if len(updatedFiles) != 0 {
// return core.MergeUpdatedConfig(base, updatedFiles), nil
// }
// if cc == nil {
// return updatedConfig, nil
// }
// return intctrlutil.MergeAndValidateConfigs(cc.Spec, updatedConfig, tpl.Keys, updatedParams)
// }
// func syncConfigmap(
// cmObj *corev1.ConfigMap,
// newCfg map[string]string,
// cli client.Client,
// ctx context.Context,
// opsCrName string,
// configSpec appsv1alpha1.ComponentConfigSpec,
// cc *appsv1alpha1.ConfigConstraintSpec,
// policy *appsv1alpha1.UpgradePolicy) error {
//
// patch := client.MergeFrom(cmObj.DeepCopy())
// cmObj.Data = newCfg
// if cmObj.Annotations == nil {
// cmObj.Annotations = make(map[string]string)
// }
// if policy != nil {
// cmObj.Annotations[constant.UpgradePolicyAnnotationKey] = string(*policy)
// }
// cmObj.Annotations[constant.LastAppliedOpsCRAnnotationKey] = opsCrName
// core.SetParametersUpdateSource(cmObj, constant.ReconfigureUserSource)
// if err := configuration.SyncEnvConfigmap(configSpec, cmObj, cc, cli, ctx); err != nil {
// return err
// }
// return cli.Patch(ctx, cmObj, patch)
// }
func updateOpsLabelWithReconfigure(obj *appsv1alpha1.OpsRequest, params []core.ParamPairs, orinalData map[string]string, formatter *appsv1alpha1.FormatterConfig) {
var maxLabelCount = 16
updateLabel := func(param map[string]interface{}) {
if obj.Labels == nil {
obj.Labels = make(map[string]string)
}
for key, val := range param {
if maxLabelCount <= 0 {
return
}
maxLabelCount--
obj.Labels[key] = core.FromValueToString(val)
}
}
updateAnnotation := func(keyFile string, param map[string]interface{}) {
data, ok := orinalData[keyFile]
if !ok {
return
}
if obj.Annotations == nil {
obj.Annotations = make(map[string]string)
}
oldValue, err := fetchOriginalValue(keyFile, data, param, formatter)
if err != nil {
log.Log.Error(err, "failed to fetch original value")
return
}
obj.Annotations[keyFile] = oldValue
}
for _, param := range params {
updateLabel(param.UpdatedParams)
if maxLabelCount <= 0 {
return
}
updateAnnotation(param.Key, param.UpdatedParams)
}
}
func fetchOriginalValue(keyFile, data string, params map[string]interface{}, formatter *appsv1alpha1.FormatterConfig) (string, error) {
baseConfigObj, err := core.FromConfigObject(keyFile, data, formatter)
if err != nil {
return "", err
}
r := make(map[string]string, len(params))
for key := range params {
oldVal := baseConfigObj.Get(key)
if oldVal != nil {
r[key] = cast.ToString(oldVal)
}
}
b, err := json.Marshal(r)
return string(b), err
}
func fromKeyValuePair(parameters []appsv1alpha1.ParameterPair) map[string]interface{} {
m := make(map[string]interface{}, len(parameters))
for _, param := range parameters {
if param.Value != nil {
m[param.Key] = *param.Value
} else {
m[param.Key] = nil
}
}
return m
}
func withFailed(failed bool) func(result *reconfiguringResult) {
return func(result *reconfiguringResult) {
result.failed = failed
}
}
func withReturned(configs map[string]string, patch *core.ConfigPatchInfo) func(result *reconfiguringResult) {
return func(result *reconfiguringResult) {
result.lastAppliedConfigs = configs
result.configPatch = patch
}
}
func withNoFormatFilesUpdated(changed bool) func(result *reconfiguringResult) {
return func(result *reconfiguringResult) {
result.noFormatFilesUpdated = changed
}
}
func makeReconfiguringResult(err error, ops ...func(*reconfiguringResult)) reconfiguringResult {
result := reconfiguringResult{
failed: false,
err: err,
}
for _, o := range ops {
o(&result)
}
return result
}
func constructReconfiguringConditions(result reconfiguringResult, resource *OpsResource, configSpec *appsv1alpha1.ComponentConfigSpec) *metav1.Condition {
if result.noFormatFilesUpdated || (result.configPatch != nil && result.configPatch.IsModify) {
return appsv1alpha1.NewReconfigureRunningCondition(
resource.OpsRequest,
appsv1alpha1.ReasonReconfigurePersisted,
configSpec.Name,
formatConfigPatchToMessage(result.configPatch, nil))
}
return appsv1alpha1.NewReconfigureRunningCondition(
resource.OpsRequest,
appsv1alpha1.ReasonReconfigureNoChanged,
configSpec.Name,
formatConfigPatchToMessage(result.configPatch, nil))
}
func i2sMap(config map[string]interface{}) map[string]string {
if len(config) == 0 {
return nil
}
m := make(map[string]string, len(config))
for key, value := range config {
data, _ := json.Marshal(value)
m[key] = string(data)
}
return m
}
func b2sMap(config map[string][]byte) map[string]string {
if len(config) == 0 {
return nil
}
m := make(map[string]string, len(config))
for key, value := range config {
m[key] = string(value)
}
return m
}
func processMergedFailed(resource *OpsResource, isInvalid bool, err error) error {
if !isInvalid {
return core.WrapError(err, "failed to update param!")
}
// if failed to validate configure, set opsRequest to failed and return
failedCondition := appsv1alpha1.NewReconfigureFailedCondition(resource.OpsRequest, err)
resource.OpsRequest.SetStatusCondition(*failedCondition)
return &FastFaileError{message: err.Error()}
}
func formatConfigPatchToMessage(configPatch *core.ConfigPatchInfo, execStatus *core.PolicyExecStatus) string {
policyName := ""
if execStatus != nil {
policyName = fmt.Sprintf("updated policy: <%s>, ", execStatus.PolicyName)
}
if configPatch == nil {
return fmt.Sprintf("%supdated full config files.", policyName)
}
return fmt.Sprintf("%supdated: %s, added: %s, deleted:%s",
policyName,
configPatch.UpdateConfig,
configPatch.AddConfig,
configPatch.DeleteConfig)
}
func updateFileContent(item *appsv1alpha1.ConfigurationItemDetail, key string, content string) {
params, ok := item.ConfigFileParams[key]
if !ok {
item.ConfigFileParams[key] = appsv1alpha1.ConfigParams{
Content: &content,
}
return
}
item.ConfigFileParams[key] = appsv1alpha1.ConfigParams{
Parameters: params.Parameters,
Content: &content,
}
}
func updateParameters(item *appsv1alpha1.ConfigurationItemDetail, key string, parameters []appsv1alpha1.ParameterPair) {
updatedParams := make(map[string]*string, len(parameters))
for _, parameter := range parameters {
updatedParams[parameter.Key] = parameter.Value
}
params, ok := item.ConfigFileParams[key]
if !ok {
item.ConfigFileParams[key] = appsv1alpha1.ConfigParams{
Parameters: updatedParams,
}
return
}
item.ConfigFileParams[key] = appsv1alpha1.ConfigParams{
Content: params.Content,
Parameters: mergeMaps(params.Parameters, updatedParams),
}
}
func mergeMaps(m1 map[string]*string, m2 map[string]*string) map[string]*string {
merged := make(map[string]*string)
for key, value := range m1 {
merged[key] = value
}
for key, value := range m2 {
merged[key] = value
}
return merged
}
func hasFileUpdate(config appsv1alpha1.ConfigurationItem) bool {
for _, key := range config.Keys {
if key.FileContent != "" {
return true
}
}
return false
}