Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi namespaces cache #62

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api/v1alpha1/resourcesyncrule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ const (

type ResourceSyncRuleStatus struct{}

// GetRelatedNamespaces gathers namespaces from rule matchers
// it returns an empty list if any of the matchers is without namespace filter
// since it means every namespace is related
func (s *ResourceSyncRule) GetRelatedNamespaces() []string {
Copy link

@panyuenlau panyuenlau Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great if we can add an unit test for this function

namespaces := []string{}

_namespaces := map[string]struct{}{}

for _, rule := range s.Spec.Rules {
for _, match := range rule.Matches {
if len(match.Namespaces) == 0 {
return namespaces
Copy link

@panyuenlau panyuenlau Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why are we returning namespaces here (I believe it is always empty if we reach this line)? Don't we need to check the next rule in the outermost for loop?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it returns there, because if there is a match without namespace filtering that means actually it cares about resources from every namespace

}
for _, ns := range match.Namespaces {
_namespaces[ns] = struct{}{}
}
}
}

for ns := range _namespaces {
namespaces = append(namespaces, ns)
}

return namespaces
}

// +kubebuilder:object:root=true

// ResourceSyncRule is the Schema for the resource sync rule API
Expand Down
4 changes: 2 additions & 2 deletions controllers/cluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ func (r *ClusterReconciler) getRemoteCluster(ctx context.Context, cluster *clust
return nil, errors.WrapIf(err, "could not create new cluster")
}

err = remoteCluster.AddController(clusters.NewManagedController("remote-cluster", NewRemoteClusterReconciler(cluster.Name, r.GetManager(), r.GetLogger()), r.GetLogger()))
err = remoteCluster.AddController(clusters.NewManagedController("remote-cluster", nil, NewRemoteClusterReconciler(cluster.Name, r.GetManager(), r.GetLogger()), r.GetLogger()))
if err != nil {
return nil, errors.WrapIf(err, "could not add managed controller")
}

err = remoteCluster.AddController(clusters.NewManagedController("remote-cluster-feature", NewClusterFeatureReconciler(cluster.Name, remoteCluster, r.GetLogger()), r.GetLogger()))
err = remoteCluster.AddController(clusters.NewManagedController("remote-cluster-feature", nil, NewClusterFeatureReconciler(cluster.Name, remoteCluster, r.GetLogger()), r.GetLogger()))
if err != nil {
return nil, errors.WrapIf(err, "could not add managed controller")
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/resource_sync_rule_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func InitNewResourceSyncController(rule *clusterregistryv1alpha1.ResourceSyncRul
if err != nil {
return nil, errors.WithStackIf(err)
}
ctrl := clusters.NewManagedController(rule.Name, srec, log, clusters.WithRequiredClusterFeatures(requiredClusterFeatures...))
ctrl := clusters.NewManagedController(rule.Name, rule.GetRelatedNamespaces(), srec, log, clusters.WithRequiredClusterFeatures(requiredClusterFeatures...))

return ctrl, cluster.AddController(ctrl)
}
12 changes: 11 additions & 1 deletion controllers/sync_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,17 @@ func (r *syncReconciler) createClient(config *rest.Config, cache cache.Cache) (c
}

func (r *syncReconciler) createAndStartCache() (cache.Cache, error) {
cche, err := cache.New(r.localMgr.GetConfig(), cache.Options{
cacheFunc := cache.New

relatedNamespaces := r.rule.GetRelatedNamespaces()
if len(relatedNamespaces) > 0 {
r.GetLogger().Info("create multi namespace cache", "namespaces", relatedNamespaces)
cacheFunc = cache.MultiNamespacedCacheBuilder(relatedNamespaces)
} else {
r.GetLogger().Info("create cache")
}

cche, err := cacheFunc(r.localMgr.GetConfig(), cache.Options{
Scheme: r.localMgr.GetScheme(),
Mapper: r.localMgr.GetRESTMapper(),
})
Expand Down
17 changes: 17 additions & 0 deletions docs/resource-sync-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Resource Sync Rule

### Limit namespace cache
In larger cluster with namespaces more than 30-40, it was observed that CR-controller watches/caches all the namespaces into the pod memory,
which caused frequent OOMKilled issue while attaching peer clusters.

To reduce memory caching by cluster-registry-controller we can use `namespaces` field in `ResourceSyncRule` to allow caching on selected
namespaces.

custom-resource
`spec.rules.match.namespaces`
```
namespaces:
items:
type: string
type: array
```
2 changes: 1 addition & 1 deletion pkg/clusters/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestClusterFeatures(t *testing.T) {
cf2 := clusters.NewClusterFeature("another-test-feature", "another-test-feature", nil)

r := clusters.NewManagedReconciler("test", logr.Discard())
c := clusters.NewManagedController("test", r, logr.Discard(), clusters.WithRequiredClusterFeatures(
c := clusters.NewManagedController("test", nil, r, logr.Discard(), clusters.WithRequiredClusterFeatures(
clusters.ClusterFeatureRequirement{
Name: "test-feature",
MatchLabels: map[string]string{
Expand Down
14 changes: 12 additions & 2 deletions pkg/clusters/managed_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ManagedControllers map[string]ManagedController

type managedController struct {
name string
relatedNamespaces []string
reconciler ManagedReconciler
log logr.Logger
mgr ctrl.Manager
Expand All @@ -66,9 +67,10 @@ func WithRequiredClusterFeatures(features ...ClusterFeatureRequirement) ManagedC
}
}

func NewManagedController(name string, r ManagedReconciler, l logr.Logger, options ...ManagedControllerOption) ManagedController {
func NewManagedController(name string, relatedNamespaces []string, r ManagedReconciler, l logr.Logger, options ...ManagedControllerOption) ManagedController {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create and use a new option here to set the related namespaces

m := &managedController{
name: name,
relatedNamespaces: relatedNamespaces,
reconciler: r,
log: l.WithName(name),
requiredClusterFeatures: make([]ClusterFeatureRequirement, 0),
Expand Down Expand Up @@ -188,7 +190,15 @@ func (c *managedController) createAndStartCache() (cache.Cache, error) {
return nil, errors.New("context is nil")
}

cche, err := cache.New(c.mgr.GetConfig(), cache.Options{
cacheFunc := cache.New
if len(c.relatedNamespaces) > 0 {
c.log.Info("create multi namespace cache", "namespaces", c.relatedNamespaces)
cacheFunc = cache.MultiNamespacedCacheBuilder(c.relatedNamespaces)
} else {
c.log.Info("create cache")
}

cche, err := cacheFunc(c.mgr.GetConfig(), cache.Options{
Scheme: c.mgr.GetScheme(),
Mapper: c.mgr.GetRESTMapper(),
})
Expand Down