@@ -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,10 @@ 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
302
314
}
303
315
return fmt .Errorf ("unable to get %v/%v secret" , ns , name )
304
316
}
@@ -474,14 +486,7 @@ func Run(ctx context.Context, rs *options.DeschedulerServer) error {
474
486
return err
475
487
}
476
488
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 {
489
+ if (deschedulerPolicy .MetricsCollector != nil && deschedulerPolicy .MetricsCollector .Enabled ) || metricsProviderListToMap (deschedulerPolicy .MetricsProviders )[api .KubernetesMetrics ] != nil {
485
490
metricsClient , err := client .CreateMetricsClient (clientConnection , "descheduler" )
486
491
if err != nil {
487
492
return err
@@ -594,23 +599,17 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
594
599
defer eventBroadcaster .Shutdown ()
595
600
596
601
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
- }
602
+ metricProviderTokenReconciliation := noReconciliation
605
603
606
- if prometheusConfiguration != nil && prometheusConfiguration .URL != "" {
607
- if prometheusConfiguration .AuthToken != nil {
604
+ prometheusProvider := metricsProviderListToMap (deschedulerPolicy .MetricsProviders )[api .PrometheusMetrics ]
605
+ if prometheusProvider != nil && prometheusProvider .Prometheus != nil && prometheusProvider .Prometheus .URL != "" {
606
+ if prometheusProvider .Prometheus .AuthToken != nil {
608
607
// Will get reconciled
609
- namespacedSharedInformerFactory = informers .NewSharedInformerFactoryWithOptions (rs .Client , 0 , informers .WithTransform (trimManagedFields ), informers .WithNamespace (prometheusConfiguration .AuthToken .SecretReference .Namespace ))
610
- saTokenReconciliation = secretReconciliation
608
+ namespacedSharedInformerFactory = informers .NewSharedInformerFactoryWithOptions (rs .Client , 0 , informers .WithTransform (trimManagedFields ), informers .WithNamespace (prometheusProvider . Prometheus .AuthToken .SecretReference .Namespace ))
609
+ metricProviderTokenReconciliation = secretReconciliation
611
610
} else {
612
611
// Use the sa token and assume it has the sufficient permissions to authenticate
613
- saTokenReconciliation = inClusterReconciliation
612
+ metricProviderTokenReconciliation = inClusterReconciliation
614
613
}
615
614
}
616
615
@@ -623,13 +622,13 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
623
622
defer cancel ()
624
623
625
624
sharedInformerFactory .Start (ctx .Done ())
626
- if saTokenReconciliation == secretReconciliation {
625
+ if metricProviderTokenReconciliation == secretReconciliation {
627
626
namespacedSharedInformerFactory .Start (ctx .Done ())
628
627
}
629
628
630
629
sharedInformerFactory .WaitForCacheSync (ctx .Done ())
631
630
descheduler .podEvictor .WaitForEventHandlersSync (ctx )
632
- if saTokenReconciliation == secretReconciliation {
631
+ if metricProviderTokenReconciliation == secretReconciliation {
633
632
namespacedSharedInformerFactory .WaitForCacheSync (ctx .Done ())
634
633
}
635
634
@@ -647,12 +646,12 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
647
646
}
648
647
}
649
648
650
- if saTokenReconciliation == secretReconciliation {
649
+ if metricProviderTokenReconciliation == secretReconciliation {
651
650
go descheduler .runAuthenticationSecretReconciler (ctx )
652
651
}
653
652
654
653
wait .NonSlidingUntil (func () {
655
- if saTokenReconciliation == inClusterReconciliation {
654
+ if metricProviderTokenReconciliation == inClusterReconciliation {
656
655
// Read the sa token and assume it has the sufficient permissions to authenticate
657
656
if err := descheduler .reconcileInClusterSAToken (); err != nil {
658
657
klog .ErrorS (err , "unable to reconcile an in cluster SA token" )
0 commit comments