Skip to content

Commit

Permalink
Limit cache for k8s native resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard committed Jan 11, 2025
1 parent fbc0b9e commit 389e8d6
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ import (
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/cache"

routev1 "github.com/openshift/api/route/v1"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
discovery2 "k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/cache"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand All @@ -46,6 +47,7 @@ import (
grafanav1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"
"github.com/grafana/grafana-operator/v5/controllers"
"github.com/grafana/grafana-operator/v5/controllers/autodetect"
"github.com/grafana/grafana-operator/v5/controllers/model"
"github.com/grafana/grafana-operator/v5/embeds"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -106,6 +108,42 @@ func main() {
watchNamespace, _ := os.LookupEnv(watchNamespaceEnvVar)
watchNamespaceSelector, _ := os.LookupEnv(watchNamespaceEnvSelector)

restConfig := ctrl.GetConfigOrDie()
// Platform detection
autodetect, err := autodetect.New(restConfig)
if err != nil {
setupLog.Error(err, "failed to setup auto-detect routine")
os.Exit(1)
}
isOpenShift, err := autodetect.IsOpenshift()
if err != nil {
setupLog.Error(err, "unable to detect the platform")
os.Exit(1)
}

cacheOptionsByObject := map[client.Object]cache.ByObject{
&v1.Deployment{}: {
Label: labels.SelectorFromSet(model.CommonLabels),
},
&corev1.Service{}: {
Label: labels.SelectorFromSet(model.CommonLabels),
},
&corev1.ServiceAccount{}: {
Label: labels.SelectorFromSet(model.CommonLabels),
},
&networkingv1.Ingress{}: {
Label: labels.SelectorFromSet(model.CommonLabels),
},
&corev1.PersistentVolumeClaim{}: {
Label: labels.SelectorFromSet(model.CommonLabels),
},
}
if isOpenShift {
cacheOptionsByObject[&routev1.Route{}] = cache.ByObject{
Label: labels.SelectorFromSet(model.CommonLabels),
}
}

controllerOptions := ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
Expand All @@ -118,6 +156,9 @@ func main() {
LeaderElection: enableLeaderElection,
LeaderElectionID: "f75f3bba.integreatly.org",
PprofBindAddress: pprofAddr,
Cache: cache.Options{
ByObject: cacheOptionsByObject,
},
}

getNamespaceConfig := func(namespaces string) map[string]cache.Config {
Expand Down Expand Up @@ -186,29 +227,17 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
defer stop()

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), controllerOptions)
mgr, err := ctrl.NewManager(restConfig, controllerOptions)
if err != nil {
setupLog.Error(err, "unable to create new manager")
os.Exit(1) //nolint
}

restConfig := ctrl.GetConfigOrDie()
autodetect, err := autodetect.New(restConfig)
if err != nil {
setupLog.Error(err, "failed to setup auto-detect routine")
os.Exit(1)
}
isOpenShift, err := autodetect.IsOpenshift()
if err != nil {
setupLog.Error(err, "unable to detect the platform")
os.Exit(1)
}

if err = (&controllers.GrafanaReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
IsOpenShift: isOpenShift,
Discovery: discovery2.NewDiscoveryClientForConfigOrDie(ctrl.GetConfigOrDie()),
Discovery: discovery2.NewDiscoveryClientForConfigOrDie(restConfig),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Grafana")
os.Exit(1)
Expand Down

0 comments on commit 389e8d6

Please sign in to comment.