diff --git a/main.go b/main.go index 9d3cb6490..24c2dc574 100644 --- a/main.go +++ b/main.go @@ -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. @@ -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" @@ -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{ @@ -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 { @@ -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)