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: notify dependent components when a component updated #8818

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions controllers/apps/component/component_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func (r *ComponentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
&componentPostProvisionTransformer{},
// update component status
&componentStatusTransformer{Client: r.Client},
// notify dependent components the possible spec changes
&componentNotifierTransformer{},
).Build()

// Execute stage
Expand Down
32 changes: 32 additions & 0 deletions controllers/apps/component/transformer_component_deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -140,6 +142,9 @@ func (t *componentDeletionTransformer) deleteCompResources(transCtx *componentTr
graphCli.Status(dag, comp, transCtx.Component)
return intctrlutil.NewRequeueError(time.Second*1, "not all component sub-resources deleted")
} else {
if err = notifyDependents4CompDeletion(transCtx, dag); err != nil {
return intctrlutil.NewRequeueError(appsutil.RequeueDuration, fmt.Sprintf("notify dependent components error: %s", err.Error()))
}
graphCli.Delete(dag, comp)
}

Expand Down Expand Up @@ -211,6 +216,33 @@ func handleRBACResourceDeletion(obj client.Object, transCtx *componentTransformC
}
}

func notifyDependents4CompDeletion(transCtx *componentTransformContext, dag *graph.DAG) error {
var (
ctx = transCtx.Context
cli = transCtx.Client
comp = transCtx.Component
)

compDef := &appsv1.ComponentDefinition{}
if err := transCtx.Client.Get(transCtx.Context, types.NamespacedName{Name: comp.Spec.CompDef}, compDef); err != nil {
return errors.Wrap(err, "failed to get the component definition for dependents notifier at deletion")
}

synthesizedComp, err := component.BuildSynthesizedComponent(ctx, cli, compDef, comp)
if err != nil {
return errors.Wrap(err, "failed to build synthesized component for dependents notifier at deletion")
}

bak := transCtx.SynthesizeComponent
defer func() {
transCtx.SynthesizeComponent = bak
}()

transCtx.SynthesizeComponent = synthesizedComp
transformer := &componentNotifierTransformer{}
return transformer.Transform(transCtx, dag)
}

func compOwnedWorkloadKinds() []client.ObjectList {
return []client.ObjectList{
&workloads.InstanceSetList{},
Expand Down
143 changes: 143 additions & 0 deletions controllers/apps/component/transformer_component_notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright (C) 2022-2025 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 component

import (
"fmt"
"time"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/graph"
"github.com/apecloud/kubeblocks/pkg/controller/model"
)

type componentNotifierTransformer struct{}

var _ graph.Transformer = &componentNotifierTransformer{}

func (t *componentNotifierTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
transCtx, _ := ctx.(*componentTransformContext)
if !model.IsObjectUpdating(transCtx.ComponentOrig) {
return nil
}

dependents, err := t.dependents(transCtx)
if err != nil {
return err
}

graphCli, _ := transCtx.Client.(model.GraphClient)
for _, compName := range dependents {
if err = t.notify(transCtx, graphCli, dag, compName); err != nil {
return err
}
}
return nil
}

func (t *componentNotifierTransformer) dependents(transCtx *componentTransformContext) ([]string, error) {
synthesizedComp := transCtx.SynthesizeComponent
dependents := make([]string, 0)
for compName, compDefName := range synthesizedComp.Comp2CompDefs {
if compName == synthesizedComp.Name {
continue // skip self
}
depended, err := t.depended(transCtx, compDefName)
if err != nil {
return nil, err
}
if depended {
dependents = append(dependents, compName)
}
}
return dependents, nil
}

func (t *componentNotifierTransformer) depended(transCtx *componentTransformContext, compDefName string) (bool, error) {
compDefReferenced := func(v appsv1.EnvVar) string {
if v.ValueFrom != nil {
if v.ValueFrom.HostNetworkVarRef != nil {
return v.ValueFrom.HostNetworkVarRef.CompDef
}
if v.ValueFrom.ServiceVarRef != nil {
return v.ValueFrom.ServiceVarRef.CompDef
}
if v.ValueFrom.CredentialVarRef != nil {
return v.ValueFrom.CredentialVarRef.CompDef
}
if v.ValueFrom.TLSVarRef != nil {
return v.ValueFrom.TLSVarRef.CompDef
}
if v.ValueFrom.ServiceRefVarRef != nil {
return v.ValueFrom.ServiceRefVarRef.CompDef
}
if v.ValueFrom.ComponentVarRef != nil {
return v.ValueFrom.ComponentVarRef.CompDef
}
}
return ""
}

compDef, err := getNCheckCompDefinition(transCtx.Context, transCtx.Client, compDefName)
if err != nil {
return false, err
}

synthesizedComp := transCtx.SynthesizeComponent
for _, v := range compDef.Spec.Vars {
compDefPattern := compDefReferenced(v)
if len(compDefPattern) > 0 {
if component.PrefixOrRegexMatched(synthesizedComp.CompDefName, compDefPattern) {
return true, nil
}
}
}
return false, nil
}

func (t *componentNotifierTransformer) notify(transCtx *componentTransformContext,
graphCli model.GraphClient, dag *graph.DAG, compName string) error {
synthesizedComp := transCtx.SynthesizeComponent

comp := &appsv1.Component{}
compKey := types.NamespacedName{
Namespace: synthesizedComp.Namespace,
Name: constant.GenerateClusterComponentName(synthesizedComp.ClusterName, compName),
}
if err := transCtx.Client.Get(transCtx.Context, compKey, comp); err != nil {
return client.IgnoreNotFound(err)
}

compCopy := comp.DeepCopy()
if compCopy.Annotations == nil {
compCopy.Annotations = make(map[string]string)
}
compCopy.Annotations[constant.ReconcileAnnotationKey] =
fmt.Sprintf("%s@%s", synthesizedComp.Name, time.Now().Format(time.RFC3339))

graphCli.Patch(dag, comp, compCopy)

return nil
}
Loading
Loading