Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reconfigure reconcile decouples cd/cv (#6609) #6629

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apis/apps/v1alpha1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ func (configuration *ConfigurationSpec) GetConfigurationItem(name string) *Confi
return nil
}

func (configuration *ConfigurationSpec) GetConfigSpec(configSpecName string) *ComponentConfigSpec {
if configItem := configuration.GetConfigurationItem(configSpecName); configItem != nil {
return configItem.ConfigSpec
}
return nil
}

func (status *ConfigurationStatus) GetItemStatus(name string) *ConfigurationItemDetailStatus {
for i := range status.ConfigurationItemStatus {
itemStatus := &status.ConfigurationItemStatus[i]
Expand Down
75 changes: 18 additions & 57 deletions controllers/apps/configuration/config_reconcile_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,49 @@ import (
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/controller/component"
configctrl "github.com/apecloud/kubeblocks/pkg/controller/configuration"
rsmcore "github.com/apecloud/kubeblocks/pkg/controller/rsm"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
"github.com/apecloud/kubeblocks/pkg/generics"
)

type configSpecList []appsv1alpha1.ComponentConfigSpec

type configReconcileContext struct {
configctrl.ResourceFetcher[configReconcileContext]

Name string
MatchingLabels client.MatchingLabels
ConfigSpec *appsv1alpha1.ComponentConfigSpec
ConfigMap *corev1.ConfigMap
Name string
MatchingLabels client.MatchingLabels
ConfigMap *corev1.ConfigMap
BuiltinComponent *component.SynthesizedComponent

Containers []string
StatefulSets []appv1.StatefulSet
RSMList []workloads.ReplicatedStateMachine
Deployments []appv1.Deployment

ConfigConstraint *appsv1alpha1.ConfigConstraint
reqCtx intctrlutil.RequestCtx
}

func newConfigReconcileContext(resourceCtx *configctrl.ResourceCtx,
cm *corev1.ConfigMap,
cc *appsv1alpha1.ConfigConstraint,
configSpecName string,
reqCtx intctrlutil.RequestCtx,
matchingLabels client.MatchingLabels) *configReconcileContext {
configContext := configReconcileContext{
ConfigMap: cm,
ConfigConstraint: cc,
Name: configSpecName,
MatchingLabels: matchingLabels,
reqCtx: reqCtx,
ConfigMap: cm,
Name: configSpecName,
MatchingLabels: matchingLabels,
}
return configContext.Init(resourceCtx, &configContext)
}

func (l configSpecList) findByName(name string) *appsv1alpha1.ComponentConfigSpec {
for i := range l {
configSpec := &l[i]
if configSpec.Name == name {
return configSpec
}
}
return nil
}

func (c *configReconcileContext) GetRelatedObjects() error {
return c.Cluster().
ClusterDef().
ClusterVer().
ClusterComponent().
RSM().
SynthesizedComponent().
Complete()
}

Expand Down Expand Up @@ -111,35 +98,9 @@ func (c *configReconcileContext) RSM() *configReconcileContext {
return c.Wrap(stsFn)
}

func (c *configReconcileContext) Complete() (err error) {
err = c.Err
if err != nil {
return
}

var configSpecs configSpecList
if configSpecs, err = cfgcore.GetConfigTemplatesFromComponent(
c.clusterComponents(),
c.clusterDefComponents(),
c.clusterVerComponents(),
c.ComponentName); err != nil {
return
}
c.ConfigSpec = configSpecs.findByName(c.Name)
return
}

func (c *configReconcileContext) clusterComponents() []appsv1alpha1.ClusterComponentSpec {
return c.ClusterObj.Spec.ComponentSpecs
}

func (c *configReconcileContext) clusterDefComponents() []appsv1alpha1.ClusterComponentDefinition {
return c.ClusterDefObj.Spec.ComponentDefs
}

func (c *configReconcileContext) clusterVerComponents() []appsv1alpha1.ClusterComponentVersion {
if c.ClusterVerObj == nil {
return nil
}
return c.ClusterVerObj.Spec.ComponentVersions
func (c *configReconcileContext) SynthesizedComponent() *configReconcileContext {
return c.Wrap(func() (err error) {
c.BuiltinComponent, err = component.BuildSynthesizedComponentWrapper(c.reqCtx, c.Client, c.ClusterObj, c.ClusterComObj)
return err
})
}
98 changes: 30 additions & 68 deletions controllers/apps/configuration/reconfigure_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ 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"
Expand All @@ -39,7 +37,6 @@ import (
cfgcm "github.com/apecloud/kubeblocks/pkg/configuration/config_manager"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
configctrl "github.com/apecloud/kubeblocks/pkg/controller/configuration"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
Expand All @@ -61,7 +58,7 @@ const (
configurationNotRelatedComponentMessage = "related component does not found any configSpecs, skip reconfigure"
)

var ConfigurationRequiredLabels = []string{
var reconfigureRequiredLabels = []string{
constant.AppNameLabelKey,
constant.AppInstanceLabelKey,
constant.KBAppComponentLabelKey,
Expand Down Expand Up @@ -113,23 +110,21 @@ func (r *ReconfigureReconciler) Reconcile(ctx context.Context, req ctrl.Request)
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],
resources, err := prepareRelatedResource(reqCtx, r.Client, config)
if err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "failed to fetch related resources")
}
tpl := &appsv1alpha1.ConfigConstraint{}
if err := r.Client.Get(reqCtx.Ctx, key, tpl); err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(config, r.Recorder, err, reqCtx.Log)
if resources.configSpec == nil {
reqCtx.Log.Info(fmt.Sprintf("not found configSpec[%s] in the component[%s].",
config.Labels[constant.CMConfigurationSpecProviderLabelKey], resources.componentName))
reqCtx.Recorder.Event(config,
corev1.EventTypeWarning,
appsv1alpha1.ReasonReconfigureFailed,
configurationNotRelatedComponentMessage)
return updateConfigPhase(r.Client, reqCtx, config, appsv1alpha1.CFinishedPhase, configurationNotRelatedComponentMessage)
}
return r.sync(reqCtx, config, tpl)

return r.sync(reqCtx, config, resources)
}

// SetupWithManager sets up the controller with the Manager.
Expand All @@ -141,35 +136,18 @@ func (r *ReconfigureReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func checkConfigurationObject(object client.Object) bool {
return checkConfigLabels(object, ConfigurationRequiredLabels)
return checkConfigLabels(object, reconfigureRequiredLabels)
}

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.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)
func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *corev1.ConfigMap, resources *reconfigureRelatedResource) (ctrl.Result, error) {
configPatch, forceRestart, err := createConfigPatch(configMap, resources.configConstraintObj.Spec.FormatterConfig, resources.configSpec.Keys)
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")
reqCtx.Recorder.Event(configMap, corev1.EventTypeNormal, appsv1alpha1.ReasonReconfigureRunning, "nothing changed, skip reconfigure")
return r.updateConfigCMStatus(reqCtx, configMap, core.ReconfigureNoChangeType, nil)
}

Expand All @@ -186,13 +164,13 @@ func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *c
Context: reqCtx.Ctx,
Client: r.Client,
Namespace: configMap.Namespace,
ClusterName: configMap.Labels[constant.AppInstanceLabelKey],
ComponentName: componentName,
ClusterName: resources.clusterName,
ComponentName: resources.componentName,
},
configMap,
configConstraint,
configSpecName,
componentLabels)
resources.configSpec.Name,
reqCtx,
resources.componentMatchLabels())
if err := reconcileContext.GetRelatedObjects(); err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(configMap, r.Recorder, err, reqCtx.Log)
}
Expand All @@ -202,34 +180,18 @@ func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *c
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 && len(reconcileContext.RSMList) == 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)
}

synthesizedComp, err := component.BuildSynthesizedComponentWrapper(reqCtx, r.Client, reconcileContext.ClusterObj, reconcileContext.ClusterComObj)
if err != nil {
reqCtx.Recorder.Eventf(configMap,
corev1.EventTypeWarning, appsv1alpha1.ReasonReconfigureFailed,
"build synthesized component failed, skip reconfigure")
if len(reconcileContext.StatefulSets) == 0 && len(reconcileContext.RSMList) == 0 {
reqCtx.Recorder.Event(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,
ConfigSpecName: resources.configSpec.Name,
ConfigPatch: configPatch,
ConfigMap: configMap,
ConfigConstraint: &configConstraint.Spec,
ConfigConstraint: &resources.configConstraintObj.Spec,
Client: r.Client,
Ctx: reqCtx,
Cluster: reconcileContext.ClusterObj,
Expand All @@ -238,8 +200,8 @@ func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *c
DeploymentUnits: reconcileContext.Deployments,
RSMList: reconcileContext.RSMList,
ClusterComponent: reconcileContext.ClusterComObj,
SynthesizedComponent: synthesizedComp,
Restart: forceRestart || !cfgcm.IsSupportReload(configConstraint.Spec.ReloadOptions),
SynthesizedComponent: reconcileContext.BuiltinComponent,
Restart: forceRestart || !cfgcm.IsSupportReload(resources.configConstraintObj.Spec.ReloadOptions),
ReconfigureClientFactory: GetClientFactory(),
})
}
Expand Down
Loading
Loading