From 63238ade03933be8d91eedbc63fc88c1de851d2e Mon Sep 17 00:00:00 2001 From: Tibi <110664232+TiberiuGC@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:40:49 +0300 Subject: [PATCH] Only setup necessary configmap informers depending on used CARM version (#157) Issue #, if available: Description of changes: - if CARMv2 feature gate is enabled, setup `ack-carm-map` map informer, otherwise setup `ack-role-account-map` informer. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --- pkg/runtime/cache/cache.go | 30 ++++++++++++++++++++++++------ pkg/runtime/service_controller.go | 1 + 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/runtime/cache/cache.go b/pkg/runtime/cache/cache.go index 7dd464e..65e0c59 100644 --- a/pkg/runtime/cache/cache.go +++ b/pkg/runtime/cache/cache.go @@ -21,6 +21,8 @@ import ( "github.com/jaypipes/envutil" kubernetes "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" + + "github.com/aws-controllers-k8s/runtime/pkg/featuregate" ) const ( @@ -85,10 +87,16 @@ type Caches struct { } // New instantiate a new Caches object. -func New(log logr.Logger, config Config) Caches { +func New(log logr.Logger, config Config, features featuregate.FeatureGates) Caches { + var carmMaps, accounts *CARMMap + if features.IsEnabled(featuregate.CARMv2) { + carmMaps = NewCARMMapCache(log) + } else { + accounts = NewCARMMapCache(log) + } return Caches{ - Accounts: NewCARMMapCache(log), - CARMMaps: NewCARMMapCache(log), + Accounts: accounts, + CARMMaps: carmMaps, Namespaces: NewNamespaceCache(log, config.WatchScope, config.Ignored), } } @@ -110,9 +118,19 @@ func (c Caches) Run(clientSet kubernetes.Interface) { // WaitForCachesToSync waits for both of the namespace and configMap // informers to sync - by checking their hasSynced functions. func (c Caches) WaitForCachesToSync(ctx context.Context) bool { - namespaceSynced := cache.WaitForCacheSync(ctx.Done(), c.Namespaces.hasSynced) - accountSynced := cache.WaitForCacheSync(ctx.Done(), c.Accounts.hasSynced) - return namespaceSynced && accountSynced + // if the cache is not initialized, sync status should be true + namespaceSynced, accountSynced, carmSynced := true, true, true + // otherwise check their hasSynced functions + if c.Namespaces != nil { + namespaceSynced = cache.WaitForCacheSync(ctx.Done(), c.Namespaces.hasSynced) + } + if c.Accounts != nil { + accountSynced = cache.WaitForCacheSync(ctx.Done(), c.Accounts.hasSynced) + } + if c.CARMMaps != nil { + carmSynced = cache.WaitForCacheSync(ctx.Done(), c.CARMMaps.hasSynced) + } + return namespaceSynced && accountSynced && carmSynced } // Stop closes the stop channel and cause all the SharedInformers diff --git a/pkg/runtime/service_controller.go b/pkg/runtime/service_controller.go index 0e8ff55..cd27ba9 100644 --- a/pkg/runtime/service_controller.go +++ b/pkg/runtime/service_controller.go @@ -217,6 +217,7 @@ func (c *serviceController) BindControllerManager(mgr ctrlrt.Manager, cfg ackcfg NamespaceKubePublic, NamespaceKubeNodeLease, }}, + cfg.FeatureGates, ) // We want to run the caches if the length of the namespaces slice is // either 0 (watching all namespaces) or greater than 1 (watching multiple