@@ -34,10 +34,13 @@ import (
3434	_ "k8s.io/component-base/logs/json/register" 
3535	"k8s.io/klog/v2" 
3636	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 
37+ 	"sigs.k8s.io/cluster-api/util/flags" 
3738	ctrl "sigs.k8s.io/controller-runtime" 
38- 	"sigs.k8s.io/controller-runtime/pkg/client" 
39+ 	cache "sigs.k8s.io/controller-runtime/pkg/cache" 
40+ 	client "sigs.k8s.io/controller-runtime/pkg/client" 
3941	"sigs.k8s.io/controller-runtime/pkg/client/config" 
4042	"sigs.k8s.io/controller-runtime/pkg/controller" 
43+ 	"sigs.k8s.io/controller-runtime/pkg/webhook" 
4144
4245	infrav1alpha5 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha5" 
4346	infrav1alpha6 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha6" 
5457	setupLog  =  ctrl .Log .WithName ("setup" )
5558
5659	// flags. 
57- 	metricsBindAddr               string 
60+ 	diagnosticsOptions           =  flags. DiagnosticsOptions {} 
5861	enableLeaderElection         bool 
5962	leaderElectionLeaseDuration  time.Duration 
6063	leaderElectionRenewDeadline  time.Duration 
@@ -91,8 +94,7 @@ func InitFlags(fs *pflag.FlagSet) {
9194	logs .AddFlags (fs , logs .SkipLoggingConfigurationFlags ())
9295	logsv1 .AddFlags (logOptions , fs )
9396
94- 	fs .StringVar (& metricsBindAddr , "metrics-bind-addr" , "localhost:8080" ,
95- 		"The address the metric endpoint binds to." )
97+ 	flags .AddDiagnosticsOptions (fs , & diagnosticsOptions )
9698
9799	fs .BoolVar (& enableLeaderElection , "leader-elect" , false ,
98100		"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager." )
@@ -143,6 +145,10 @@ func InitFlags(fs *pflag.FlagSet) {
143145	fs .BoolVar (& showVersion , "version" , false , "Show current version and exit." )
144146}
145147
148+ // Add RBAC for the authorized diagnostics endpoint. 
149+ // +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create 
150+ // +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create 
151+ 
146152func  main () {
147153	InitFlags (pflag .CommandLine )
148154	pflag .CommandLine .SetNormalizeFunc (cliflag .WordSepNormalizeFunc )
@@ -183,22 +189,41 @@ func main() {
183189		}
184190	}
185191
192+ 	diagnosticsOpts  :=  flags .GetDiagnosticsOptions (diagnosticsOptions )
193+ 
194+ 	var  watchNamespaces  map [string ]cache.Config 
195+ 	if  watchNamespace  !=  ""  {
196+ 		watchNamespaces  =  map [string ]cache.Config {
197+ 			watchNamespace : {},
198+ 		}
199+ 	}
200+ 
186201	mgr , err  :=  ctrl .NewManager (cfg , ctrl.Options {
187- 		Scheme :             scheme ,
188- 		MetricsBindAddress : metricsBindAddr ,
189- 		LeaderElection :     enableLeaderElection ,
190- 		LeaderElectionID :   "controller-leader-election-capo" ,
191- 		LeaseDuration :      & leaderElectionLeaseDuration ,
192- 		RenewDeadline :      & leaderElectionRenewDeadline ,
193- 		RetryPeriod :        & leaderElectionRetryPeriod ,
194- 		Namespace :          watchNamespace ,
195- 		SyncPeriod :         & syncPeriod ,
196- 		ClientDisableCacheFor : []client.Object {
197- 			& corev1.ConfigMap {},
198- 			& corev1.Secret {},
202+ 		Scheme :           scheme ,
203+ 		Metrics :          diagnosticsOpts ,
204+ 		LeaderElection :   enableLeaderElection ,
205+ 		LeaderElectionID : "controller-leader-election-capo" ,
206+ 		LeaseDuration :    & leaderElectionLeaseDuration ,
207+ 		RenewDeadline :    & leaderElectionRenewDeadline ,
208+ 		RetryPeriod :      & leaderElectionRetryPeriod ,
209+ 		Cache : cache.Options {
210+ 			DefaultNamespaces : watchNamespaces ,
211+ 			SyncPeriod :        & syncPeriod ,
212+ 		},
213+ 		Client : client.Options {
214+ 			Cache : & client.CacheOptions {
215+ 				DisableFor : []client.Object {
216+ 					& corev1.ConfigMap {},
217+ 					& corev1.Secret {},
218+ 				},
219+ 			},
199220		},
200- 		Port :                   webhookPort ,
201- 		CertDir :                webhookCertDir ,
221+ 		WebhookServer : webhook .NewServer (
222+ 			webhook.Options {
223+ 				Port :    webhookPort ,
224+ 				CertDir : webhookCertDir ,
225+ 			},
226+ 		),
202227		HealthProbeBindAddress : healthAddr ,
203228	})
204229	if  err  !=  nil  {
0 commit comments