@@ -145,6 +145,14 @@ func (ir *informerResources) CopyTo(fakeClient *fakeclientset.Clientset, newFact
145
145
return nil
146
146
}
147
147
148
+ func metricsProviderListToMap (providersList []api.MetricsProvider ) map [api.MetricsSource ]* api.MetricsProvider {
149
+ providersMap := make (map [api.MetricsSource ]* api.MetricsProvider )
150
+ for _ , provider := range providersList {
151
+ providersMap [provider .Source ] = & provider
152
+ }
153
+ return providersMap
154
+ }
155
+
148
156
func newDescheduler (ctx context.Context , rs * options.DeschedulerServer , deschedulerPolicy * api.DeschedulerPolicy , evictionPolicyGroupVersion string , eventRecorder events.EventRecorder , sharedInformerFactory , namespacedSharedInformerFactory informers.SharedInformerFactory ) (* descheduler , error ) {
149
157
podInformer := sharedInformerFactory .Core ().V1 ().Pods ().Informer ()
150
158
@@ -195,11 +203,7 @@ func newDescheduler(ctx context.Context, rs *options.DeschedulerServer, deschedu
195
203
podEvictionReactionFnc : podEvictionReactionFnc ,
196
204
prometheusClient : rs .PrometheusClient ,
197
205
queue : workqueue .NewRateLimitingQueueWithConfig (workqueue .DefaultControllerRateLimiter (), workqueue.RateLimitingQueueConfig {Name : "descheduler" }),
198
- metricsProviders : make (map [api.MetricsSource ]* api.MetricsProvider ),
199
- }
200
-
201
- for _ , provider := range deschedulerPolicy .MetricsProviders {
202
- desch .metricsProviders [provider .Source ] = & provider
206
+ metricsProviders : metricsProviderListToMap (deschedulerPolicy .MetricsProviders ),
203
207
}
204
208
205
209
if rs .MetricsClient != nil {
@@ -215,12 +219,16 @@ func newDescheduler(ctx context.Context, rs *options.DeschedulerServer, deschedu
215
219
}
216
220
217
221
prometheusProvider := desch .metricsProviders [api .PrometheusMetrics ]
218
- if namespacedSharedInformerFactory != nil && prometheusProvider != nil {
219
- if prometheusProvider .Prometheus == nil || prometheusProvider .Prometheus .AuthToken == nil || prometheusProvider .Prometheus .AuthToken .SecretReference == nil {
222
+ if prometheusProvider != nil && prometheusProvider .Prometheus != nil && prometheusProvider .Prometheus .AuthToken != nil {
223
+ authTokenSecret := prometheusProvider .Prometheus .AuthToken .SecretReference
224
+ if authTokenSecret == nil || authTokenSecret .Namespace == "" {
220
225
return nil , fmt .Errorf ("prometheus metrics source configuration is missing authentication token secret" )
221
226
}
227
+ if namespacedSharedInformerFactory == nil {
228
+ return nil , fmt .Errorf ("namespacedSharedInformerFactory not configured" )
229
+ }
222
230
namespacedSharedInformerFactory .Core ().V1 ().Secrets ().Informer ().AddEventHandler (desch .eventHandler ())
223
- desch .namespacedSecretsLister = namespacedSharedInformerFactory .Core ().V1 ().Secrets ().Lister ().Secrets (desch . metricsProviders [ api . PrometheusMetrics ]. Prometheus . AuthToken . SecretReference .Namespace )
231
+ desch .namespacedSecretsLister = namespacedSharedInformerFactory .Core ().V1 ().Secrets ().Lister ().Secrets (authTokenSecret .Namespace )
224
232
}
225
233
226
234
return desch , nil
@@ -299,6 +307,11 @@ func (d *descheduler) sync() error {
299
307
// clear the token if the secret is not found
300
308
if apierrors .IsNotFound (err ) {
301
309
d .currentPrometheusAuthToken = ""
310
+ if d .previousPrometheusClientTransport != nil {
311
+ d .previousPrometheusClientTransport .CloseIdleConnections ()
312
+ }
313
+ d .previousPrometheusClientTransport = nil
314
+ d .prometheusClient = nil
302
315
}
303
316
return fmt .Errorf ("unable to get %v/%v secret" , ns , name )
304
317
}
@@ -474,14 +487,7 @@ func Run(ctx context.Context, rs *options.DeschedulerServer) error {
474
487
return err
475
488
}
476
489
477
- hasKubernetesMetricsProvider := false
478
- for _ , provider := range deschedulerPolicy .MetricsProviders {
479
- if provider .Source == api .KubernetesMetrics {
480
- hasKubernetesMetricsProvider = true
481
- break
482
- }
483
- }
484
- if (deschedulerPolicy .MetricsCollector != nil && deschedulerPolicy .MetricsCollector .Enabled ) || hasKubernetesMetricsProvider {
490
+ if (deschedulerPolicy .MetricsCollector != nil && deschedulerPolicy .MetricsCollector .Enabled ) || metricsProviderListToMap (deschedulerPolicy .MetricsProviders )[api .KubernetesMetrics ] != nil {
485
491
metricsClient , err := client .CreateMetricsClient (clientConnection , "descheduler" )
486
492
if err != nil {
487
493
return err
@@ -594,23 +600,17 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
594
600
defer eventBroadcaster .Shutdown ()
595
601
596
602
var namespacedSharedInformerFactory informers.SharedInformerFactory
597
- saTokenReconciliation := noReconciliation
598
-
599
- var prometheusConfiguration * api.Prometheus
600
- for _ , provider := range deschedulerPolicy .MetricsProviders {
601
- if provider .Source == api .PrometheusMetrics {
602
- prometheusConfiguration = provider .Prometheus
603
- }
604
- }
603
+ metricProviderTokenReconciliation := noReconciliation
605
604
606
- if prometheusConfiguration != nil && prometheusConfiguration .URL != "" {
607
- if prometheusConfiguration .AuthToken != nil {
605
+ prometheusProvider := metricsProviderListToMap (deschedulerPolicy .MetricsProviders )[api .PrometheusMetrics ]
606
+ if prometheusProvider != nil && prometheusProvider .Prometheus != nil && prometheusProvider .Prometheus .URL != "" {
607
+ if prometheusProvider .Prometheus .AuthToken != nil {
608
608
// Will get reconciled
609
- namespacedSharedInformerFactory = informers .NewSharedInformerFactoryWithOptions (rs .Client , 0 , informers .WithTransform (trimManagedFields ), informers .WithNamespace (prometheusConfiguration .AuthToken .SecretReference .Namespace ))
610
- saTokenReconciliation = secretReconciliation
609
+ namespacedSharedInformerFactory = informers .NewSharedInformerFactoryWithOptions (rs .Client , 0 , informers .WithTransform (trimManagedFields ), informers .WithNamespace (prometheusProvider . Prometheus .AuthToken .SecretReference .Namespace ))
610
+ metricProviderTokenReconciliation = secretReconciliation
611
611
} else {
612
612
// Use the sa token and assume it has the sufficient permissions to authenticate
613
- saTokenReconciliation = inClusterReconciliation
613
+ metricProviderTokenReconciliation = inClusterReconciliation
614
614
}
615
615
}
616
616
@@ -623,13 +623,13 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
623
623
defer cancel ()
624
624
625
625
sharedInformerFactory .Start (ctx .Done ())
626
- if saTokenReconciliation == secretReconciliation {
626
+ if metricProviderTokenReconciliation == secretReconciliation {
627
627
namespacedSharedInformerFactory .Start (ctx .Done ())
628
628
}
629
629
630
630
sharedInformerFactory .WaitForCacheSync (ctx .Done ())
631
631
descheduler .podEvictor .WaitForEventHandlersSync (ctx )
632
- if saTokenReconciliation == secretReconciliation {
632
+ if metricProviderTokenReconciliation == secretReconciliation {
633
633
namespacedSharedInformerFactory .WaitForCacheSync (ctx .Done ())
634
634
}
635
635
@@ -647,12 +647,12 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
647
647
}
648
648
}
649
649
650
- if saTokenReconciliation == secretReconciliation {
650
+ if metricProviderTokenReconciliation == secretReconciliation {
651
651
go descheduler .runAuthenticationSecretReconciler (ctx )
652
652
}
653
653
654
654
wait .NonSlidingUntil (func () {
655
- if saTokenReconciliation == inClusterReconciliation {
655
+ if metricProviderTokenReconciliation == inClusterReconciliation {
656
656
// Read the sa token and assume it has the sufficient permissions to authenticate
657
657
if err := descheduler .reconcileInClusterSAToken (); err != nil {
658
658
klog .ErrorS (err , "unable to reconcile an in cluster SA token" )
0 commit comments