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

Commit

Permalink
configurator: only watch required MeshConfig (#4587)
Browse files Browse the repository at this point in the history
Updates the lister to only watch the required
MeshConfig instead of all MeshConfigs, and
removes unnecessary code pertaining to irrelevant
MeshConfigs.
Also renames the import aliases and groups them
for consistency.

Signed-off-by: Shashank Ram <[email protected]>
  • Loading branch information
shashankram authored Mar 10, 2022
1 parent fe04957 commit b7a4a3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
27 changes: 15 additions & 12 deletions pkg/configurator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,37 @@ import (
"fmt"
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"

configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"

"github.com/openservicemesh/osm/pkg/errcode"
"github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
informers "github.com/openservicemesh/osm/pkg/gen/client/config/informers/externalversions"
"github.com/openservicemesh/osm/pkg/messaging"
configClientset "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
configInformers "github.com/openservicemesh/osm/pkg/gen/client/config/informers/externalversions"

"github.com/openservicemesh/osm/pkg/announcements"
"github.com/openservicemesh/osm/pkg/errcode"
"github.com/openservicemesh/osm/pkg/k8s"
"github.com/openservicemesh/osm/pkg/messaging"
"github.com/openservicemesh/osm/pkg/metricsstore"
)

// NewConfigurator implements configurator.Configurator and creates the Kubernetes client to manage namespaces.
func NewConfigurator(meshConfigClientSet versioned.Interface, stop <-chan struct{}, osmNamespace, meshConfigName string,
func NewConfigurator(meshConfigClientSet configClientset.Interface, stop <-chan struct{}, osmNamespace, meshConfigName string,
msgBroker *messaging.Broker) Configurator {
return newConfigurator(meshConfigClientSet, stop, osmNamespace, meshConfigName, msgBroker)
}

func newConfigurator(meshConfigClientSet versioned.Interface, stop <-chan struct{}, osmNamespace string, meshConfigName string,
func newConfigurator(meshConfigClientSet configClientset.Interface, stop <-chan struct{}, osmNamespace string, meshConfigName string,
msgBroker *messaging.Broker) *client {
informerFactory := informers.NewSharedInformerFactoryWithOptions(
listOption := configInformers.WithTweakListOptions(func(opt *metav1.ListOptions) {
opt.FieldSelector = fields.OneTermEqualSelector(metav1.ObjectNameField, meshConfigName).String()
})
informerFactory := configInformers.NewSharedInformerFactoryWithOptions(
meshConfigClientSet,
k8s.DefaultKubeEventResyncInterval,
informers.WithNamespace(osmNamespace),
configInformers.WithNamespace(osmNamespace),
listOption,
)
informer := informerFactory.Config().V1alpha2().MeshConfigs().Informer()
c := &client{
Expand Down Expand Up @@ -93,9 +98,7 @@ func (c *client) getMeshConfig() configv1alpha2.MeshConfig {
func (c *client) metricsHandler() cache.ResourceEventHandlerFuncs {
handleMetrics := func(obj interface{}) {
config := obj.(*configv1alpha2.MeshConfig)
if config.Name != c.meshConfigName {
return
}

// This uses reflection to iterate over the feature flags to avoid
// enumerating them here individually. This code assumes the following:
// - MeshConfig.Spec.FeatureFlags is a struct, not a pointer to a struct
Expand Down
14 changes: 0 additions & 14 deletions pkg/configurator/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ func TestMetricsHandler(t *testing.T) {
assertMetricsContain(`osm_feature_flag_enabled{feature_flag="enableRetryPolicy"} 1` + "\n")
assertMetricsContain(`osm_feature_flag_enabled{feature_flag="enableSnapshotCacheMode"} 0` + "\n")

// Adding a different MeshConfig whose settings are ignored
handlers.OnAdd(&configv1alpha2.MeshConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "not-" + osmMeshConfigName,
},
Spec: configv1alpha2.MeshConfigSpec{
FeatureFlags: configv1alpha2.FeatureFlags{
EnableSnapshotCacheMode: true,
},
},
})
assertMetricsContain(`osm_feature_flag_enabled{feature_flag="enableRetryPolicy"} 1` + "\n")
assertMetricsContain(`osm_feature_flag_enabled{feature_flag="enableSnapshotCacheMode"} 0` + "\n")

// Updating the "real" MeshConfig
handlers.OnUpdate(nil, &configv1alpha2.MeshConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit b7a4a3b

Please sign in to comment.