Skip to content

Commit d365253

Browse files
committed
Addressing reviev comments
1 parent d7421d7 commit d365253

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ profiles:
353353
"cpu" : 50
354354
"memory": 50
355355
"pods": 50
356-
metricsUtilization:
357-
source: KubernetesMetrics
358-
# prometheus:
359-
# query: instance:node_cpu:rate:sum
356+
# metricsUtilization:
357+
# source: Prometheus
358+
# prometheus:
359+
# query: instance:node_cpu:rate:sum
360360
evictionLimits:
361361
node: 5
362362
plugins:

pkg/api/types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ type ReferencedResourceList = map[v1.ResourceName]*resource.Quantity
142142
type Prometheus struct {
143143
URL string
144144
// authToken used for authentication with the prometheus server.
145-
// If not set the in cluster authentication token from the container's file system is read.
145+
// If not set the in cluster authentication token for the descheduler service
146+
// account is read from the container's file system.
146147
AuthToken *AuthToken
147148
}
148149

pkg/api/v1alpha2/types.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ type MetricsProvider struct {
113113

114114
type Prometheus struct {
115115
URL string `json:"url,omitempty"`
116-
// If not set the in cluster authentication token from the container's file system is read.
116+
// authToken used for authentication with the prometheus server.
117+
// If not set the in cluster authentication token for the descheduler service
118+
// account is read from the container's file system.
117119
AuthToken *AuthToken `json:"authToken,omitempty"`
118120
}
119121

pkg/descheduler/descheduler.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ func (ir *informerResources) CopyTo(fakeClient *fakeclientset.Clientset, newFact
145145
return nil
146146
}
147147

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+
148156
func newDescheduler(ctx context.Context, rs *options.DeschedulerServer, deschedulerPolicy *api.DeschedulerPolicy, evictionPolicyGroupVersion string, eventRecorder events.EventRecorder, sharedInformerFactory, namespacedSharedInformerFactory informers.SharedInformerFactory) (*descheduler, error) {
149157
podInformer := sharedInformerFactory.Core().V1().Pods().Informer()
150158

@@ -195,11 +203,7 @@ func newDescheduler(ctx context.Context, rs *options.DeschedulerServer, deschedu
195203
podEvictionReactionFnc: podEvictionReactionFnc,
196204
prometheusClient: rs.PrometheusClient,
197205
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),
203207
}
204208

205209
if rs.MetricsClient != nil {
@@ -215,12 +219,16 @@ func newDescheduler(ctx context.Context, rs *options.DeschedulerServer, deschedu
215219
}
216220

217221
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 == "" {
220225
return nil, fmt.Errorf("prometheus metrics source configuration is missing authentication token secret")
221226
}
227+
if namespacedSharedInformerFactory == nil {
228+
return nil, fmt.Errorf("namespacedSharedInformerFactory not configured")
229+
}
222230
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)
224232
}
225233

226234
return desch, nil
@@ -299,6 +307,11 @@ func (d *descheduler) sync() error {
299307
// clear the token if the secret is not found
300308
if apierrors.IsNotFound(err) {
301309
d.currentPrometheusAuthToken = ""
310+
if d.previousPrometheusClientTransport != nil {
311+
d.previousPrometheusClientTransport.CloseIdleConnections()
312+
}
313+
d.previousPrometheusClientTransport = nil
314+
d.prometheusClient = nil
302315
}
303316
return fmt.Errorf("unable to get %v/%v secret", ns, name)
304317
}
@@ -474,14 +487,7 @@ func Run(ctx context.Context, rs *options.DeschedulerServer) error {
474487
return err
475488
}
476489

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 {
485491
metricsClient, err := client.CreateMetricsClient(clientConnection, "descheduler")
486492
if err != nil {
487493
return err
@@ -594,23 +600,17 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
594600
defer eventBroadcaster.Shutdown()
595601

596602
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
605604

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 {
608608
// 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
611611
} else {
612612
// Use the sa token and assume it has the sufficient permissions to authenticate
613-
saTokenReconciliation = inClusterReconciliation
613+
metricProviderTokenReconciliation = inClusterReconciliation
614614
}
615615
}
616616

@@ -623,13 +623,13 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
623623
defer cancel()
624624

625625
sharedInformerFactory.Start(ctx.Done())
626-
if saTokenReconciliation == secretReconciliation {
626+
if metricProviderTokenReconciliation == secretReconciliation {
627627
namespacedSharedInformerFactory.Start(ctx.Done())
628628
}
629629

630630
sharedInformerFactory.WaitForCacheSync(ctx.Done())
631631
descheduler.podEvictor.WaitForEventHandlersSync(ctx)
632-
if saTokenReconciliation == secretReconciliation {
632+
if metricProviderTokenReconciliation == secretReconciliation {
633633
namespacedSharedInformerFactory.WaitForCacheSync(ctx.Done())
634634
}
635635

@@ -647,12 +647,12 @@ func RunDeschedulerStrategies(ctx context.Context, rs *options.DeschedulerServer
647647
}
648648
}
649649

650-
if saTokenReconciliation == secretReconciliation {
650+
if metricProviderTokenReconciliation == secretReconciliation {
651651
go descheduler.runAuthenticationSecretReconciler(ctx)
652652
}
653653

654654
wait.NonSlidingUntil(func() {
655-
if saTokenReconciliation == inClusterReconciliation {
655+
if metricProviderTokenReconciliation == inClusterReconciliation {
656656
// Read the sa token and assume it has the sufficient permissions to authenticate
657657
if err := descheduler.reconcileInClusterSAToken(); err != nil {
658658
klog.ErrorS(err, "unable to reconcile an in cluster SA token")

pkg/framework/plugins/nodeutilization/lownodeutilization.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,20 @@ func NewLowNodeUtilization(args runtime.Object, handle frameworktypes.Handle) (f
107107
var usageClient usageClient
108108
// MetricsServer is deprecated, removed once dropped
109109
if metricsUtilization != nil {
110-
if metricsUtilization.MetricsServer || metricsUtilization.Source == api.KubernetesMetrics {
110+
switch {
111+
case metricsUtilization.MetricsServer, metricsUtilization.Source == api.KubernetesMetrics:
111112
if handle.MetricsCollector() == nil {
112113
return nil, fmt.Errorf("metrics client not initialized")
113114
}
114115
usageClient = newActualUsageClient(resourceNames, handle.GetPodsAssignedToNodeFunc(), handle.MetricsCollector())
115-
} else if metricsUtilization.Source == api.PrometheusMetrics {
116+
case metricsUtilization.Source == api.PrometheusMetrics:
116117
if handle.PrometheusClient() == nil {
117118
return nil, fmt.Errorf("prometheus client not initialized")
118119
}
119120
usageClient = newPrometheusUsageClient(handle.GetPodsAssignedToNodeFunc(), handle.PrometheusClient(), metricsUtilization.Prometheus.Query)
120-
} else if metricsUtilization.Source != "" {
121+
case metricsUtilization.Source != "":
121122
return nil, fmt.Errorf("unrecognized metrics source")
122-
} else {
123+
default:
123124
return nil, fmt.Errorf("metrics source is empty")
124125
}
125126
} else {

0 commit comments

Comments
 (0)