From e263d87f0aac3ce6b2e8c94c66b49255143d1bc0 Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Mon, 18 Dec 2023 20:05:00 +0100 Subject: [PATCH] Code cleanup Signed-off-by: Tobias Wolf --- cmd/main.go | 55 ++++++++----------- ...openstackclusterstackrelease_controller.go | 6 +- ...kclusterstackreleasetemplate_controller.go | 6 +- .../openstacknodeimagerelease_controller.go | 6 +- internal/controller/suite_test.go | 4 +- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index d7d1ed54..94e17215 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,17 +17,16 @@ 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" - 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" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" @@ -36,51 +35,46 @@ 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)) } 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.", + ) + opts := zap.Options{ Development: true, } + opts.BindFlags(flag.CommandLine) flag.Parse() ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) 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", }) if err != nil { setupLog.Error(err, "unable to start manager") @@ -108,7 +102,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/openstackclusterstackrelease_controller.go b/internal/controller/openstackclusterstackrelease_controller.go index 03d8cad3..c0aee3ea 100644 --- a/internal/controller/openstackclusterstackrelease_controller.go +++ b/internal/controller/openstackclusterstackrelease_controller.go @@ -19,7 +19,7 @@ package controller import ( "context" - infrav1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" + apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -48,7 +48,7 @@ type OpenStackClusterStackReleaseReconciler struct { func (r *OpenStackClusterStackReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) - openstackclusterstackrelease := &infrav1alpha1.OpenStackClusterStackRelease{} + openstackclusterstackrelease := &apiv1alpha1.OpenStackClusterStackRelease{} _ = r.Client.Get(ctx, req.NamespacedName, openstackclusterstackrelease) // TODO(user): your logic here @@ -59,6 +59,6 @@ func (r *OpenStackClusterStackReleaseReconciler) Reconcile(ctx context.Context, // SetupWithManager sets up the controller with the Manager. func (r *OpenStackClusterStackReleaseReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&infrav1alpha1.OpenStackClusterStackRelease{}). + For(&apiv1alpha1.OpenStackClusterStackRelease{}). Complete(r) } diff --git a/internal/controller/openstackclusterstackreleasetemplate_controller.go b/internal/controller/openstackclusterstackreleasetemplate_controller.go index 24b24780..f1243ac7 100644 --- a/internal/controller/openstackclusterstackreleasetemplate_controller.go +++ b/internal/controller/openstackclusterstackreleasetemplate_controller.go @@ -19,7 +19,7 @@ package controller import ( "context" - infrav1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" + apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -48,7 +48,7 @@ type OpenStackClusterStackReleaseTemplateReconciler struct { func (r *OpenStackClusterStackReleaseTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) - openstackclusterstackreleasetemplate := &infrav1alpha1.OpenStackClusterStackReleaseTemplate{} + openstackclusterstackreleasetemplate := &apiv1alpha1.OpenStackClusterStackReleaseTemplate{} _ = r.Client.Get(ctx, req.NamespacedName, openstackclusterstackreleasetemplate) return ctrl.Result{}, nil @@ -57,6 +57,6 @@ func (r *OpenStackClusterStackReleaseTemplateReconciler) Reconcile(ctx context.C // SetupWithManager sets up the controller with the Manager. func (r *OpenStackClusterStackReleaseTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&infrav1alpha1.OpenStackClusterStackReleaseTemplate{}). + For(&apiv1alpha1.OpenStackClusterStackReleaseTemplate{}). Complete(r) } diff --git a/internal/controller/openstacknodeimagerelease_controller.go b/internal/controller/openstacknodeimagerelease_controller.go index f7c4b48d..084135bb 100644 --- a/internal/controller/openstacknodeimagerelease_controller.go +++ b/internal/controller/openstacknodeimagerelease_controller.go @@ -20,7 +20,7 @@ package controller import ( "context" - infrav1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" + apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -49,7 +49,7 @@ type OpenStackNodeImageReleaseReconciler struct { func (r *OpenStackNodeImageReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) - openstacknodeimagerelease := infrav1alpha1.OpenStackNodeImageRelease{} + openstacknodeimagerelease := apiv1alpha1.OpenStackNodeImageRelease{} _ = r.Client.Get(ctx, req.NamespacedName, &openstacknodeimagerelease) return ctrl.Result{}, nil @@ -58,6 +58,6 @@ func (r *OpenStackNodeImageReleaseReconciler) Reconcile(ctx context.Context, req // SetupWithManager sets up the controller with the Manager. func (r *OpenStackNodeImageReleaseReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&infrav1alpha1.OpenStackNodeImageRelease{}). + For(&apiv1alpha1.OpenStackNodeImageRelease{}). Complete(r) } 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