@@ -22,6 +22,7 @@ package parameters
2222import (
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
4647type 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,
292343func 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