Skip to content

Commit

Permalink
🌱 Code cleanup (#13)
Browse files Browse the repository at this point in the history
* Code cleanup

Signed-off-by: Tobias Wolf <[email protected]>

* Lint fix

Signed-off-by: Roman Hros <[email protected]>

---------

Signed-off-by: Tobias Wolf <[email protected]>
Signed-off-by: Roman Hros <[email protected]>
Co-authored-by: Roman Hros <[email protected]>
  • Loading branch information
NotTheEvilOne and chess-knight authored Jan 24, 2024
1 parent 971da5f commit 36591a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
55 changes: 24 additions & 31 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand All @@ -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()

Expand All @@ -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,
},
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 36591a1

Please sign in to comment.