-
Notifications
You must be signed in to change notification settings - Fork 155
/
reconfigure_controller.go
312 lines (273 loc) · 11.7 KB
/
reconfigure_controller.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
/*
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"
"encoding/json"
"fmt"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgcm "github.com/apecloud/kubeblocks/pkg/configuration/config_manager"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
// ReconfigureReconciler reconciles a ReconfigureRequest object
type ReconfigureReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
}
const (
ConfigReconcileInterval = time.Second * 1
)
const (
configurationNoChangedMessage = "the configuration file has not been modified, skip reconfigure"
configurationNotUsingMessage = "the configmap is not used by any container, skip reconfigure"
configurationNotRelatedComponentMessage = "related component does not found any configSpecs, skip reconfigure"
)
var ConfigurationRequiredLabels = []string{
constant.AppNameLabelKey,
constant.AppInstanceLabelKey,
constant.KBAppComponentLabelKey,
constant.CMConfigurationTemplateNameLabelKey,
constant.CMConfigurationTypeLabelKey,
constant.CMConfigurationSpecProviderLabelKey,
}
// +kubebuilder:rbac:groups=core,resources=configmap,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=configmap/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the ReconfigureRequest object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.2/pkg/reconcile
func (r *ReconfigureReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reqCtx := intctrlutil.RequestCtx{
Ctx: ctx,
Req: req,
Log: log.FromContext(ctx).WithName("ReconfigureRequestReconcile").WithValues("ConfigMap", req.NamespacedName),
Recorder: r.Recorder,
}
config := &corev1.ConfigMap{}
if err := r.Client.Get(reqCtx.Ctx, reqCtx.Req.NamespacedName, config); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "cannot find configmap")
}
if !checkConfigurationObject(config) {
return intctrlutil.Reconciled()
}
reqCtx.Log = reqCtx.Log.
WithValues("ClusterName", config.Labels[constant.AppInstanceLabelKey]).
WithValues("ComponentName", config.Labels[constant.KBAppComponentLabelKey])
if hash, ok := config.Labels[constant.CMInsConfigurationHashLabelKey]; ok && hash == config.ResourceVersion {
return intctrlutil.Reconciled()
}
isAppliedConfigs, err := checkAndApplyConfigsChanged(r.Client, reqCtx, config)
if err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "failed to check last-applied-configuration")
} else if isAppliedConfigs {
return updateConfigPhase(r.Client, reqCtx, config, appsv1alpha1.CFinishedPhase, configurationNoChangedMessage)
}
// process configuration without ConfigConstraints
cfgConstraintsName, ok := config.Labels[constant.CMConfigurationConstraintsNameLabelKey]
if !ok || cfgConstraintsName == "" {
reqCtx.Log.Info("configuration without ConfigConstraints.")
return r.sync(reqCtx, config, &appsv1alpha1.ConfigConstraint{})
}
// process configuration with ConfigConstraints
key := types.NamespacedName{
Namespace: config.Namespace,
Name: config.Labels[constant.CMConfigurationConstraintsNameLabelKey],
}
tpl := &appsv1alpha1.ConfigConstraint{}
if err := r.Client.Get(reqCtx.Ctx, key, tpl); err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(config, r.Recorder, err, reqCtx.Log)
}
return r.sync(reqCtx, config, tpl)
}
// SetupWithManager sets up the controller with the Manager.
func (r *ReconfigureReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.ConfigMap{}).
WithEventFilter(predicate.NewPredicateFuncs(checkConfigurationObject)).
Complete(r)
}
func checkConfigurationObject(object client.Object) bool {
return checkConfigLabels(object, ConfigurationRequiredLabels)
}
func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *corev1.ConfigMap, configConstraint *appsv1alpha1.ConfigConstraint) (ctrl.Result, error) {
var (
componentName = configMap.Labels[constant.KBAppComponentLabelKey]
configSpecName = configMap.Labels[constant.CMConfigurationSpecProviderLabelKey]
)
componentLabels := map[string]string{
constant.AppNameLabelKey: configMap.Labels[constant.AppNameLabelKey],
constant.AppInstanceLabelKey: configMap.Labels[constant.AppInstanceLabelKey],
constant.KBAppComponentLabelKey: configMap.Labels[constant.KBAppComponentLabelKey],
}
var keySelector []string
if keysLabel, ok := configMap.Labels[constant.CMConfigurationCMKeysLabelKey]; ok && keysLabel != "" {
keySelector = strings.Split(keysLabel, ",")
}
configPatch, forceRestart, err := createConfigPatch(configMap, configConstraint.Spec.FormatterConfig, keySelector)
if err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(configMap, r.Recorder, err, reqCtx.Log)
}
// No parameters updated
if configPatch != nil && !configPatch.IsModify {
reqCtx.Recorder.Eventf(configMap, corev1.EventTypeNormal, appsv1alpha1.ReasonReconfigureRunning,
"nothing changed, skip reconfigure")
return r.updateConfigCMStatus(reqCtx, configMap, core.ReconfigureNoChangeType, nil)
}
if configPatch != nil {
reqCtx.Log.V(1).Info(fmt.Sprintf(
"reconfigure params: \n\tadd: %s\n\tdelete: %s\n\tupdate: %s",
configPatch.AddConfig,
configPatch.DeleteConfig,
configPatch.UpdateConfig))
}
reconcileContext := newConfigReconcileContext(
&intctrlutil.ResourceCtx{
Context: reqCtx.Ctx,
Client: r.Client,
Namespace: configMap.Namespace,
ClusterName: configMap.Labels[constant.AppInstanceLabelKey],
ComponentName: componentName,
},
configMap,
configConstraint,
configSpecName,
componentLabels)
if err := reconcileContext.GetRelatedObjects(); err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(configMap, r.Recorder, err, reqCtx.Log)
}
// Assumption: It is required that the cluster must have a component.
if reconcileContext.ClusterComObj == nil {
reqCtx.Log.Info("not found component.")
return intctrlutil.Reconciled()
}
if reconcileContext.ConfigSpec == nil {
reqCtx.Log.Info(fmt.Sprintf("not found configSpec[%s] in the component[%s].", configSpecName, componentName))
reqCtx.Recorder.Eventf(configMap,
corev1.EventTypeWarning,
appsv1alpha1.ReasonReconfigureFailed,
configurationNotRelatedComponentMessage)
return updateConfigPhase(r.Client, reqCtx, configMap, appsv1alpha1.CFinishedPhase, configurationNotRelatedComponentMessage)
}
if len(reconcileContext.StatefulSets) == 0 && len(reconcileContext.Deployments) == 0 {
reqCtx.Recorder.Eventf(configMap,
corev1.EventTypeWarning, appsv1alpha1.ReasonReconfigureFailed,
"the configmap is not used by any container, skip reconfigure")
return updateConfigPhase(r.Client, reqCtx, configMap, appsv1alpha1.CFinishedPhase, configurationNotUsingMessage)
}
return r.performUpgrade(reconfigureParams{
ConfigSpecName: configSpecName,
ConfigPatch: configPatch,
ConfigMap: configMap,
ConfigConstraint: &configConstraint.Spec,
Client: r.Client,
Ctx: reqCtx,
Cluster: reconcileContext.ClusterObj,
ContainerNames: reconcileContext.Containers,
ComponentUnits: reconcileContext.StatefulSets,
DeploymentUnits: reconcileContext.Deployments,
RSMList: reconcileContext.RSMList,
Component: reconcileContext.ClusterDefComObj,
ClusterComponent: reconcileContext.ClusterComObj,
Restart: forceRestart || !cfgcm.IsSupportReload(configConstraint.Spec.ReloadOptions),
ReconfigureClientFactory: GetClientFactory(),
})
}
func (r *ReconfigureReconciler) updateConfigCMStatus(reqCtx intctrlutil.RequestCtx, cfg *corev1.ConfigMap, reconfigureType string, result *intctrlutil.Result) (ctrl.Result, error) {
configData, err := json.Marshal(cfg.Data)
if err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(cfg, r.Recorder, err, reqCtx.Log)
}
if ok, err := updateAppliedConfigs(r.Client, reqCtx, cfg, configData, reconfigureType, result); err != nil || !ok {
return intctrlutil.RequeueAfter(ConfigReconcileInterval, reqCtx.Log, "failed to patch status and retry...", "error", err)
}
return intctrlutil.Reconciled()
}
func (r *ReconfigureReconciler) performUpgrade(params reconfigureParams) (ctrl.Result, error) {
policy, err := NewReconfigurePolicy(params.ConfigConstraint, params.ConfigPatch, getUpgradePolicy(params.ConfigMap), params.Restart)
if err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(params.ConfigMap, r.Recorder, err, params.Ctx.Log)
}
returnedStatus, err := policy.Upgrade(params)
if err != nil {
params.Ctx.Log.Error(err, "failed to update engine parameters")
}
switch returnedStatus.Status {
default:
return updateConfigPhaseWithResult(
params.Client,
params.Ctx,
params.ConfigMap,
reconciled(returnedStatus, policy.GetPolicyName(), appsv1alpha1.CFailedAndPausePhase,
withFailed(core.MakeError("unknown status"), false)),
)
case ESFailedAndRetry:
return updateConfigPhaseWithResult(
params.Client,
params.Ctx,
params.ConfigMap,
reconciled(returnedStatus, policy.GetPolicyName(), appsv1alpha1.CFailedPhase,
withFailed(err, true)),
)
case ESRetry:
return updateConfigPhaseWithResult(
params.Client,
params.Ctx,
params.ConfigMap,
reconciled(returnedStatus, policy.GetPolicyName(), appsv1alpha1.CUpgradingPhase),
)
case ESFailed:
return updateConfigPhaseWithResult(
params.Client,
params.Ctx,
params.ConfigMap,
reconciled(returnedStatus, policy.GetPolicyName(), appsv1alpha1.CFailedAndPausePhase,
withFailed(err, false)),
)
case ESNone:
params.Ctx.Recorder.Eventf(
params.ConfigMap,
corev1.EventTypeNormal,
appsv1alpha1.ReasonReconfigureSucceed,
"the reconfigure[%s] request[%s] has been processed successfully",
policy.GetPolicyName(),
getOpsRequestID(params.ConfigMap))
result := reconciled(returnedStatus, policy.GetPolicyName(), appsv1alpha1.CFinishedPhase)
return r.updateConfigCMStatus(params.Ctx, params.ConfigMap, policy.GetPolicyName(), &result)
}
}
func getOpsRequestID(cm *corev1.ConfigMap) string {
if len(cm.Annotations) != 0 {
return cm.Annotations[constant.LastAppliedOpsCRAnnotationKey]
}
return ""
}