diff --git a/cmd/main.go b/cmd/main.go index cbbda4f4..247ffb3d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,19 +17,18 @@ limitations under the License. // Package main is the main package. package main +// Import packages including all Kubernetes client auth plugins: k8s.io/client-go/plugin/pkg/client/auth. import ( "flag" "os" "time" githubclient "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client" - infrastructureclusterstackxk8siov1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" + apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" "github.com/sovereignCloudStack/cluster-stack-provider-openstack/internal/controller" "k8s.io/apimachinery/pkg/runtime" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" - // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) - // to ensure that exec-entrypoint and run can make use of them. + runtimeutils "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth" "sigs.k8s.io/cluster-api/util/record" ctrl "sigs.k8s.io/controller-runtime" @@ -40,15 +39,13 @@ import ( ) var ( - scheme = runtime.NewScheme() - setupLog = ctrl.Log.WithName("setup") + k8sScheme = runtime.NewScheme() + setupLog = ctrl.Log.WithName("clusterstack-provider-openstack-setup") ) func init() { - utilruntime.Must(clientgoscheme.AddToScheme(scheme)) - - utilruntime.Must(infrastructureclusterstackxk8siov1alpha1.AddToScheme(scheme)) - //+kubebuilder:scaffold:scheme + runtimeutils.Must(scheme.AddToScheme(k8sScheme)) + runtimeutils.Must(apiv1alpha1.AddToScheme(k8sScheme)) } var ( @@ -57,19 +54,27 @@ var ( ) func main() { - var metricsAddr string - var enableLeaderElection bool - var probeAddr string + var ( + metricsAddr string + enableLeaderElection bool + probeAddr string + ) + flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") - flag.BoolVar(&enableLeaderElection, "leader-elect", false, - "Enable leader election for controller manager. "+ - "Enabling this will ensure there is only one active controller manager.") + flag.BoolVar( + &enableLeaderElection, + "leader-elect", + false, + "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.", + ) flag.StringVar(&releaseDir, "release-dir", "/tmp/downloads/", "Specify release directory for cluster-stack releases") flag.IntVar(&imageImportTimeout, "image-import-timeout", 0, "Maximum time in minutes that you allow cspo to import image. If image-import-timeout <= 0, cspo waits forever.") + opts := zap.Options{ Development: true, } + opts.BindFlags(flag.CommandLine) flag.Parse() @@ -78,22 +83,11 @@ func main() { syncPeriod := 5 * time.Minute mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - Scheme: scheme, + Scheme: k8sScheme, Metrics: metricsserver.Options{BindAddress: metricsAddr}, HealthProbeBindAddress: probeAddr, LeaderElection: enableLeaderElection, - LeaderElectionID: "26a5dd7c.clusterstack.x-k8s.io", - // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily - // when the Manager ends. This requires the binary to immediately end when the - // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly - // speeds up voluntary leader transitions as the new leader don't have to wait - // LeaseDuration time first. - // - // In the default scaffold provided, the program ends immediately after - // the manager stops, so would be fine to enable this option. However, - // if you are doing or is intended to do any operation such as perform cleanups - // after the manager stops then its usage might be unsafe. - // LeaderElectionReleaseOnCancel: true, + LeaderElectionID: "openstack.provider.clusterstack.x-k8s.io", Cache: cache.Options{ SyncPeriod: &syncPeriod, }, @@ -125,7 +119,6 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "OpenStackNodeImageRelease") os.Exit(1) } - //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 81fdf3ff..6934b309 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -24,7 +24,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - infrastructureclusterstackxk8siov1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" + apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" @@ -71,7 +71,7 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) Expect(cfg).NotTo(BeNil()) - err = infrastructureclusterstackxk8siov1alpha1.AddToScheme(scheme.Scheme) + err = apiv1alpha1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) //+kubebuilder:scaffold:scheme