Skip to content

Commit 4e143e4

Browse files
committed
OPTIM/MINOR: watch only specific configmaps
1 parent 851af81 commit 4e143e4

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

.aspell.yml

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ allowed:
4545
- cfg
4646
- optim
4747
- prometheus
48+
- configmaps

pkg/k8s/main.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
k8ssync "github.com/haproxytech/kubernetes-ingress/pkg/k8s/sync"
3434
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
3535
crdclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
36+
"k8s.io/apimachinery/pkg/fields"
37+
3638
errGw "k8s.io/apimachinery/pkg/api/errors"
3739
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3840
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -158,7 +160,7 @@ func (k k8s) MonitorChanges(eventChan chan k8ssync.SyncDataEvent, stop chan stru
158160
informersSynced := &[]cache.InformerSynced{}
159161
k.runPodInformer(eventChan, stop, informersSynced)
160162
for _, namespace := range k.whiteListedNS {
161-
k.runInformers(eventChan, stop, namespace, informersSynced)
163+
k.runInformers(eventChan, stop, namespace, informersSynced, osArgs)
162164
k.runCRInformers(eventChan, stop, namespace, informersSynced, k.crs)
163165
if gatewayAPIInstalled {
164166
k.runInformersGwAPI(eventChan, stop, namespace, informersSynced)
@@ -213,7 +215,21 @@ func (k k8s) runCRInformers(eventChan chan k8ssync.SyncDataEvent, stop chan stru
213215
}
214216
}
215217

216-
func (k k8s) runInformers(eventChan chan k8ssync.SyncDataEvent, stop chan struct{}, namespace string, informersSynced *[]cache.InformerSynced) {
218+
func (k k8s) runConfigMapInformers(eventChan chan k8ssync.SyncDataEvent, stop chan struct{}, informersSynced *[]cache.InformerSynced, configMap utils.NamespaceValue) {
219+
if configMap.Name != "" {
220+
fieldSelector := fields.OneTermEqualSelector("metadata.name", configMap.Name).String()
221+
factory := k8sinformers.NewSharedInformerFactoryWithOptions(k.builtInClient, k.cacheResyncPeriod, k8sinformers.WithNamespace(configMap.Namespace),
222+
k8sinformers.WithTweakListOptions(func(opts *metav1.ListOptions) {
223+
opts.FieldSelector = fieldSelector
224+
}))
225+
226+
cmi := k.getConfigMapInformer(eventChan, factory)
227+
go cmi.Run(stop)
228+
*informersSynced = append(*informersSynced, cmi.HasSynced)
229+
}
230+
}
231+
232+
func (k k8s) runInformers(eventChan chan k8ssync.SyncDataEvent, stop chan struct{}, namespace string, informersSynced *[]cache.InformerSynced, osArgs utils.OSArgs) {
217233
factory := k8sinformers.NewSharedInformerFactoryWithOptions(k.builtInClient, k.cacheResyncPeriod, k8sinformers.WithNamespace(namespace))
218234
// Core.V1 Resources
219235
nsi := k.getNamespaceInfomer(eventChan, factory)
@@ -222,10 +238,12 @@ func (k k8s) runInformers(eventChan chan k8ssync.SyncDataEvent, stop chan struct
222238
go svci.Run(stop)
223239
seci := k.getSecretInformer(eventChan, factory)
224240
go seci.Run(stop)
225-
cmi := k.getConfigMapInformer(eventChan, factory)
226-
go cmi.Run(stop)
241+
*informersSynced = append(*informersSynced, svci.HasSynced, nsi.HasSynced, seci.HasSynced)
227242

228-
*informersSynced = append(*informersSynced, svci.HasSynced, nsi.HasSynced, seci.HasSynced, cmi.HasSynced)
243+
k.runConfigMapInformers(eventChan, stop, informersSynced, osArgs.ConfigMap)
244+
k.runConfigMapInformers(eventChan, stop, informersSynced, osArgs.ConfigMapTCPServices)
245+
k.runConfigMapInformers(eventChan, stop, informersSynced, osArgs.ConfigMapErrorFiles)
246+
k.runConfigMapInformers(eventChan, stop, informersSynced, osArgs.ConfigMapPatternFiles)
229247

230248
// Ingress and IngressClass Resources
231249
ii, ici := k.getIngressInformers(eventChan, factory)

pkg/store/events.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ func (k *K8s) EventConfigMap(ns *Namespace, data *ConfigMap) (updateRequired boo
216216
}
217217
*cm = *data
218218
cm.Loaded = true
219-
updateRequired = true
219+
if !cm.Empty() {
220+
updateRequired = true
221+
}
220222
logger.Debugf("configmap '%s/%s' processed", cm.Namespace, cm.Name)
221223
case MODIFIED:
222224
if cm.Equal(data) {
@@ -228,7 +230,9 @@ func (k *K8s) EventConfigMap(ns *Namespace, data *ConfigMap) (updateRequired boo
228230
case DELETED:
229231
cm.Loaded = false
230232
cm.Annotations = map[string]string{}
231-
updateRequired = true
233+
if !cm.Empty() {
234+
updateRequired = true
235+
}
232236
logger.Debugf("configmap '%s/%s' deleted", cm.Namespace, cm.Name)
233237
}
234238
return updateRequired

pkg/store/types-equal.go

+11
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ func (a *ConfigMap) Equal(b *ConfigMap) bool {
7777
return true
7878
}
7979

80+
// Empty checks if configmap is empty
81+
func (a *ConfigMap) Empty() bool {
82+
if a == nil {
83+
return true
84+
}
85+
if len(a.Annotations) == 0 {
86+
return true
87+
}
88+
return false
89+
}
90+
8091
// Equal compares two secrets, ignores statuses and old values
8192
func (a *Secret) Equal(b *Secret) bool {
8293
if a == nil || b == nil {

0 commit comments

Comments
 (0)