Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
configurator: optimize ConfigMap informer filtering (#2000)
Browse files Browse the repository at this point in the history
This change makes use of the filtering capability in informers
to filter API responses on the server side, by using a FieldSelector
to filter on the name of the ConfigMap as opposed to filtering all
objects on the client (osm-controller) side. The informer is already
namespaced, so the namespace and name should be enough to accurately
only have OSM's ConfigMap in the informer cache and events.

This change will also reduce the number of ConfigMap related events
the controller will receive when there are additional ConfigMaps in
the controller's namespace (ex. Grafana's ConfigMap when Grafana is
deployed as a part of control plane), and thus improving performance
in such scenarios. The informer resyncs are on a per resource basis,
so unnecessary events can take up processing resources.
  • Loading branch information
shashankram authored Nov 6, 2020
1 parent 49a8099 commit 1290720
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions pkg/configurator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package configurator

import (
"fmt"
"reflect"
"strconv"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -54,7 +55,12 @@ func NewConfigurator(kubeClient kubernetes.Interface, stop <-chan struct{}, osmN
}

func newConfigurator(kubeClient kubernetes.Interface, stop <-chan struct{}, osmNamespace, osmConfigMapName string) *Client {
informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, k8s.DefaultKubeEventResyncInterval, informers.WithNamespace(osmNamespace))
// Ensure this informer exclusively watches only the Namespace where OSM in installed and the particular 'osm-config' ConfigMap
informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient,
k8s.DefaultKubeEventResyncInterval, informers.WithNamespace(osmNamespace),
informers.WithTweakListOptions(func(listOptions *metav1.ListOptions) {
listOptions.FieldSelector = fields.OneTermEqualSelector("metadata.name", osmConfigMapName).String()
}))
informer := informerFactory.Core().V1().ConfigMaps().Informer()
client := Client{
informer: informer,
Expand All @@ -65,16 +71,9 @@ func newConfigurator(kubeClient kubernetes.Interface, stop <-chan struct{}, osmN
osmConfigMapName: osmConfigMapName,
}

// Ensure this exclusively watches only the Namespace where OSM in installed and the particular ConfigMap we need.
shouldObserve := func(obj interface{}) bool {
ns := reflect.ValueOf(obj).Elem().FieldByName("ObjectMeta").FieldByName("Namespace").String()
name := reflect.ValueOf(obj).Elem().FieldByName("ObjectMeta").FieldByName("Name").String()
return ns == osmNamespace && name == osmConfigMapName
}

informerName := "ConfigMap"
providerName := "OSMConfigMap"
informer.AddEventHandler(k8s.GetKubernetesEventHandlers(informerName, providerName, client.announcements, shouldObserve))
informer.AddEventHandler(k8s.GetKubernetesEventHandlers(informerName, providerName, client.announcements, nil))

client.run(stop)

Expand Down

0 comments on commit 1290720

Please sign in to comment.