Skip to content

Commit a7ee586

Browse files
leon-apeclaude
andauthored
feat(parameters): migrate config manager to kbagent (#10020)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1dc89b3 commit a7ee586

47 files changed

Lines changed: 1546 additions & 3339 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

controllers/apps/component/component_controller.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ func (r *ComponentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
165165
&componentAccountProvisionTransformer{},
166166
// render config/script templates
167167
&componentFileTemplateTransformer{},
168-
// HACK: the legacy reload sidecar
169-
&componentReloadSidecarTransformer{Client: r.Client},
170168
// handle restore before workloads transform
171169
&componentRestoreTransformer{Client: r.Client},
172170
// handle RBAC for component workloads

controllers/apps/component/transformer_component_reload_sidecar.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

controllers/parameters/componentparameter_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (r *ComponentParameterReconciler) reconcile(reqCtx intctrlutil.RequestCtx,
112112
return r.failWithInvalidComponent(componentParameter, reqCtx)
113113
}
114114

115-
taskCtx, err := NewTaskContext(reqCtx.Ctx, r.Client, componentParameter, fetcherTask)
115+
taskCtx, err := newTaskContext(reqCtx.Ctx, r.Client, componentParameter, fetcherTask)
116116
if err != nil {
117117
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, errors.Wrap(err, "failed to create task context").Error())
118118
}
@@ -136,7 +136,7 @@ func (r *ComponentParameterReconciler) failWithInvalidComponent(componentParam *
136136
return intctrlutil.Reconciled()
137137
}
138138

139-
func (r *ComponentParameterReconciler) runTasks(taskCtx *TaskContext, tasks []Task, resource *Task) error {
139+
func (r *ComponentParameterReconciler) runTasks(taskCtx *taskContext, tasks []Task, resource *Task) error {
140140
var (
141141
errs []error
142142
compParameter = taskCtx.componentParameter

controllers/parameters/componentparameter_controller_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,26 @@ import (
3333
)
3434

3535
var _ = Describe("ComponentParameter Controller", func() {
36-
3736
BeforeEach(cleanEnv)
3837

3938
AfterEach(cleanEnv)
4039

41-
Context("When updating configuration", func() {
42-
It("Should reconcile success", func() {
43-
mockReconcileResource()
40+
Context("reconcile", func() {
41+
It("should reconcile success", func() {
42+
_, _, _, _, itsObj := mockReconcileResource()
4443

44+
By("wait for component parameter to be ready")
4545
cfgKey := client.ObjectKey{
46-
Name: core.GenerateComponentConfigurationName(clusterName, defaultCompName),
4746
Namespace: testCtx.DefaultNamespace,
47+
Name: core.GenerateComponentConfigurationName(clusterName, defaultCompName),
4848
}
49-
50-
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, componentParameter *parametersv1alpha1.ComponentParameter) {
51-
g.Expect(componentParameter.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
52-
itemStatus := parameters.GetItemStatus(&componentParameter.Status, configSpecName)
53-
g.Expect(itemStatus.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
49+
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, cfg *parametersv1alpha1.ComponentParameter) {
50+
g.Expect(cfg.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
51+
status := parameters.GetItemStatus(&cfg.Status, configSpecName)
52+
g.Expect(status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
5453
})).Should(Succeed())
5554

56-
By("reconfiguring parameters.")
55+
By("update parameters")
5756
Eventually(testapps.GetAndChangeObj(&testCtx, cfgKey, func(cfg *parametersv1alpha1.ComponentParameter) {
5857
item := parameters.GetConfigTemplateItem(&cfg.Spec, configSpecName)
5958
item.ConfigFileParams = map[string]parametersv1alpha1.ParametersInFile{
@@ -66,13 +65,24 @@ var _ = Describe("ComponentParameter Controller", func() {
6665
}
6766
})).Should(Succeed())
6867

68+
By("check component parameter status is updated to Upgrading")
6969
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, cfg *parametersv1alpha1.ComponentParameter) {
70-
itemStatus := parameters.GetItemStatus(&cfg.Status, configSpecName)
71-
g.Expect(itemStatus).ShouldNot(BeNil())
72-
g.Expect(itemStatus.UpdateRevision).Should(BeEquivalentTo("2"))
73-
g.Expect(itemStatus.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
70+
status := parameters.GetItemStatus(&cfg.Status, configSpecName)
71+
g.Expect(status).ShouldNot(BeNil())
72+
g.Expect(status.UpdateRevision).Should(BeEquivalentTo("2"))
73+
g.Expect(status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CUpgradingPhase))
7474
})).Should(Succeed())
75-
})
7675

76+
By("mock the reconfigure done")
77+
mockReconfigureDone(itsObj.Namespace, itsObj.Name, configSpecName, configHash3)
78+
79+
By("check component parameter status is updated to Finished")
80+
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, cfg *parametersv1alpha1.ComponentParameter) {
81+
status := parameters.GetItemStatus(&cfg.Status, configSpecName)
82+
g.Expect(status).ShouldNot(BeNil())
83+
g.Expect(status.UpdateRevision).Should(BeEquivalentTo("2"))
84+
g.Expect(status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
85+
})).Should(Succeed())
86+
})
7787
})
7888
})

controllers/parameters/reconcile_task.go renamed to controllers/parameters/componentparameter_controller_utils.go

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package parameters
2222
import (
2323
"context"
2424
"encoding/json"
25+
"fmt"
2526
"reflect"
2627
"strconv"
2728

@@ -32,6 +33,7 @@ import (
3233
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3334
"sigs.k8s.io/controller-runtime/pkg/log"
3435

36+
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
3537
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
3638
"github.com/apecloud/kubeblocks/pkg/constant"
3739
"github.com/apecloud/kubeblocks/pkg/controller/component"
@@ -40,7 +42,6 @@ import (
4042
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
4143
"github.com/apecloud/kubeblocks/pkg/parameters"
4244
"github.com/apecloud/kubeblocks/pkg/parameters/core"
43-
cfgutil "github.com/apecloud/kubeblocks/pkg/parameters/util"
4445
)
4546

4647
type Task struct {
@@ -49,18 +50,18 @@ type Task struct {
4950
Status *parametersv1alpha1.ConfigTemplateItemDetailStatus
5051
Name string
5152

52-
Do func(resource *Task, taskCtx *TaskContext, revision string) error
53+
Do func(resource *Task, taskCtx *taskContext, revision string) error
5354
}
5455

55-
type TaskContext struct {
56+
type taskContext struct {
5657
componentParameter *parametersv1alpha1.ComponentParameter
5758
configRender *parametersv1alpha1.ParamConfigRenderer
5859
ctx context.Context
5960
component *component.SynthesizedComponent
6061
paramsDefs []*parametersv1alpha1.ParametersDefinition
6162
}
6263

63-
func NewTaskContext(ctx context.Context, cli client.Client, componentParameter *parametersv1alpha1.ComponentParameter, fetchTask *Task) (*TaskContext, error) {
64+
func newTaskContext(ctx context.Context, cli client.Client, componentParameter *parametersv1alpha1.ComponentParameter, fetchTask *Task) (*taskContext, error) {
6465
// build synthesized component for the component
6566
cmpd := fetchTask.ComponentDefObj
6667
synthesizedComp, err := component.BuildSynthesizedComponent(ctx, cli, cmpd, fetchTask.ComponentObj)
@@ -98,18 +99,68 @@ func NewTaskContext(ctx context.Context, cli client.Client, componentParameter *
9899
}
99100
}
100101

101-
return &TaskContext{ctx: ctx,
102+
return &taskContext{ctx: ctx,
102103
componentParameter: componentParameter,
103104
configRender: configRender,
104105
component: synthesizedComp,
105106
paramsDefs: paramsDefs,
106107
}, nil
107108
}
108109

109-
func NewTask(item parametersv1alpha1.ConfigTemplateItemDetail, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) Task {
110+
func buildTemplateVars(ctx context.Context, cli client.Reader,
111+
compDef *appsv1.ComponentDefinition, synthesizedComp *component.SynthesizedComponent) error {
112+
if compDef != nil && len(compDef.Spec.Vars) > 0 {
113+
templateVars, _, err := component.ResolveTemplateNEnvVars(ctx, cli, synthesizedComp, compDef.Spec.Vars)
114+
if err != nil {
115+
return err
116+
}
117+
synthesizedComp.TemplateVars = templateVars
118+
}
119+
return nil
120+
}
121+
122+
func generateReconcileTasks(reqCtx intctrlutil.RequestCtx, componentParameter *parametersv1alpha1.ComponentParameter) []Task {
123+
tasks := make([]Task, 0, len(componentParameter.Spec.ConfigItemDetails))
124+
for _, item := range componentParameter.Spec.ConfigItemDetails {
125+
if status := fromItemStatus(reqCtx, &componentParameter.Status, item, componentParameter.GetGeneration()); status != nil {
126+
tasks = append(tasks, newTask(item, status))
127+
}
128+
}
129+
return tasks
130+
}
131+
132+
func fromItemStatus(ctx intctrlutil.RequestCtx, status *parametersv1alpha1.ComponentParameterStatus, item parametersv1alpha1.ConfigTemplateItemDetail, generation int64) *parametersv1alpha1.ConfigTemplateItemDetailStatus {
133+
if item.ConfigSpec == nil {
134+
ctx.Log.V(1).WithName(item.Name).Info(fmt.Sprintf("configuration is creating and pass: %s", item.Name))
135+
return nil
136+
}
137+
itemStatus := parameters.GetItemStatus(status, item.Name)
138+
if itemStatus == nil || itemStatus.Phase == "" {
139+
ctx.Log.WithName(item.Name).Info(fmt.Sprintf("ComponentParameters cr is creating: %v", item))
140+
status.ConfigurationItemStatus = append(status.ConfigurationItemStatus, parametersv1alpha1.ConfigTemplateItemDetailStatus{
141+
Name: item.Name,
142+
Phase: parametersv1alpha1.CInitPhase,
143+
UpdateRevision: strconv.FormatInt(generation, 10),
144+
})
145+
itemStatus = parameters.GetItemStatus(status, item.Name)
146+
}
147+
if !isReconcileStatus(itemStatus.Phase) {
148+
ctx.Log.V(1).WithName(item.Name).Info(fmt.Sprintf("configuration cr is creating or deleting and pass: %v", itemStatus))
149+
return nil
150+
}
151+
return itemStatus
152+
}
153+
154+
func isReconcileStatus(phase parametersv1alpha1.ParameterPhase) bool {
155+
return phase != "" &&
156+
phase != parametersv1alpha1.CCreatingPhase &&
157+
phase != parametersv1alpha1.CDeletingPhase
158+
}
159+
160+
func newTask(item parametersv1alpha1.ConfigTemplateItemDetail, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) Task {
110161
return Task{
111162
Name: item.Name,
112-
Do: func(resource *Task, taskCtx *TaskContext, revision string) error {
163+
Do: func(resource *Task, taskCtx *taskContext, revision string) error {
113164
if item.ConfigSpec == nil {
114165
return core.MakeError("not found config spec: %s", item.Name)
115166
}
@@ -136,7 +187,7 @@ func NewTask(item parametersv1alpha1.ConfigTemplateItemDetail, status *parameter
136187
}
137188
}
138189

139-
func syncImpl(taskCtx *TaskContext,
190+
func syncImpl(taskCtx *taskContext,
140191
fetcher *Task,
141192
item parametersv1alpha1.ConfigTemplateItemDetail,
142193
status *parametersv1alpha1.ConfigTemplateItemDetailStatus,
@@ -282,7 +333,7 @@ func updateConfigLabels(obj *corev1.ConfigMap,
282333
if obj.Labels == nil {
283334
obj.Labels = make(map[string]string)
284335
}
285-
hash, _ := cfgutil.ComputeHash(obj.Data)
336+
hash, _ := intctrlutil.ComputeHash(obj.Data)
286337
obj.Labels[constant.CMInsConfigurationHashLabelKey] = hash
287338
obj.Labels[constant.CMConfigurationSpecProviderLabelKey] = item.Name
288339
obj.Labels[constant.CMConfigurationTemplateNameLabelKey] = item.ConfigSpec.Template
@@ -292,7 +343,7 @@ func updateConfigLabels(obj *corev1.ConfigMap,
292343
func syncStatus(configMap *corev1.ConfigMap, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) (err error) {
293344
annotations := configMap.GetAnnotations()
294345
// status.CurrentRevision = GetCurrentRevision(annotations)
295-
revisions := RetrieveRevision(annotations)
346+
revisions := retrieveRevision(annotations)
296347
if len(revisions) == 0 {
297348
return
298349
}
@@ -305,22 +356,22 @@ func syncStatus(configMap *corev1.ConfigMap, status *parametersv1alpha1.ConfigTe
305356
return
306357
}
307358

308-
func updateLastDoneRevision(revision ConfigurationRevision, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) {
309-
if revision.Phase == parametersv1alpha1.CFinishedPhase {
310-
status.LastDoneRevision = strconv.FormatInt(revision.Revision, 10)
359+
func updateLastDoneRevision(revision configurationRevision, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) {
360+
if revision.phase == parametersv1alpha1.CFinishedPhase {
361+
status.LastDoneRevision = strconv.FormatInt(revision.revision, 10)
311362
}
312363
}
313364

314-
func updateRevision(revision ConfigurationRevision, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) {
315-
if revision.StrRevision == status.UpdateRevision {
316-
status.Phase = revision.Phase
365+
func updateRevision(revision configurationRevision, status *parametersv1alpha1.ConfigTemplateItemDetailStatus) {
366+
if revision.strRevision == status.UpdateRevision {
367+
status.Phase = revision.phase
317368
status.ReconcileDetail = &parametersv1alpha1.ReconcileDetail{
318-
CurrentRevision: revision.StrRevision,
319-
Policy: revision.Result.Policy,
320-
SucceedCount: revision.Result.SucceedCount,
321-
ExpectedCount: revision.Result.ExpectedCount,
322-
ExecResult: revision.Result.ExecResult,
323-
ErrMessage: revision.Result.Message,
369+
CurrentRevision: revision.strRevision,
370+
Policy: revision.result.Policy,
371+
SucceedCount: revision.result.SucceedCount,
372+
ExpectedCount: revision.result.ExpectedCount,
373+
ExecResult: revision.result.ExecResult,
374+
ErrMessage: revision.result.Message,
324375
}
325376
}
326377
}

0 commit comments

Comments
 (0)