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

do not duplicate cached clients #635

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 10 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ func main() {
}
crtConfig.Print()

discoveryClient, err := discovery.NewDiscoveryClientForConfig(ctrl.GetConfigOrDie())
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
setupLog.Error(err, "failed to create discovery client")
os.Exit(1)
}

// Webhook server will be created with default values (port 9443) as per doc - https://github.com/kubernetes-sigs/controller-runtime/blob/main/pkg/manager/manager.go#L244-L247
// Cache Options design doc - https://github.com/kubernetes-sigs/controller-runtime/blob/main/designs/cache_options.md
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
Expand All @@ -184,7 +184,11 @@ func main() {
os.Exit(1)
}

allNamespacesCluster, err := runtimecluster.New(ctrl.GetConfigOrDie(), func(options *runtimecluster.Options) {
// create a new client with a cache that watches (as opposed to the standard client) resources in all namespaces.
// This client should be used only for resources and kinds that are retrieved from other namespaces than the watched one.
// This will help keeping a reasonable memory usage for this operator since the cache won't store all other namespace scoped
// resources (secrets, etc.).
allNamespacesCluster, err := runtimecluster.New(cfg, func(options *runtimecluster.Options) {
options.Scheme = scheme
})
if err != nil {
Expand All @@ -197,12 +201,6 @@ func main() {
os.Exit(1)
}

allNamespacesClient, allNamespacesCache, err := newAllNamespacesClient(cfg)
if err != nil {
setupLog.Error(err, "")
os.Exit(1)
}

scalesClient, err := newScalesClient(cfg)
if err != nil {
setupLog.Error(err, "unable to create scales client")
Expand Down Expand Up @@ -243,7 +241,7 @@ func main() {
}
if err := (&idler.Reconciler{
Scheme: mgr.GetScheme(),
AllNamespacesClient: allNamespacesClient,
AllNamespacesClient: allNamespacesCluster.GetClient(),
Client: mgr.GetClient(),
ScalesClient: scalesClient,
DynamicClient: dynamicClient,
Expand All @@ -258,15 +256,15 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
GetHostCluster: cluster.GetHostCluster,
AllNamespacesClient: allNamespacesClient,
AllNamespacesClient: allNamespacesCluster.GetClient(),
VersionCheckManager: status.VersionCheckManager{GetGithubClientFunc: commonclient.NewGitHubClient},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MemberStatus")
os.Exit(1)
}
if err = (nstemplateset.NewReconciler(&nstemplateset.APIClient{
Client: mgr.GetClient(),
AllNamespacesClient: allNamespacesClient,
AllNamespacesClient: allNamespacesCluster.GetClient(),
Scheme: mgr.GetScheme(),
GetHostCluster: cluster.GetHostCluster,
})).SetupWithManager(mgr, allNamespacesCluster, discoveryClient); err != nil {
Expand Down Expand Up @@ -317,13 +315,6 @@ func main() {
os.Exit(1)
}

go func() {
if err := allNamespacesCache.Start(stopChannel); err != nil {
setupLog.Error(err, "failed to start all-namespaces cache")
os.Exit(1)
}
}()

setupLog.Info("starting manager")
if err := mgr.Start(stopChannel); err != nil {
setupLog.Error(err, "problem running manager")
Expand All @@ -332,20 +323,6 @@ func main() {

}

// newAllNamespacesClient creates a new client that watches (as opposed to the standard client) resources in all namespaces.
// This client should be used only for resources and kinds that are retrieved from other namespaces than the watched one.
// This will help keeping a reasonable memory usage for this operator since the cache won't store all other namespace scoped
// resources (secrets, etc.).
func newAllNamespacesClient(config *rest.Config) (client.Client, cache.Cache, error) {
clusterAllNamespaces, err := runtimecluster.New(config, func(clusterOptions *runtimecluster.Options) {
clusterOptions.Scheme = scheme
})
if err != nil {
return nil, nil, err
}
return clusterAllNamespaces.GetClient(), clusterAllNamespaces.GetCache(), nil
}

func newScalesClient(config *rest.Config) (scale.ScalesGetter, error) {
c, err := kubernetes.NewForConfig(config)
if err != nil {
Expand Down
Loading