|
| 1 | +/* |
| 2 | +Copyright (C) 2022-2025 ApeCloud Co., Ltd |
| 3 | +
|
| 4 | +This file is part of KubeBlocks project |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU Affero General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU Affero General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU Affero General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +package component |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "slices" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/go-logr/logr" |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + |
| 32 | + workloads "github.com/apecloud/kubeblocks/apis/workloads/v1" |
| 33 | + "github.com/apecloud/kubeblocks/pkg/constant" |
| 34 | + intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil" |
| 35 | + "github.com/apecloud/kubeblocks/pkg/kbagent" |
| 36 | + "github.com/apecloud/kubeblocks/pkg/kbagent/proto" |
| 37 | +) |
| 38 | + |
| 39 | +const ( |
| 40 | + // new replicas task & event |
| 41 | + newReplicaTask = "newReplica" |
| 42 | + defaultNewReplicaTaskReportPeriodSeconds = 60 |
| 43 | +) |
| 44 | + |
| 45 | +func NewReplicaTask(compName, uid string, source *corev1.Pod, replicas []string) (map[string]string, error) { |
| 46 | + port, err := intctrlutil.GetPortByName(*source, kbagent.ContainerName, kbagent.DefaultStreamingPortName) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + task := proto.Task{ |
| 51 | + Instance: compName, |
| 52 | + Task: newReplicaTask, |
| 53 | + UID: uid, |
| 54 | + Replicas: strings.Join(replicas, ","), |
| 55 | + NotifyAtFinish: true, |
| 56 | + ReportPeriodSeconds: defaultNewReplicaTaskReportPeriodSeconds, |
| 57 | + NewReplica: &proto.NewReplicaTask{ |
| 58 | + Remote: intctrlutil.PodFQDN(source.Namespace, compName, source.Name), |
| 59 | + Port: port, |
| 60 | + Replicas: strings.Join(replicas, ","), |
| 61 | + }, |
| 62 | + } |
| 63 | + return buildKBAgentTaskEnv(task) |
| 64 | +} |
| 65 | + |
| 66 | +func handleNewReplicaTaskEvent(logger logr.Logger, ctx context.Context, cli client.Client, namespace string, event proto.TaskEvent) error { |
| 67 | + key := types.NamespacedName{ |
| 68 | + Namespace: namespace, |
| 69 | + Name: event.Instance, |
| 70 | + } |
| 71 | + its := &workloads.InstanceSet{} |
| 72 | + if err := cli.Get(ctx, key, its); err != nil { |
| 73 | + logger.Error(err, "get ITS failed when handle new replica task event", |
| 74 | + "code", event.Code, "finished", !event.EndTime.IsZero(), "message", event.Message) |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + var err error |
| 79 | + finished := !event.EndTime.IsZero() |
| 80 | + switch { |
| 81 | + case finished && event.Code == 0: |
| 82 | + err = handleNewReplicaTaskEvent4Finished(ctx, cli, its, event) |
| 83 | + case finished: |
| 84 | + err = handleNewReplicaTaskEvent4Failed(ctx, cli, its, event) |
| 85 | + default: |
| 86 | + err = handleNewReplicaTaskEvent4Unfinished(ctx, cli, its, event) |
| 87 | + } |
| 88 | + if err != nil { |
| 89 | + logger.Error(err, "handle new replica task event failed", |
| 90 | + "code", event.Code, "finished", finished, "message", event.Message) |
| 91 | + } else { |
| 92 | + logger.Info("handle new replica task event success", |
| 93 | + "code", event.Code, "finished", finished, "message", event.Message) |
| 94 | + } |
| 95 | + return err |
| 96 | +} |
| 97 | + |
| 98 | +func handleNewReplicaTaskEvent4Finished(ctx context.Context, cli client.Client, its *workloads.InstanceSet, event proto.TaskEvent) error { |
| 99 | + if err := func() error { |
| 100 | + envKey := types.NamespacedName{ |
| 101 | + Namespace: its.Namespace, |
| 102 | + Name: constant.GetCompEnvCMName(its.Name), |
| 103 | + } |
| 104 | + obj := &corev1.ConfigMap{} |
| 105 | + err := cli.Get(ctx, envKey, obj) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + parameters, err := updateKBAgentTaskEnv(obj.Data, func(task proto.Task) *proto.Task { |
| 111 | + if task.Task == newReplicaTask { |
| 112 | + replicas := strings.Split(task.Replicas, ",") |
| 113 | + replicas = slices.DeleteFunc(replicas, func(r string) bool { |
| 114 | + return r == event.Replica |
| 115 | + }) |
| 116 | + if len(replicas) == 0 { |
| 117 | + return nil |
| 118 | + } |
| 119 | + task.Replicas = strings.Join(replicas, ",") |
| 120 | + if task.NewReplica != nil { |
| 121 | + task.NewReplica.Replicas = task.Replicas |
| 122 | + } |
| 123 | + } |
| 124 | + return &task |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + if parameters == nil { |
| 130 | + return nil // do nothing |
| 131 | + } |
| 132 | + |
| 133 | + if obj.Data == nil { |
| 134 | + obj.Data = make(map[string]string) |
| 135 | + } |
| 136 | + for k, v := range parameters { |
| 137 | + obj.Data[k] = v |
| 138 | + } |
| 139 | + return cli.Update(ctx, obj) |
| 140 | + }(); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + // TODO: mark data loaded |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func handleNewReplicaTaskEvent4Unfinished(ctx context.Context, cli client.Client, its *workloads.InstanceSet, event proto.TaskEvent) error { |
| 150 | + // TODO: do nothing, log the event.Message |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func handleNewReplicaTaskEvent4Failed(ctx context.Context, cli client.Client, its *workloads.InstanceSet, event proto.TaskEvent) error { |
| 155 | + // TODO: do nothing, log the event.Message |
| 156 | + return nil |
| 157 | +} |
0 commit comments