From 3f2bf7061a066f09ecc18ebb6f68b0af18b3eb46 Mon Sep 17 00:00:00 2001 From: gnosyslambda Date: Thu, 19 Mar 2026 14:49:23 +0900 Subject: [PATCH] refactor: replace os.Exit with error return in binoculars StartUp - StartUp now returns error instead of calling os.Exit(-1) - WaitGroup creation moved next to grpcCommon.Listen - Caller explicitly shuts down gateway and metrics before os.Exit(1) Fixes #4779 Signed-off-by: gnosyslambda --- cmd/binoculars/main.go | 8 +++++++- internal/binoculars/server.go | 18 +++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/cmd/binoculars/main.go b/cmd/binoculars/main.go index cd4e0221766..6305552c589 100644 --- a/cmd/binoculars/main.go +++ b/cmd/binoculars/main.go @@ -71,7 +71,13 @@ func main() { ) defer shutdownGateway() - shutdown, wg := binoculars.StartUp(&config) + shutdown, wg, err := binoculars.StartUp(&config) + if err != nil { + log.Errorf("Binoculars startup failed: %v", err) + shutdownGateway() + shutdownMetricServer() + os.Exit(1) + } go func() { <-stopSignal shutdown() diff --git a/internal/binoculars/server.go b/internal/binoculars/server.go index 643333a457e..06747ece09a 100644 --- a/internal/binoculars/server.go +++ b/internal/binoculars/server.go @@ -1,7 +1,7 @@ package binoculars import ( - "os" + "fmt" "sync" "github.com/armadaproject/armada/internal/binoculars/configuration" @@ -10,28 +10,22 @@ import ( "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/cluster" grpcCommon "github.com/armadaproject/armada/internal/common/grpc" - log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/api/binoculars" ) -func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup) { - wg := &sync.WaitGroup{} - wg.Add(1) - +func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup, error) { kubernetesClientProvider, err := cluster.NewKubernetesClientProvider( config.ImpersonateUsers, config.Kubernetes.QPS, config.Kubernetes.Burst, ) if err != nil { - log.Errorf("Failed to connect to kubernetes because %s", err) - os.Exit(-1) + return nil, nil, fmt.Errorf("failed to connect to kubernetes: %w", err) } authServices, err := auth.ConfigureAuth(config.Auth) if err != nil { - log.Errorf("Failed to create auth services %s", err) - os.Exit(-1) + return nil, nil, fmt.Errorf("failed to create auth services: %w", err) } grpcServer := grpcCommon.CreateGrpcServer(config.Grpc.KeepaliveParams, config.Grpc.KeepaliveEnforcementPolicy, authServices, config.Grpc.Tls) @@ -47,7 +41,9 @@ func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup) { binocularsServer := server.NewBinocularsServer(logService, cordonService) binoculars.RegisterBinocularsServer(grpcServer, binocularsServer) + wg := &sync.WaitGroup{} + wg.Add(1) grpcCommon.Listen(config.GrpcPort, grpcServer, wg) - return grpcServer.GracefulStop, wg + return grpcServer.GracefulStop, wg, nil }