diff --git a/cmd/operator.go b/cmd/operator.go index b0ba2a4d..2fe19d97 100644 --- a/cmd/operator.go +++ b/cmd/operator.go @@ -40,6 +40,8 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/log/zap" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + "sigs.k8s.io/controller-runtime/pkg/webhook" ) var ( @@ -138,9 +140,13 @@ var controllerCmd = &cobra.Command{ func generateManagerConfig(heistConfig *HeistConfig) ctrl.Options { options := ctrl.Options{ - Scheme: scheme, - MetricsBindAddress: heistConfig.Operator.MetricsBindAddress, - Port: heistConfig.Operator.WebhookPort, + Scheme: scheme, + Metrics: metricsserver.Options{ + BindAddress: heistConfig.Operator.MetricsBindAddress, + }, + WebhookServer: webhook.NewServer(webhook.Options{ + Port: heistConfig.Operator.WebhookPort, + }), HealthProbeBindAddress: heistConfig.Operator.HealthProbeBindAddress, LeaderElection: heistConfig.Operator.LeaderElect, LeaderElectionID: heistConfig.Operator.LeaderElectionID, @@ -155,18 +161,29 @@ func generateManagerConfig(heistConfig *HeistConfig) ctrl.Options { setupLog.Info("Operator Scope: multi namespace", "namespaces", namespaces) options.NewCache = func(config *rest.Config, opts cache.Options) (cache.Cache, error) { - opts.Namespaces = namespaces + opts.DefaultNamespaces = getNamespacedCacheConfig(namespaces) return cache.New(config, opts) } return options default: setupLog.Info("Operator Scope: single namespace", "namespaces", []string{watchedNamespaces}) - options.Cache.Namespaces = []string{watchedNamespaces} + options.Cache.DefaultNamespaces = getNamespacedCacheConfig([]string{watchedNamespaces}) return options } } +func getNamespacedCacheConfig(namespaces []string) (namespaceConfig map[string]cache.Config) { + namespaceConfig = map[string]cache.Config{} + + for _, namespace := range namespaces { + // leave all cache.Config fields nil to use default settings + namespaceConfig[namespace] = cache.Config{} + } + + return namespaceConfig +} + func init() { rootCmd.AddCommand(controllerCmd) diff --git a/pkg/agent/agent_suite_test.go b/pkg/agent/agent_suite_test.go index 6dc7d2cf..af93559f 100644 --- a/pkg/agent/agent_suite_test.go +++ b/pkg/agent/agent_suite_test.go @@ -40,6 +40,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + "sigs.k8s.io/controller-runtime/pkg/webhook" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -124,12 +126,16 @@ func BeforeSuiteSetup() func() { WithVaultAPI(vaultAPI). WithRestConfig(cfg). WithOptions(controllerruntime.Options{ - Scheme: scheme.Scheme, - Host: webhookInstallOptions.LocalServingHost, - Port: webhookInstallOptions.LocalServingPort, - CertDir: webhookInstallOptions.LocalServingCertDir, - LeaderElection: false, - MetricsBindAddress: "0", + Scheme: scheme.Scheme, + WebhookServer: webhook.NewServer(webhook.Options{ + Host: webhookInstallOptions.LocalServingHost, + Port: webhookInstallOptions.LocalServingPort, + CertDir: webhookInstallOptions.LocalServingCertDir, + }), + LeaderElection: false, + Metrics: metricsserver.Options{ + BindAddress: "0", + }, }). Register(heistv1alpha1.Component()). Register(controllers.Component(&controllers.Config{})). diff --git a/pkg/agentserver/agentserver_suite_test.go b/pkg/agentserver/agentserver_suite_test.go index 3908cb21..87b0b102 100644 --- a/pkg/agentserver/agentserver_suite_test.go +++ b/pkg/agentserver/agentserver_suite_test.go @@ -43,6 +43,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + "sigs.k8s.io/controller-runtime/pkg/webhook" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -132,12 +134,16 @@ func BeforeSuiteSetup() func() { WithVaultAPI(vaultAPI). WithRestConfig(cfg). WithOptions(controllerruntime.Options{ - Scheme: scheme.Scheme, - Host: webhookInstallOptions.LocalServingHost, - Port: webhookInstallOptions.LocalServingPort, - CertDir: webhookInstallOptions.LocalServingCertDir, - LeaderElection: false, - MetricsBindAddress: "0", + Scheme: scheme.Scheme, + WebhookServer: webhook.NewServer(webhook.Options{ + Host: webhookInstallOptions.LocalServingHost, + Port: webhookInstallOptions.LocalServingPort, + CertDir: webhookInstallOptions.LocalServingCertDir, + }), + LeaderElection: false, + Metrics: metricsserver.Options{ + BindAddress: "0", + }, }). Register(heistv1alpha1.Component()). Register(controllers.Component(&controllers.Config{})). diff --git a/pkg/apis/heist.youniqx.com/v1alpha1/webhook_suite_test.go b/pkg/apis/heist.youniqx.com/v1alpha1/webhook_suite_test.go index 26adb786..1e4ca059 100644 --- a/pkg/apis/heist.youniqx.com/v1alpha1/webhook_suite_test.go +++ b/pkg/apis/heist.youniqx.com/v1alpha1/webhook_suite_test.go @@ -41,6 +41,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + "sigs.k8s.io/controller-runtime/pkg/webhook" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -136,12 +138,16 @@ var _ = BeforeSuite(func() { WithVaultAPI(vaultAPI). WithRestConfig(cfg). WithOptions(ctrl.Options{ - Scheme: scheme, - Host: webhookInstallOptions.LocalServingHost, - Port: webhookInstallOptions.LocalServingPort, - CertDir: webhookInstallOptions.LocalServingCertDir, - LeaderElection: false, - MetricsBindAddress: "0", + Scheme: scheme, + WebhookServer: webhook.NewServer(webhook.Options{ + Host: webhookInstallOptions.LocalServingHost, + Port: webhookInstallOptions.LocalServingPort, + CertDir: webhookInstallOptions.LocalServingCertDir, + }), + LeaderElection: false, + Metrics: metricsserver.Options{ + BindAddress: "0", + }, }). Register(Component()). Complete() diff --git a/pkg/injector/injector_suite_test.go b/pkg/injector/injector_suite_test.go index e47b1a2a..1896925f 100644 --- a/pkg/injector/injector_suite_test.go +++ b/pkg/injector/injector_suite_test.go @@ -29,6 +29,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + "sigs.k8s.io/controller-runtime/pkg/webhook" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -121,12 +123,16 @@ func BeforeSuiteSetup() func() { WithVaultAPI(vaultAPI). WithRestConfig(cfg). WithOptions(controllerruntime.Options{ - Scheme: scheme.Scheme, - Host: webhookInstallOptions.LocalServingHost, - Port: webhookInstallOptions.LocalServingPort, - CertDir: webhookInstallOptions.LocalServingCertDir, - LeaderElection: false, - MetricsBindAddress: "0", + Scheme: scheme.Scheme, + WebhookServer: webhook.NewServer(webhook.Options{ + Host: webhookInstallOptions.LocalServingHost, + Port: webhookInstallOptions.LocalServingPort, + CertDir: webhookInstallOptions.LocalServingCertDir, + }), + LeaderElection: false, + Metrics: metricsserver.Options{ + BindAddress: "0", + }, }). Register(heistv1alpha1.Component()). Register(controllers.Component(&controllers.Config{})).