From 8b8fc5266a0aa80842b19777c73f7e04fea3af4c Mon Sep 17 00:00:00 2001 From: Jim Ryan Date: Wed, 27 Nov 2024 17:07:15 +0000 Subject: [PATCH] refactor configmap.go for extensability (#6873) --- internal/k8s/configmap.go | 28 ++++++++++++++++++---------- internal/k8s/controller.go | 2 ++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/internal/k8s/configmap.go b/internal/k8s/configmap.go index 3a22fdee00..001775aaa3 100644 --- a/internal/k8s/configmap.go +++ b/internal/k8s/configmap.go @@ -50,9 +50,8 @@ func createConfigMapHandlers(lbc *LoadBalancerController, name string) cache.Res } } -// addConfigMapHandler adds the handler for config maps to the controller -func (lbc *LoadBalancerController) addConfigMapHandler(handlers cache.ResourceEventHandlerFuncs, namespace string) { - options := cache.InformerOptions{ +func (lbc *LoadBalancerController) getConfigMapHandlerOptions(handlers cache.ResourceEventHandlerFuncs, namespace string) cache.InformerOptions { + return cache.InformerOptions{ ListerWatcher: cache.NewListWatchFromClient( lbc.client.CoreV1().RESTClient(), "configmaps", @@ -62,6 +61,12 @@ func (lbc *LoadBalancerController) addConfigMapHandler(handlers cache.ResourceEv ResyncPeriod: lbc.resync, Handler: handlers, } +} + +// addConfigMapHandler adds the handler for config maps to the controller +func (lbc *LoadBalancerController) addConfigMapHandler(handlers cache.ResourceEventHandlerFuncs, namespace string) { + options := lbc.getConfigMapHandlerOptions(handlers, namespace) + lbc.configMapLister.Store, lbc.configMapController = cache.NewInformerWithOptions(options) lbc.cacheSyncs = append(lbc.cacheSyncs, lbc.configMapController.HasSynced) } @@ -75,14 +80,17 @@ func (lbc *LoadBalancerController) syncConfigMap(task task) { lbc.syncQueue.Requeue(task, err) return } - if configExists { - lbc.configMap = obj.(*v1.ConfigMap) - externalStatusAddress, exists := lbc.configMap.Data["external-status-address"] - if exists { - lbc.statusUpdater.SaveStatusFromExternalStatus(externalStatusAddress) + switch key { + case lbc.nginxConfigMapName: + if configExists { + lbc.configMap = obj.(*v1.ConfigMap) + externalStatusAddress, exists := lbc.configMap.Data["external-status-address"] + if exists { + lbc.statusUpdater.SaveStatusFromExternalStatus(externalStatusAddress) + } + } else { + lbc.configMap = nil } - } else { - lbc.configMap = nil } if !lbc.isNginxReady { diff --git a/internal/k8s/controller.go b/internal/k8s/controller.go index 830abce4ed..79d7f6b0bf 100644 --- a/internal/k8s/controller.go +++ b/internal/k8s/controller.go @@ -171,6 +171,7 @@ type LoadBalancerController struct { telemetryCollector *telemetry.Collector telemetryChan chan struct{} weightChangesDynamicReload bool + nginxConfigMapName string } var keyFunc = cache.DeletionHandlingMetaNamespaceKeyFunc @@ -263,6 +264,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc isLatencyMetricsEnabled: input.IsLatencyMetricsEnabled, isIPV6Disabled: input.IsIPV6Disabled, weightChangesDynamicReload: input.DynamicWeightChangesReload, + nginxConfigMapName: input.ConfigMaps, } lbc.syncQueue = newTaskQueue(lbc.Logger, lbc.sync)