|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package scheduler |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "reflect" |
| 20 | + |
| 21 | + "k8s.io/apimachinery/pkg/types" |
| 22 | + "k8s.io/client-go/util/workqueue" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 26 | + |
| 27 | + "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" |
| 28 | + "github.com/pingcap/tidb-operator/pkg/client" |
| 29 | +) |
| 30 | + |
| 31 | +func (r *Reconciler) ClusterEventHandler() handler.TypedEventHandler[client.Object, reconcile.Request] { |
| 32 | + return handler.TypedFuncs[client.Object, reconcile.Request]{ |
| 33 | + UpdateFunc: func(ctx context.Context, event event.TypedUpdateEvent[client.Object], |
| 34 | + queue workqueue.TypedRateLimitingInterface[reconcile.Request], |
| 35 | + ) { |
| 36 | + oldObj := event.ObjectOld.(*v1alpha1.Cluster) |
| 37 | + newObj := event.ObjectNew.(*v1alpha1.Cluster) |
| 38 | + |
| 39 | + if newObj.Status.PD != oldObj.Status.PD { |
| 40 | + r.Logger.Info("pd url is updating", "from", oldObj.Status.PD, "to", newObj.Status.PD) |
| 41 | + } else if !reflect.DeepEqual(oldObj.Spec.SuspendAction, newObj.Spec.SuspendAction) { |
| 42 | + r.Logger.Info("suspend action is updating", "from", oldObj.Spec.SuspendAction, "to", newObj.Spec.SuspendAction) |
| 43 | + } else if oldObj.Spec.Paused != newObj.Spec.Paused { |
| 44 | + r.Logger.Info("cluster paused is updating", "from", oldObj.Spec.Paused, "to", newObj.Spec.Paused) |
| 45 | + } else { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + var sl v1alpha1.SchedulerList |
| 50 | + if err := r.Client.List(ctx, &sl, client.MatchingLabels{ |
| 51 | + v1alpha1.LabelKeyManagedBy: v1alpha1.LabelValManagedByOperator, |
| 52 | + v1alpha1.LabelKeyCluster: newObj.Name, |
| 53 | + v1alpha1.LabelKeyComponent: v1alpha1.LabelValComponentScheduler, |
| 54 | + }, client.InNamespace(newObj.Namespace)); err != nil { |
| 55 | + r.Logger.Error(err, "cannot list all scheduler instances", "ns", newObj.Namespace, "cluster", newObj.Name) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + for i := range sl.Items { |
| 60 | + scheduler := &sl.Items[i] |
| 61 | + queue.Add(reconcile.Request{ |
| 62 | + NamespacedName: types.NamespacedName{ |
| 63 | + Name: scheduler.Name, |
| 64 | + Namespace: scheduler.Namespace, |
| 65 | + }, |
| 66 | + }) |
| 67 | + } |
| 68 | + }, |
| 69 | + DeleteFunc: func(ctx context.Context, event event.TypedDeleteEvent[client.Object], |
| 70 | + queue workqueue.TypedRateLimitingInterface[reconcile.Request], |
| 71 | + ) { |
| 72 | + obj := event.Object.(*v1alpha1.Cluster) |
| 73 | + |
| 74 | + r.Logger.Info("enqueue all scheduler instances because of the cluster is deleting", "ns", obj.Namespace, "cluster", obj.Name) |
| 75 | + |
| 76 | + var sl v1alpha1.SchedulerList |
| 77 | + if err := r.Client.List(ctx, &sl, client.MatchingLabels{ |
| 78 | + v1alpha1.LabelKeyManagedBy: v1alpha1.LabelValManagedByOperator, |
| 79 | + v1alpha1.LabelKeyCluster: obj.Name, |
| 80 | + v1alpha1.LabelKeyComponent: v1alpha1.LabelValComponentScheduler, |
| 81 | + }, client.InNamespace(obj.Namespace)); err != nil { |
| 82 | + r.Logger.Error(err, "cannot list all scheduler instances", "ns", obj.Namespace, "cluster", obj.Name) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + for i := range sl.Items { |
| 87 | + scheduler := &sl.Items[i] |
| 88 | + queue.Add(reconcile.Request{ |
| 89 | + NamespacedName: types.NamespacedName{ |
| 90 | + Name: scheduler.Name, |
| 91 | + Namespace: scheduler.Namespace, |
| 92 | + }, |
| 93 | + }) |
| 94 | + } |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
0 commit comments