|
| 1 | +/* |
| 2 | +Copyright 2022 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + "k8s.io/client-go/util/workqueue" |
| 24 | + "k8s.io/klog/v2" |
| 25 | + |
| 26 | + config "github.com/karmada-io/karmada/pkg/controllers/cluster/evictionqueue_config" |
| 27 | + "github.com/karmada-io/karmada/pkg/metrics" |
| 28 | + "github.com/karmada-io/karmada/pkg/sharedcli/ratelimiterflag" |
| 29 | + "github.com/karmada-io/karmada/pkg/util" |
| 30 | + "github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager" |
| 31 | +) |
| 32 | + |
| 33 | +// EvictionWorker enhances AsyncWorker with dynamic rate limiting and metrics |
| 34 | +// for eviction operations. It provides a queue that adjusts its processing rate |
| 35 | +// based on cluster health status. |
| 36 | +type EvictionWorker interface { |
| 37 | + util.AsyncWorker |
| 38 | +} |
| 39 | + |
| 40 | +type evictionWorker struct { |
| 41 | + name string |
| 42 | + keyFunc util.KeyFunc |
| 43 | + reconcileFunc util.ReconcileFunc |
| 44 | + resourceKindFunc func(key interface{}) (clusterName, resourceKind string) |
| 45 | + queue workqueue.TypedRateLimitingInterface[any] |
| 46 | +} |
| 47 | + |
| 48 | +// EvictionWorkerOptions configures a new EvictionWorker instance. |
| 49 | +type EvictionWorkerOptions struct { |
| 50 | + // Name is the queue's name used for metrics and logging |
| 51 | + Name string |
| 52 | + |
| 53 | + // KeyFunc generates keys from objects for queue operations |
| 54 | + KeyFunc util.KeyFunc |
| 55 | + |
| 56 | + // ReconcileFunc processes keys from the queue |
| 57 | + ReconcileFunc util.ReconcileFunc |
| 58 | + |
| 59 | + // ResourceKindFunc returns resource metadata for metrics collection |
| 60 | + ResourceKindFunc func(key interface{}) (clusterName, resourceKind string) |
| 61 | + |
| 62 | + // InformerManager provides cluster information for dynamic rate limiting |
| 63 | + InformerManager genericmanager.SingleClusterInformerManager |
| 64 | + |
| 65 | + // EvictionQueueOptions configures dynamic rate limiting behavior |
| 66 | + EvictionQueueOptions config.EvictionQueueOptions |
| 67 | + |
| 68 | + // RateLimiterOptions configures general rate limiter behavior |
| 69 | + RateLimiterOptions ratelimiterflag.Options |
| 70 | +} |
| 71 | + |
| 72 | +// NewEvictionWorker creates a new EvictionWorker with dynamic rate limiting. |
| 73 | +func NewEvictionWorker(opts EvictionWorkerOptions) EvictionWorker { |
| 74 | + rateLimiter := NewGracefulEvictionRateLimiter[interface{}]( |
| 75 | + opts.InformerManager, |
| 76 | + opts.EvictionQueueOptions, |
| 77 | + opts.RateLimiterOptions, |
| 78 | + ) |
| 79 | + |
| 80 | + return &evictionWorker{ |
| 81 | + name: opts.Name, |
| 82 | + keyFunc: opts.KeyFunc, |
| 83 | + reconcileFunc: opts.ReconcileFunc, |
| 84 | + resourceKindFunc: opts.ResourceKindFunc, |
| 85 | + queue: workqueue.NewRateLimitingQueueWithConfig(rateLimiter, workqueue.RateLimitingQueueConfig{ |
| 86 | + Name: opts.Name, |
| 87 | + }), |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Enqueue converts an object to a key and adds it to the queue. |
| 92 | +func (w *evictionWorker) Enqueue(obj interface{}) { |
| 93 | + key, err := w.keyFunc(obj) |
| 94 | + if err != nil { |
| 95 | + klog.Errorf("Failed to generate key for obj: %+v, err: %v", obj, err) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + if key == nil { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + w.Add(key) |
| 104 | +} |
| 105 | + |
| 106 | +// Add puts an item into the queue and updates metrics. |
| 107 | +func (w *evictionWorker) Add(item interface{}) { |
| 108 | + if item == nil { |
| 109 | + klog.Warningf("Ignore nil item from queue") |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + w.queue.Add(item) |
| 114 | + metrics.RecordEvictionQueueMetrics(w.name, float64(w.queue.Len())) |
| 115 | + |
| 116 | + // Update resource kind metrics if possible |
| 117 | + if w.resourceKindFunc != nil { |
| 118 | + clusterName, resourceKind := w.resourceKindFunc(item) |
| 119 | + metrics.RecordEvictionKindMetrics(clusterName, resourceKind, true) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// AddAfter adds an item to the queue after a delay and updates metrics. |
| 124 | +func (w *evictionWorker) AddAfter(item interface{}, duration time.Duration) { |
| 125 | + if item == nil { |
| 126 | + klog.Warningf("Ignore nil item from queue") |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + w.queue.AddAfter(item, duration) |
| 131 | + metrics.RecordEvictionQueueMetrics(w.name, float64(w.queue.Len())) |
| 132 | + |
| 133 | + // Update resource kind metrics if possible |
| 134 | + if w.resourceKindFunc != nil { |
| 135 | + clusterName, resourceKind := w.resourceKindFunc(item) |
| 136 | + metrics.RecordEvictionKindMetrics(clusterName, resourceKind, true) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// worker processes items from the queue until the context is canceled. |
| 141 | +func (w *evictionWorker) worker(ctx context.Context) { |
| 142 | + for w.processNextWorkItem(ctx) { |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// processNextWorkItem handles a single item from the queue with metrics tracking. |
| 147 | +// Returns false when the queue is shutting down, true otherwise. |
| 148 | +func (w *evictionWorker) processNextWorkItem(ctx context.Context) bool { |
| 149 | + key, quit := w.queue.Get() |
| 150 | + if quit { |
| 151 | + return false |
| 152 | + } |
| 153 | + defer w.queue.Done(key) |
| 154 | + |
| 155 | + // Update queue metrics |
| 156 | + metrics.RecordEvictionQueueMetrics(w.name, float64(w.queue.Len())) |
| 157 | + |
| 158 | + // Get resource metadata for metrics |
| 159 | + var clusterName, resourceKind string |
| 160 | + if w.resourceKindFunc != nil { |
| 161 | + clusterName, resourceKind = w.resourceKindFunc(key) |
| 162 | + } |
| 163 | + |
| 164 | + // Process the item and measure latency |
| 165 | + startTime := time.Now() |
| 166 | + err := w.reconcileFunc(key) |
| 167 | + metrics.RecordEvictionProcessingMetrics(w.name, err, startTime) |
| 168 | + |
| 169 | + if err != nil { |
| 170 | + // Requeue with rate limiting on error |
| 171 | + w.queue.AddRateLimited(key) |
| 172 | + // Item remains in queue, so don't decrease metrics count |
| 173 | + return true |
| 174 | + } |
| 175 | + |
| 176 | + // Successfully processed |
| 177 | + w.queue.Forget(key) |
| 178 | + |
| 179 | + // Decrease resource kind count only after successful processing |
| 180 | + metrics.RecordEvictionKindMetrics(clusterName, resourceKind, false) |
| 181 | + |
| 182 | + return true |
| 183 | +} |
| 184 | + |
| 185 | +// Run starts worker goroutines and ensures cleanup when context is canceled. |
| 186 | +func (w *evictionWorker) Run(ctx context.Context, workerNumber int) { |
| 187 | + klog.Infof("Starting %d workers for eviction worker %s", workerNumber, w.name) |
| 188 | + for i := 0; i < workerNumber; i++ { |
| 189 | + go w.worker(ctx) |
| 190 | + } |
| 191 | + |
| 192 | + // Clean up when context is canceled |
| 193 | + go func() { |
| 194 | + <-ctx.Done() |
| 195 | + klog.Infof("Shutting down eviction worker %s", w.name) |
| 196 | + w.queue.ShutDown() |
| 197 | + }() |
| 198 | +} |
0 commit comments