Skip to content

Commit 9fd0e20

Browse files
authored
Merge pull request kubernetes#129345 from pohly/log-client-go-workqueue
client-go workqueue: add optional logger
2 parents 7820802 + f20eb2e commit 9fd0e20

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

pkg/controller/disruption/disruption.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,24 @@ func NewDisruptionControllerInternal(ctx context.Context,
182182
workqueue.DefaultTypedControllerRateLimiter[string](),
183183
workqueue.TypedRateLimitingQueueConfig[string]{
184184
DelayingQueue: workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[string]{
185-
Clock: clock,
186-
Name: "disruption",
185+
Logger: &logger,
186+
Clock: clock,
187+
Name: "disruption",
187188
}),
188189
},
189190
),
190191
recheckQueue: workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[string]{
191-
Clock: clock,
192-
Name: "disruption_recheck",
192+
Logger: &logger,
193+
Clock: clock,
194+
Name: "disruption_recheck",
193195
}),
194196
stalePodDisruptionQueue: workqueue.NewTypedRateLimitingQueueWithConfig(
195197
workqueue.DefaultTypedControllerRateLimiter[string](),
196198
workqueue.TypedRateLimitingQueueConfig[string]{
197199
DelayingQueue: workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[string]{
198-
Clock: clock,
199-
Name: "stale_pod_disruption",
200+
Logger: &logger,
201+
Clock: clock,
202+
Name: "stale_pod_disruption",
200203
}),
201204
},
202205
),

staging/src/k8s.io/client-go/util/workqueue/delaying_queue.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
25+
"k8s.io/klog/v2"
2526
"k8s.io/utils/clock"
2627
)
2728

@@ -46,6 +47,10 @@ type DelayingQueueConfig = TypedDelayingQueueConfig[any]
4647

4748
// TypedDelayingQueueConfig specifies optional configurations to customize a DelayingInterface.
4849
type TypedDelayingQueueConfig[T comparable] struct {
50+
// An optional logger. The name of the queue does *not* get added to it, this should
51+
// be done by the caller if desired.
52+
Logger *klog.Logger
53+
4954
// Name for the queue. If unnamed, the metrics will not be registered.
5055
Name string
5156

@@ -94,6 +99,10 @@ func TypedNewDelayingQueue[T comparable]() TypedDelayingInterface[T] {
9499
// NewTypedDelayingQueueWithConfig constructs a new workqueue with options to
95100
// customize different properties.
96101
func NewTypedDelayingQueueWithConfig[T comparable](config TypedDelayingQueueConfig[T]) TypedDelayingInterface[T] {
102+
logger := klog.Background()
103+
if config.Logger != nil {
104+
logger = *config.Logger
105+
}
97106
if config.Clock == nil {
98107
config.Clock = clock.RealClock{}
99108
}
@@ -106,7 +115,7 @@ func NewTypedDelayingQueueWithConfig[T comparable](config TypedDelayingQueueConf
106115
})
107116
}
108117

109-
return newDelayingQueue(config.Clock, config.Queue, config.Name, config.MetricsProvider)
118+
return newDelayingQueue(logger, config.Clock, config.Queue, config.Name, config.MetricsProvider)
110119
}
111120

112121
// NewDelayingQueueWithCustomQueue constructs a new workqueue with ability to
@@ -135,7 +144,7 @@ func NewDelayingQueueWithCustomClock(clock clock.WithTicker, name string) Delayi
135144
})
136145
}
137146

138-
func newDelayingQueue[T comparable](clock clock.WithTicker, q TypedInterface[T], name string, provider MetricsProvider) *delayingType[T] {
147+
func newDelayingQueue[T comparable](logger klog.Logger, clock clock.WithTicker, q TypedInterface[T], name string, provider MetricsProvider) *delayingType[T] {
139148
ret := &delayingType[T]{
140149
TypedInterface: q,
141150
clock: clock,
@@ -145,7 +154,7 @@ func newDelayingQueue[T comparable](clock clock.WithTicker, q TypedInterface[T],
145154
metrics: newRetryMetrics(name, provider),
146155
}
147156

148-
go ret.waitingLoop()
157+
go ret.waitingLoop(logger)
149158
return ret
150159
}
151160

@@ -264,8 +273,8 @@ func (q *delayingType[T]) AddAfter(item T, duration time.Duration) {
264273
const maxWait = 10 * time.Second
265274

266275
// waitingLoop runs until the workqueue is shutdown and keeps a check on the list of items to be added.
267-
func (q *delayingType[T]) waitingLoop() {
268-
defer utilruntime.HandleCrash()
276+
func (q *delayingType[T]) waitingLoop(logger klog.Logger) {
277+
defer utilruntime.HandleCrashWithLogger(logger)
269278

270279
// Make a placeholder channel to use when there are no items in our list
271280
never := make(<-chan time.Time)

staging/src/k8s.io/client-go/util/workqueue/parallelizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func ParallelizeUntil(ctx context.Context, workers, pieces int, doWorkPiece DoWo
7474
wg.Add(workers)
7575
for i := 0; i < workers; i++ {
7676
go func() {
77-
defer utilruntime.HandleCrash()
77+
defer utilruntime.HandleCrashWithContext(ctx)
7878
defer wg.Done()
7979
for chunk := range toProcess {
8080
start := chunk * chunkSize

0 commit comments

Comments
 (0)