@@ -61,7 +61,7 @@ type ClusterStateFeeder interface {
61
61
InitFromHistoryProvider (historyProvider history.HistoryProvider )
62
62
63
63
// InitFromCheckpoints loads historical checkpoints into clusterState.
64
- InitFromCheckpoints ()
64
+ InitFromCheckpoints (ctx context. Context )
65
65
66
66
// LoadVPAs updates clusterState with current state of VPAs.
67
67
LoadVPAs (ctx context.Context )
@@ -70,10 +70,10 @@ type ClusterStateFeeder interface {
70
70
LoadPods ()
71
71
72
72
// LoadRealTimeMetrics updates clusterState with current usage metrics of containers.
73
- LoadRealTimeMetrics ()
73
+ LoadRealTimeMetrics (ctx context. Context )
74
74
75
75
// GarbageCollectCheckpoints removes historical checkpoints that don't have a matching VPA.
76
- GarbageCollectCheckpoints ()
76
+ GarbageCollectCheckpoints (ctx context. Context )
77
77
}
78
78
79
79
// ClusterStateFeederFactory makes instances of ClusterStateFeeder.
@@ -113,26 +113,32 @@ func (m ClusterStateFeederFactory) Make() *clusterStateFeeder {
113
113
}
114
114
115
115
// WatchEvictionEventsWithRetries watches new Events with reason=Evicted and passes them to the observer.
116
- func WatchEvictionEventsWithRetries (kubeClient kube_client.Interface , observer oom.Observer , namespace string ) {
116
+ func WatchEvictionEventsWithRetries (ctx context. Context , kubeClient kube_client.Interface , observer oom.Observer , namespace string ) {
117
117
go func () {
118
118
options := metav1.ListOptions {
119
119
FieldSelector : "reason=Evicted" ,
120
120
}
121
121
122
122
watchEvictionEventsOnce := func () {
123
- watchInterface , err := kubeClient .CoreV1 ().Events (namespace ).Watch (context . TODO () , options )
123
+ watchInterface , err := kubeClient .CoreV1 ().Events (namespace ).Watch (ctx , options )
124
124
if err != nil {
125
125
klog .ErrorS (err , "Cannot initialize watching events" )
126
126
return
127
127
}
128
+ defer watchInterface .Stop ()
128
129
watchEvictionEvents (watchInterface .ResultChan (), observer )
129
130
}
130
131
for {
131
- watchEvictionEventsOnce ()
132
- // Wait between attempts, retrying too often breaks API server.
133
- waitTime := wait .Jitter (evictionWatchRetryWait , evictionWatchJitterFactor )
134
- klog .V (1 ).InfoS ("An attempt to watch eviction events finished" , "waitTime" , waitTime )
135
- time .Sleep (waitTime )
132
+ select {
133
+ case <- ctx .Done ():
134
+ return
135
+ default :
136
+ watchEvictionEventsOnce ()
137
+ // Wait between attempts, retrying too often breaks API server.
138
+ waitTime := wait .Jitter (evictionWatchRetryWait , evictionWatchJitterFactor )
139
+ klog .V (1 ).InfoS ("An attempt to watch eviction events finished" , "waitTime" , waitTime )
140
+ time .Sleep (waitTime )
141
+ }
136
142
}
137
143
}()
138
144
}
@@ -188,10 +194,10 @@ func newPodClients(kubeClient kube_client.Interface, resourceEventHandler cache.
188
194
}
189
195
190
196
// NewPodListerAndOOMObserver creates pair of pod lister and OOM observer.
191
- func NewPodListerAndOOMObserver (kubeClient kube_client.Interface , namespace string , stopCh <- chan struct {}) (v1lister.PodLister , oom.Observer ) {
197
+ func NewPodListerAndOOMObserver (ctx context. Context , kubeClient kube_client.Interface , namespace string , stopCh <- chan struct {}) (v1lister.PodLister , oom.Observer ) {
192
198
oomObserver := oom .NewObserver ()
193
199
podLister := newPodClients (kubeClient , oomObserver , namespace , stopCh )
194
- WatchEvictionEventsWithRetries (kubeClient , oomObserver , namespace )
200
+ WatchEvictionEventsWithRetries (ctx , kubeClient , oomObserver , namespace )
195
201
return podLister , oomObserver
196
202
}
197
203
@@ -258,9 +264,9 @@ func (feeder *clusterStateFeeder) setVpaCheckpoint(checkpoint *vpa_types.Vertica
258
264
return nil
259
265
}
260
266
261
- func (feeder * clusterStateFeeder ) InitFromCheckpoints () {
267
+ func (feeder * clusterStateFeeder ) InitFromCheckpoints (ctx context. Context ) {
262
268
klog .V (3 ).InfoS ("Initializing VPA from checkpoints" )
263
- feeder .LoadVPAs (context . TODO () )
269
+ feeder .LoadVPAs (ctx )
264
270
265
271
namespaces := make (map [string ]bool )
266
272
for _ , v := range feeder .clusterState .VPAs () {
@@ -269,7 +275,7 @@ func (feeder *clusterStateFeeder) InitFromCheckpoints() {
269
275
270
276
for namespace := range namespaces {
271
277
klog .V (3 ).InfoS ("Fetching checkpoints" , "namespace" , namespace )
272
- checkpointList , err := feeder .vpaCheckpointClient .VerticalPodAutoscalerCheckpoints (namespace ).List (context . TODO () , metav1.ListOptions {})
278
+ checkpointList , err := feeder .vpaCheckpointClient .VerticalPodAutoscalerCheckpoints (namespace ).List (ctx , metav1.ListOptions {})
273
279
if err != nil {
274
280
klog .ErrorS (err , "Cannot list VPA checkpoints" , "namespace" , namespace )
275
281
}
@@ -285,7 +291,7 @@ func (feeder *clusterStateFeeder) InitFromCheckpoints() {
285
291
}
286
292
}
287
293
288
- func (feeder * clusterStateFeeder ) GarbageCollectCheckpoints () {
294
+ func (feeder * clusterStateFeeder ) GarbageCollectCheckpoints (ctx context. Context ) {
289
295
klog .V (3 ).InfoS ("Starting garbage collection of checkpoints" )
290
296
291
297
allVPAKeys := map [model.VpaID ]bool {}
@@ -303,7 +309,7 @@ func (feeder *clusterStateFeeder) GarbageCollectCheckpoints() {
303
309
allVPAKeys [vpaID ] = true
304
310
}
305
311
306
- namespaceList , err := feeder .coreClient .Namespaces ().List (context . TODO () , metav1.ListOptions {})
312
+ namespaceList , err := feeder .coreClient .Namespaces ().List (ctx , metav1.ListOptions {})
307
313
if err != nil {
308
314
klog .ErrorS (err , "Cannot list namespaces" )
309
315
return
@@ -319,7 +325,7 @@ func (feeder *clusterStateFeeder) GarbageCollectCheckpoints() {
319
325
klog .V (3 ).InfoS ("Skipping namespace; it does not meet cleanup criteria" , "namespace" , namespace , "vpaObjectNamespace" , feeder .vpaObjectNamespace , "ignoredNamespaces" , feeder .ignoredNamespaces )
320
326
continue
321
327
}
322
- err := feeder .cleanupCheckpointsForNamespace (namespace , allVPAKeys )
328
+ err := feeder .cleanupCheckpointsForNamespace (ctx , namespace , allVPAKeys )
323
329
if err != nil {
324
330
klog .ErrorS (err , "error cleanining checkpoints" )
325
331
}
@@ -338,16 +344,16 @@ func (feeder *clusterStateFeeder) shouldIgnoreNamespace(namespace string) bool {
338
344
return false
339
345
}
340
346
341
- func (feeder * clusterStateFeeder ) cleanupCheckpointsForNamespace (namespace string , allVPAKeys map [model.VpaID ]bool ) error {
347
+ func (feeder * clusterStateFeeder ) cleanupCheckpointsForNamespace (ctx context. Context , namespace string , allVPAKeys map [model.VpaID ]bool ) error {
342
348
var err error
343
- checkpointList , err := feeder .vpaCheckpointClient .VerticalPodAutoscalerCheckpoints (namespace ).List (context . TODO () , metav1.ListOptions {})
349
+ checkpointList , err := feeder .vpaCheckpointClient .VerticalPodAutoscalerCheckpoints (namespace ).List (ctx , metav1.ListOptions {})
344
350
if err != nil {
345
351
return err
346
352
}
347
353
for _ , checkpoint := range checkpointList .Items {
348
354
vpaID := model.VpaID {Namespace : checkpoint .Namespace , VpaName : checkpoint .Spec .VPAObjectName }
349
355
if ! allVPAKeys [vpaID ] {
350
- if errFeeder := feeder .vpaCheckpointClient .VerticalPodAutoscalerCheckpoints (namespace ).Delete (context . TODO () , checkpoint .Name , metav1.DeleteOptions {}); errFeeder != nil {
356
+ if errFeeder := feeder .vpaCheckpointClient .VerticalPodAutoscalerCheckpoints (namespace ).Delete (ctx , checkpoint .Name , metav1.DeleteOptions {}); errFeeder != nil {
351
357
err = fmt .Errorf ("failed to delete orphaned checkpoint %s: %w" , klog .KRef (namespace , checkpoint .Name ), err )
352
358
continue
353
359
}
@@ -484,8 +490,8 @@ func (feeder *clusterStateFeeder) LoadPods() {
484
490
}
485
491
}
486
492
487
- func (feeder * clusterStateFeeder ) LoadRealTimeMetrics () {
488
- containersMetrics , err := feeder .metricsClient .GetContainersMetrics ()
493
+ func (feeder * clusterStateFeeder ) LoadRealTimeMetrics (ctx context. Context ) {
494
+ containersMetrics , err := feeder .metricsClient .GetContainersMetrics (ctx )
489
495
if err != nil {
490
496
klog .ErrorS (err , "Cannot get ContainerMetricsSnapshot from MetricsClient" )
491
497
}
0 commit comments