Skip to content

Commit

Permalink
chore: remove the support for generated components (#8154)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Sep 20, 2024
1 parent a63502a commit 240bbf8
Show file tree
Hide file tree
Showing 32 changed files with 446 additions and 788 deletions.
4 changes: 2 additions & 2 deletions controllers/apps/component_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ var _ = Describe("Component Controller", func() {
Name: compObj.Name,
}
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
envVars, _ := buildEnvVarsNData(nil, targetEnvVars, false)
envVars, _ := buildEnvVarsNData(targetEnvVars)
targetEnvVarsMapping := map[string]corev1.EnvVar{}
for i, v := range envVars {
targetEnvVarsMapping[v.Name] = envVars[i]
Expand All @@ -1353,7 +1353,7 @@ var _ = Describe("Component Controller", func() {
Name: constant.GenerateClusterComponentEnvPattern(clusterObj.Name, compName),
}
Eventually(testapps.CheckObj(&testCtx, envCMKey, func(g Gomega, cm *corev1.ConfigMap) {
_, envData := buildEnvVarsNData(nil, targetEnvVars, false)
_, envData := buildEnvVarsNData(targetEnvVars)
for k, v := range envData {
Expect(cm.Data).Should(HaveKeyWithValue(k, v))
}
Expand Down
21 changes: 8 additions & 13 deletions controllers/apps/configuration/config_reconcile_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package configuration

import (
"context"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
"github.com/apecloud/kubeblocks/pkg/controller/component"
configctrl "github.com/apecloud/kubeblocks/pkg/controller/configuration"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
"github.com/apecloud/kubeblocks/pkg/generics"
)

type configReconcileContext struct {
configctrl.ResourceFetcher[configReconcileContext]

ctx context.Context
Name string
MatchingLabels client.MatchingLabels
ConfigMap *corev1.ConfigMap
BuiltinComponent *component.SynthesizedComponent

Containers []string
InstanceSetList []workloads.InstanceSet

reqCtx intctrlutil.RequestCtx
}

func newConfigReconcileContext(resourceCtx *configctrl.ResourceCtx,
func newConfigReconcileContext(ctx context.Context,
resourceCtx *configctrl.ResourceCtx,
cm *corev1.ConfigMap,
configSpecName string,
reqCtx intctrlutil.RequestCtx,
matchingLabels client.MatchingLabels) *configReconcileContext {
configContext := configReconcileContext{
reqCtx: reqCtx,
ctx: ctx,
ConfigMap: cm,
Name: configSpecName,
MatchingLabels: matchingLabels,
Expand Down Expand Up @@ -84,13 +84,8 @@ func (c *configReconcileContext) Workload() *configReconcileContext {

func (c *configReconcileContext) SynthesizedComponent() *configReconcileContext {
return c.Wrap(func() (err error) {
if c.ComponentDefObj != nil && c.ComponentObj != nil && len(c.ComponentObj.Spec.CompDef) > 0 {
// build synthesized component for native component
c.BuiltinComponent, err = component.BuildSynthesizedComponent(c.reqCtx, c.Client, c.ClusterObj, c.ComponentDefObj, c.ComponentObj)
} else {
// build synthesized component for generated component
c.BuiltinComponent, err = component.BuildSynthesizedComponentWrapper(c.reqCtx, c.Client, c.ClusterObj, c.ClusterComObj)
}
// build synthesized component for the component
c.BuiltinComponent, err = component.BuildSynthesizedComponent(c.ctx, c.Client, c.ComponentDefObj, c.ComponentObj, c.ClusterObj)
return err
})
}
22 changes: 8 additions & 14 deletions controllers/apps/configuration/configuration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if fetcherTask.ClusterComObj == nil || fetcherTask.ComponentObj == nil {
return r.failWithInvalidComponent(config, reqCtx)
}
if err := r.runTasks(TaskContext{config, reqCtx, fetcherTask}, tasks); err != nil {
if err := r.runTasks(TaskContext{config, ctx, fetcherTask}, tasks); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "failed to run configuration reconcile task.")
}
if !isAllReady(config) {
Expand Down Expand Up @@ -151,20 +151,14 @@ func (r *ConfigurationReconciler) runTasks(taskCtx TaskContext, tasks []Task) (e
var (
errs []error
synthesizedComp *component.SynthesizedComponent

ctx = taskCtx.reqCtx.Ctx
configuration = taskCtx.configuration
configuration = taskCtx.configuration
)

if len(taskCtx.fetcher.ComponentObj.Spec.CompDef) == 0 {
// build synthesized component for generated component
synthesizedComp, err = component.BuildSynthesizedComponentWrapper(taskCtx.reqCtx, r.Client, taskCtx.fetcher.ClusterObj, taskCtx.fetcher.ClusterComObj)
} else {
// build synthesized component for native component
synthesizedComp, err = component.BuildSynthesizedComponent(taskCtx.reqCtx, r.Client, taskCtx.fetcher.ClusterObj, taskCtx.fetcher.ComponentDefObj, taskCtx.fetcher.ComponentObj)
if err == nil {
err = buildTemplateVars(taskCtx.reqCtx.Ctx, r.Client, taskCtx.fetcher.ComponentDefObj, synthesizedComp)
}
// build synthesized component for the component
synthesizedComp, err = component.BuildSynthesizedComponent(taskCtx.ctx, r.Client,
taskCtx.fetcher.ComponentDefObj, taskCtx.fetcher.ComponentObj, taskCtx.fetcher.ClusterObj)
if err == nil {
err = buildTemplateVars(taskCtx.ctx, r.Client, taskCtx.fetcher.ComponentDefObj, synthesizedComp)
}
if err != nil {
return err
Expand All @@ -184,7 +178,7 @@ func (r *ConfigurationReconciler) runTasks(taskCtx TaskContext, tasks []Task) (e
if len(errs) > 0 {
configuration.Status.Message = utilerrors.NewAggregate(errs).Error()
}
if err := r.Client.Status().Patch(ctx, configuration, patch); err != nil {
if err := r.Client.Status().Patch(taskCtx.ctx, configuration, patch); err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
Expand Down
8 changes: 1 addition & 7 deletions controllers/apps/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
Expand All @@ -38,7 +37,6 @@ import (
"github.com/apecloud/kubeblocks/pkg/controller/builder"
"github.com/apecloud/kubeblocks/pkg/controller/component"
configctrl "github.com/apecloud/kubeblocks/pkg/controller/configuration"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
"github.com/apecloud/kubeblocks/pkg/generics"
testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"
)
Expand Down Expand Up @@ -147,11 +145,7 @@ func mockReconcileResource() (*corev1.ConfigMap, *appsv1beta1.ConfigConstraint,
AddAnnotations(core.GenerateTPLUniqLabelKeyWithConfig(configSpecName), configmap.Name).
Create(&testCtx).GetObject()

reqCtx := intctrlutil.RequestCtx{
Ctx: testCtx.Ctx,
Log: log.FromContext(testCtx.Ctx),
}
synthesizedComp, err := component.BuildSynthesizedComponent(reqCtx, testCtx.Cli, clusterObj, compDefObj, compObj)
synthesizedComp, err := component.BuildSynthesizedComponent(testCtx.Ctx, testCtx.Cli, compDefObj, compObj, clusterObj)
Expect(err).ShouldNot(HaveOccurred())

return configmap, constraint, clusterObj, compObj, synthesizedComp
Expand Down
3 changes: 2 additions & 1 deletion controllers/apps/configuration/reconcile_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package configuration

import (
"context"
"strconv"

corev1 "k8s.io/api/core/v1"
Expand All @@ -46,7 +47,7 @@ type Task struct {

type TaskContext struct {
configuration *appsv1alpha1.Configuration
reqCtx intctrlutil.RequestCtx
ctx context.Context
fetcher *Task
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/configuration/reconfigure_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *c
}

reconcileContext := newConfigReconcileContext(
reqCtx.Ctx,
&configctrl.ResourceCtx{
Context: reqCtx.Ctx,
Client: r.Client,
Expand All @@ -194,7 +195,6 @@ func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *c
},
configMap,
resources.configSpec.Name,
reqCtx,
resources.componentMatchLabels())
if err := reconcileContext.GetRelatedObjects(); err != nil {
return intctrlutil.RequeueWithErrorAndRecordEvent(configMap, r.Recorder, err, reqCtx.Log)
Expand Down
18 changes: 7 additions & 11 deletions controllers/apps/operations/rebuild_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package operations

import (
"context"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -96,7 +97,7 @@ func (r rebuildInstanceOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli cli
if err := cli.Get(reqCtx.Ctx, client.ObjectKey{Name: ins.Name, Namespace: opsRes.Cluster.Namespace}, targetPod); err != nil {
return err
}
synthesizedComp, err = r.buildSynthesizedComponent(reqCtx, cli, opsRes.Cluster, targetPod.Labels[constant.KBAppComponentLabelKey])
synthesizedComp, err = r.buildSynthesizedComponent(reqCtx.Ctx, cli, opsRes.Cluster, targetPod.Labels[constant.KBAppComponentLabelKey])
if err != nil {
return err
}
Expand Down Expand Up @@ -444,7 +445,7 @@ func (r rebuildInstanceOpsHandler) checkProgressForScalingOutPods(reqCtx intctrl
failedCount int
completedCount int
)
synthesizedComp, err := r.buildSynthesizedComponent(reqCtx, cli, opsRes.Cluster, rebuildInstance.ComponentName)
synthesizedComp, err := r.buildSynthesizedComponent(reqCtx.Ctx, cli, opsRes.Cluster, rebuildInstance.ComponentName)
if err != nil {
return 0, 0, nil, err
}
Expand Down Expand Up @@ -537,20 +538,15 @@ func (r rebuildInstanceOpsHandler) getScalingOutPodNameFromMessage(progressMsg s
return strings.Replace(strArr[0], scalingOutPodPrefixMsg+": ", "", 1)
}

func (r rebuildInstanceOpsHandler) buildSynthesizedComponent(reqCtx intctrlutil.RequestCtx,
func (r rebuildInstanceOpsHandler) buildSynthesizedComponent(ctx context.Context,
cli client.Client,
cluster *appsv1.Cluster,
componentName string) (*component.SynthesizedComponent, error) {
compSpec := getComponentSpecOrShardingTemplate(cluster, componentName)
if compSpec.ComponentDef == "" {
// TODO: remove after 0.9
return component.BuildSynthesizedComponentWrapper(reqCtx, cli, cluster, compSpec)
}
comp, compDef, err := component.GetCompNCompDefByName(reqCtx.Ctx, cli, cluster.Namespace, constant.GenerateClusterComponentName(cluster.Name, componentName))
comp, compDef, err := component.GetCompNCompDefByName(ctx, cli, cluster.Namespace, constant.GenerateClusterComponentName(cluster.Name, componentName))
if err != nil {
return nil, err
}
return component.BuildSynthesizedComponent(reqCtx, cli, cluster, compDef, comp)
return component.BuildSynthesizedComponent(ctx, cli, compDef, comp, cluster)
}

func (r rebuildInstanceOpsHandler) prepareInplaceRebuildHelper(reqCtx intctrlutil.RequestCtx,
Expand Down Expand Up @@ -590,7 +586,7 @@ func (r rebuildInstanceOpsHandler) prepareInplaceRebuildHelper(reqCtx intctrluti
if err = cli.Get(reqCtx.Ctx, client.ObjectKey{Name: instance.Name, Namespace: opsRes.Cluster.Namespace}, targetPod); err != nil {
return nil, err
}
synthesizedComp, err = r.buildSynthesizedComponent(reqCtx, cli, opsRes.Cluster, targetPod.Labels[constant.KBAppComponentLabelKey])
synthesizedComp, err = r.buildSynthesizedComponent(reqCtx.Ctx, cli, opsRes.Cluster, targetPod.Labels[constant.KBAppComponentLabelKey])
if err != nil {
return nil, err
}
Expand Down
26 changes: 11 additions & 15 deletions controllers/apps/operations/switchover.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package operations

import (
"context"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (r switchoverOpsHandler) ActionStartedCondition(reqCtx intctrlutil.RequestC
switchoverMessageMap := make(map[string]SwitchoverMessage)
for _, switchover := range opsRes.OpsRequest.Spec.SwitchoverList {
compSpec := opsRes.Cluster.Spec.GetComponentByName(switchover.ComponentName)
synthesizedComp, err := buildSynthesizedComp(reqCtx, cli, opsRes, compSpec)
synthesizedComp, err := buildSynthesizedComp(reqCtx.Ctx, cli, opsRes, compSpec)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,7 +134,7 @@ func doSwitchoverComponents(reqCtx intctrlutil.RequestCtx, cli client.Client, op
}
for _, switchover := range switchoverList {
compSpec := opsRes.Cluster.Spec.GetComponentByName(switchover.ComponentName)
synthesizedComp, err := buildSynthesizedComp(reqCtx, cli, opsRes, compSpec)
synthesizedComp, err := buildSynthesizedComp(reqCtx.Ctx, cli, opsRes, compSpec)
if err != nil {
return err
}
Expand Down Expand Up @@ -227,7 +228,7 @@ func handleSwitchoverProgress(reqCtx intctrlutil.RequestCtx, cli client.Client,
Message: fmt.Sprintf("waiting for component %s pod role label consistency after switchover", switchover.ComponentName),
}
compSpec := opsRes.Cluster.Spec.GetComponentByName(switchover.ComponentName)
synthesizedComp, errBuild := component.BuildSynthesizedComponentWrapper(reqCtx, cli, opsRes.Cluster, compSpec)
synthesizedComp, errBuild := buildSynthesizedComp(reqCtx.Ctx, cli, opsRes, compSpec)
if errBuild != nil {
checkRoleLabelProcessDetail.Message = fmt.Sprintf("handleSwitchoverProgress build synthesizedComponent %s failed", switchover.ComponentName)
checkRoleLabelProcessDetail.Status = appsv1alpha1.FailedProgressStatus
Expand Down Expand Up @@ -300,17 +301,12 @@ func setComponentSwitchoverProgressDetails(recorder record.EventRecorder,
}
}

// buildSynthesizedComp builds synthesized component for native component or generated component.
func buildSynthesizedComp(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource, clusterCompSpec *appsv1.ClusterComponentSpec) (*component.SynthesizedComponent, error) {
if len(clusterCompSpec.ComponentDef) > 0 {
compObj, compDefObj, err := component.GetCompNCompDefByName(reqCtx.Ctx, cli,
opsRes.Cluster.Namespace, constant.GenerateClusterComponentName(opsRes.Cluster.Name, clusterCompSpec.Name))
if err != nil {
return nil, err
}
// build synthesized component for native component
return component.BuildSynthesizedComponent(reqCtx, cli, opsRes.Cluster, compDefObj, compObj)
func buildSynthesizedComp(ctx context.Context, cli client.Client, opsRes *OpsResource, clusterCompSpec *appsv1.ClusterComponentSpec) (*component.SynthesizedComponent, error) {
compObj, compDefObj, err := component.GetCompNCompDefByName(ctx, cli,
opsRes.Cluster.Namespace, constant.GenerateClusterComponentName(opsRes.Cluster.Name, clusterCompSpec.Name))
if err != nil {
return nil, err
}
// build synthesized component for generated component
return component.BuildSynthesizedComponentWrapper(reqCtx, cli, opsRes.Cluster, clusterCompSpec)
// build synthesized component for the component
return component.BuildSynthesizedComponent(ctx, cli, compDefObj, compObj, opsRes.Cluster)
}
1 change: 0 additions & 1 deletion controllers/apps/transformer_cluster_component_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (t *clusterComponentStatusTransformer) reconcileComponentsStatus(transCtx *
if cluster.Status.Components == nil {
cluster.Status.Components = make(map[string]appsv1.ClusterComponentStatus)
}
// We cannot use cluster.status.components here because of simplified API generated component is not in it.
for _, compSpec := range transCtx.ComponentSpecs {
compKey := types.NamespacedName{
Namespace: cluster.Namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (t *componentAccountProvisionTransformer) Transform(ctx graph.TransformCont
}

lifecycleActions := transCtx.CompDef.Spec.LifecycleActions
if !component.IsGenerated(transCtx.Component) && (lifecycleActions == nil || lifecycleActions.AccountProvision == nil) {
if lifecycleActions == nil || lifecycleActions.AccountProvision == nil {
return nil
}

Expand Down
34 changes: 2 additions & 32 deletions controllers/apps/transformer_component_load_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/graph"
ictrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)

// componentLoadResourcesTransformer handles referenced resources validation and load them into context
Expand Down Expand Up @@ -56,31 +55,7 @@ func (t *componentLoadResourcesTransformer) Transform(ctx graph.TransformContext
}
transCtx.Cluster = cluster

if component.IsGenerated(transCtx.ComponentOrig) {
err = t.transformForGeneratedComponent(transCtx)
} else {
err = t.transformForNativeComponent(transCtx)
}
return err
}

func (t *componentLoadResourcesTransformer) transformForGeneratedComponent(transCtx *componentTransformContext) error {
reqCtx := ictrlutil.RequestCtx{
Ctx: transCtx.Context,
Log: transCtx.Logger,
Recorder: transCtx.EventRecorder,
}
comp := transCtx.Component

compDef, synthesizedComp, err := component.BuildSynthesizedComponent4Generated(reqCtx, transCtx.Client, transCtx.Cluster, comp)
if err != nil {
message := fmt.Sprintf("build synthesized component for %s failed: %s", comp.Name, err.Error())
return newRequeueError(requeueDuration, message)
}
transCtx.CompDef = compDef
transCtx.SynthesizeComponent = synthesizedComp

return nil
return t.transformForNativeComponent(transCtx)
}

func (t *componentLoadResourcesTransformer) transformForNativeComponent(transCtx *componentTransformContext) error {
Expand All @@ -98,12 +73,7 @@ func (t *componentLoadResourcesTransformer) transformForNativeComponent(transCtx
}
transCtx.CompDef = compDef

reqCtx := ictrlutil.RequestCtx{
Ctx: transCtx.Context,
Log: transCtx.Logger,
Recorder: transCtx.EventRecorder,
}
synthesizedComp, err := component.BuildSynthesizedComponent(reqCtx, transCtx.Client, transCtx.Cluster, compDef, comp)
synthesizedComp, err := component.BuildSynthesizedComponent(ctx, transCtx.Client, compDef, comp, transCtx.Cluster)
if err != nil {
message := fmt.Sprintf("build synthesized component for %s failed: %s", comp.Name, err.Error())
return newRequeueError(requeueDuration, message)
Expand Down
7 changes: 1 addition & 6 deletions controllers/apps/transformer_component_pre_terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,7 @@ func (t *componentPreTerminateTransformer) synthesizedComponent(transCtx *compon
return nil, newRequeueError(requeueDuration, err.Error())
}

reqCtx := intctrlutil.RequestCtx{
Ctx: ctx,
Log: transCtx.Logger,
Recorder: transCtx.EventRecorder,
}
synthesizedComp, err := component.BuildSynthesizedComponent(reqCtx, cli, cluster, compDef, comp)
synthesizedComp, err := component.BuildSynthesizedComponent(ctx, cli, compDef, comp, cluster)
if err != nil {
return nil, newRequeueError(requeueDuration,
fmt.Sprintf("build synthesized component failed at pre-terminate transformer: %s", err.Error()))
Expand Down
Loading

0 comments on commit 240bbf8

Please sign in to comment.