Skip to content

Commit

Permalink
fix(lint): defer calls never executed
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalgalaw committed Dec 6, 2023
1 parent 34405a3 commit 013d6e3
Showing 1 changed file with 23 additions and 44 deletions.
67 changes: 23 additions & 44 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package main
import (
"context"
"fmt"
"log"
"os"
"time"

"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -37,9 +38,12 @@ func init() {
}

func main() {
exitCode := 0
defer func() { os.Exit(exitCode) }()
if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
ctrl.SetLogger(klogr.New())

// initiate flags
Expand All @@ -50,8 +54,7 @@ func main() {

// call GenerateConfig() function from config package
if err := config.GenerateConfig(f, configFile); err != nil {
setupLog.Error(err, "failed to read config")
os.Exit(1)
return fmt.Errorf("failed to read config: %w", err)
}

// check if dry-run flag is set to true
Expand All @@ -61,11 +64,10 @@ func main() {
if dryRun {
yamlConfig, err := yaml.Marshal(config.Config)
if err != nil {
setupLog.Error(err, "failed to marshal config as yaml")
os.Exit(1)
return fmt.Errorf("failed to marshal config as yaml: %w", err)
}
fmt.Printf("generated Config as yaml:\n%s\n", yamlConfig)
os.Exit(0)
return nil
}

var watchNamespaces []string
Expand Down Expand Up @@ -101,9 +103,7 @@ func main() {
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
exitCode = 1
return
return fmt.Errorf("unable to start manager: %w", err)
}

if err = (&controller.EnterpriseReconciler{
Expand All @@ -115,9 +115,7 @@ func main() {
Username: config.Config.Garm.Username,
Password: config.Config.Garm.Password,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Enterprise")
exitCode = 1
return
return fmt.Errorf("unable to create controller Enterprise: %w", err)
}
if err = (&controller.PoolReconciler{
Client: mgr.GetClient(),
Expand All @@ -128,26 +126,18 @@ func main() {
Username: config.Config.Garm.Username,
Password: config.Config.Garm.Password,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Pool")
exitCode = 1
return
return fmt.Errorf("unable to create controller Pool: %w", err)
}

if os.Getenv("CREATE_WEBHOOK") == "true" {
if err = (&garmoperatorv1alpha1.Pool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Pool")
exitCode = 1
return
return fmt.Errorf("unable to create webhook Pool: %w", err)
}
if err = (&garmoperatorv1alpha1.Image{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Image")
exitCode = 1
return
return fmt.Errorf("unable to create webhook Image: %w", err)
}
if err = (&garmoperatorv1alpha1.Repository{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Repository")
exitCode = 1
return
return fmt.Errorf("unable to create webhook Repository: %w", err)
}
}

Expand All @@ -160,9 +150,7 @@ func main() {
Username: config.Config.Garm.Username,
Password: config.Config.Garm.Password,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Organization")
exitCode = 1
return
return fmt.Errorf("unable to create controller Organization: %w", err)
}

if err = (&controller.RepositoryReconciler{
Expand All @@ -174,9 +162,7 @@ func main() {
Username: config.Config.Garm.Username,
Password: config.Config.Garm.Password,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Repository")
exitCode = 1
return
return fmt.Errorf("unable to create controller Repository: %w", err)
}

eventChan := make(chan event.GenericEvent)
Expand All @@ -191,9 +177,7 @@ func main() {

// setup controller so it can reconcile if events from eventChan are queued
if err = runnerReconciler.SetupWithManager(mgr, eventChan); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Runner")
exitCode = 1
return
return fmt.Errorf("unable to create controller Runner: %w", err)
}

// fetch runner instances periodically and enqueue reconcile events for runner ctrl if external system has changed
Expand All @@ -204,20 +188,15 @@ func main() {
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
exitCode = 1
return
return fmt.Errorf("unable to set up health check: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
exitCode = 1
return
return fmt.Errorf("unable to set up ready check: %w", err)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
exitCode = 1
return
return fmt.Errorf("unable to start manager: %w", err)
}
return nil
}

0 comments on commit 013d6e3

Please sign in to comment.