Skip to content

Commit

Permalink
Fixing static check error
Browse files Browse the repository at this point in the history
Cannot call os.Exit(1) because it prevents the klog.Flush defer
  • Loading branch information
arschles committed Dec 9, 2020
1 parent d3c0d04 commit 88083ce
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,44 @@ var (
prometheusMetricsPath string
)

func (a *Adapter) makeProviderOrDie(globalHTTPTimeout time.Duration) provider.MetricsProvider {
func (a *Adapter) makeProvider(globalHTTPTimeout time.Duration) (provider.MetricsProvider, error) {
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
logger.Error(err, "failed to get the config")
os.Exit(1)
return nil, fmt.Errorf("failed to get the config (%s)", err)
}

scheme := scheme.Scheme
if err := appsv1.SchemeBuilder.AddToScheme(scheme); err != nil {
logger.Error(err, "failed to add apps/v1 scheme to runtime scheme")
os.Exit(1)
return nil, fmt.Errorf("failed to add apps/v1 scheme to runtime scheme (%s)", err)
}
if err := kedav1alpha1.SchemeBuilder.AddToScheme(scheme); err != nil {
logger.Error(err, "failed to add keda scheme to runtime scheme")
os.Exit(1)
return nil, fmt.Errorf("failed to add keda scheme to runtime scheme (%s)", err)
}

kubeclient, err := client.New(cfg, client.Options{
Scheme: scheme,
})
if err != nil {
logger.Error(err, "unable to construct new client")
os.Exit(1)
return nil, fmt.Errorf("unable to construct new client (%s)", err)
}

handler := scaling.NewScaleHandler(kubeclient, nil, scheme, globalHTTPTimeout)

namespace, err := getWatchNamespace()
if err != nil {
logger.Error(err, "failed to get watch namespace")
os.Exit(1)
return nil, fmt.Errorf("failed to get watch namespace (%s)", err)
}

prometheusServer := &prommetrics.PrometheusMetricServer{}
go func() { prometheusServer.NewServer(fmt.Sprintf(":%v", prometheusMetricsPort), prometheusMetricsPath) }()

return kedaprovider.NewProvider(logger, handler, kubeclient, namespace)
return kedaprovider.NewProvider(logger, handler, kubeclient, namespace), nil
}

func printVersion() {
Expand Down Expand Up @@ -128,10 +128,14 @@ func main() {
globalHTTPTimeoutMS, err := strconv.Atoi(globalHTTPTimeoutStr)
if err != nil {
logger.Error(err, "Invalid KEDA_HTTP_DEFAULT_TIMEOUT")
os.Exit(1)
return
}

kedaProvider := cmd.makeProviderOrDie(time.Duration(globalHTTPTimeoutMS) * time.Millisecond)
kedaProvider, err := cmd.makeProvider(time.Duration(globalHTTPTimeoutMS) * time.Millisecond)
if err != nil {
logger.Error(err, "making provider")
return
}
cmd.WithExternalMetrics(kedaProvider)

logger.Info(cmd.Message)
Expand Down

0 comments on commit 88083ce

Please sign in to comment.