Skip to content

Commit

Permalink
explicitly handle SIGINT and SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanford committed Sep 11, 2024
1 parent 1f9e725 commit 6a917e9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 78 deletions.
22 changes: 13 additions & 9 deletions cmd/configmap-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/operator-framework/operator-registry/pkg/server"
Expand Down Expand Up @@ -59,6 +58,9 @@ func main() {
}

func runCmdFunc(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
Expand Down Expand Up @@ -99,7 +101,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
logger := logrus.WithFields(logrus.Fields{"configMapName": configMapName, "configMapNamespace": configMapNamespace, "port": port})

client := NewClientFromConfig(kubeconfig, logger.Logger)
configMap, err := client.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
configMap, err := client.CoreV1().ConfigMaps(configMapNamespace).Get(ctx, configMapName, metav1.GetOptions{})
if err != nil {
logger.Fatalf("error getting configmap: %s", err)
}
Expand All @@ -113,7 +115,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err := sqlLoader.Migrate(context.TODO()); err != nil {
if err := sqlLoader.Migrate(ctx); err != nil {
return err
}

Expand All @@ -136,7 +138,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
}

// sanity check that the db is available
tables, err := store.ListTables(context.TODO())
tables, err := store.ListTables(ctx)
if err != nil {
logger.WithError(err).Warnf("couldn't list tables in db")
}
Expand All @@ -154,12 +156,14 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)

logger.Info("serving registry")
return graceful.Shutdown(logger, func() error {
return s.Serve(lis)
}, func() {
go func() {
<-ctx.Done()
logger.Info("shutting down server")
s.GracefulStop()
})
}()

logger.Info("serving registry")
return s.Serve(lis)
}

// NewClient creates a kubernetes client or bails out on on failures.
Expand Down
9 changes: 8 additions & 1 deletion cmd/opm/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

utilerrors "k8s.io/apimachinery/pkg/util/errors"

Expand All @@ -12,7 +15,11 @@ import (
func main() {
showAlphaHelp := os.Getenv("HELP_ALPHA") == "true"
cmd := root.NewCmd(showAlphaHelp)
if err := cmd.Execute(); err != nil {

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

if err := cmd.ExecuteContext(ctx); err != nil {
agg, ok := err.(utilerrors.Aggregate)
if !ok {
os.Exit(1)
Expand Down
35 changes: 20 additions & 15 deletions cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
"github.com/operator-framework/operator-registry/pkg/server"
Expand Down Expand Up @@ -54,6 +53,9 @@ func newRegistryServeCmd() *cobra.Command {
}

func serveFunc(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
Expand Down Expand Up @@ -93,19 +95,23 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
return err
}

if _, err := db.ExecContext(context.TODO(), `PRAGMA soft_heap_limit=1`); err != nil {
if _, err := db.ExecContext(ctx, `PRAGMA soft_heap_limit=1`); err != nil {
logger.WithError(err).Warnf("error setting soft heap limit for sqlite")
}

// migrate to the latest version
if err := migrate(cmd, db); err != nil {
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
if err != nil {
return err
}
if err := migrate(ctx, shouldSkipMigrate, db); err != nil {
logger.WithError(err).Warnf("couldn't migrate db")
}

store := sqlite.NewSQLLiteQuerierFromDb(db, sqlite.OmitManifests(true))

// sanity check that the db is available
tables, err := store.ListTables(context.TODO())
tables, err := store.ListTables(ctx)
if err != nil {
logger.WithError(err).Warnf("couldn't list tables in db")
}
Expand Down Expand Up @@ -142,19 +148,18 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)
logger.Info("serving registry")
return graceful.Shutdown(logger, func() error {
return s.Serve(lis)
}, func() {

go func() {
<-ctx.Done()
logger.Info("shutting down server")
s.GracefulStop()
})
}()

logger.Info("serving registry")
return s.Serve(lis)
}

func migrate(cmd *cobra.Command, db *sql.DB) error {
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
if err != nil {
return err
}
func migrate(ctx context.Context, shouldSkipMigrate bool, db *sql.DB) error {
if shouldSkipMigrate {
return nil
}
Expand All @@ -167,5 +172,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
return fmt.Errorf("failed to load migrator")
}

return migrator.Migrate(context.TODO())
return migrator.Migrate(ctx)
}
13 changes: 8 additions & 5 deletions cmd/opm/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/cache"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/server"
)
Expand Down Expand Up @@ -91,6 +90,9 @@ will not be reflected in the served content.
}

func (s *serve) run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

mainLogger := s.logger.Dup()
p := newProfilerInterface(s.pprofAddr, mainLogger)
if err := p.startEndpoint(); err != nil {
Expand Down Expand Up @@ -169,15 +171,16 @@ func (s *serve) run(ctx context.Context) error {
mainLogger.Info("serving registry")
p.stopCpuProfileCache()

return graceful.Shutdown(s.logger, func() error {
return grpcServer.Serve(lis)
}, func() {
go func() {
<-ctx.Done()
mainLogger.Info("shutting down server")
grpcServer.GracefulStop()
if err := p.stopEndpoint(ctx); err != nil {
mainLogger.Warnf("error shutting down pprof server: %v", err)
}
})
}()

return grpcServer.Serve(lis)
}

// manages an HTTP pprof endpoint served by `server`,
Expand Down
34 changes: 19 additions & 15 deletions cmd/registry-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
"github.com/operator-framework/operator-registry/pkg/server"
Expand Down Expand Up @@ -58,6 +57,9 @@ func main() {
}

func runCmdFunc(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
Expand Down Expand Up @@ -95,19 +97,23 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
return err
}

if _, err := db.ExecContext(context.TODO(), `PRAGMA soft_heap_limit=1`); err != nil {
if _, err := db.ExecContext(ctx, `PRAGMA soft_heap_limit=1`); err != nil {
logger.WithError(err).Warnf("error setting soft heap limit for sqlite")
}

// migrate to the latest version
if err := migrate(cmd, db); err != nil {
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
if err != nil {
return err
}
if err := migrate(ctx, shouldSkipMigrate, db); err != nil {
logger.WithError(err).Warnf("couldn't migrate db")
}

store := sqlite.NewSQLLiteQuerierFromDb(db, sqlite.OmitManifests(true))

// sanity check that the db is available
tables, err := store.ListTables(context.TODO())
tables, err := store.ListTables(ctx)
if err != nil {
logger.WithError(err).Warnf("couldn't list tables in db")
}
Expand All @@ -124,20 +130,18 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)
logger.Info("serving registry")

return graceful.Shutdown(logger, func() error {
return s.Serve(lis)
}, func() {
go func() {
<-ctx.Done()
logger.Info("shutting down server")
s.GracefulStop()
})
}()

logger.Info("serving registry")
return s.Serve(lis)
}

func migrate(cmd *cobra.Command, db *sql.DB) error {
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
if err != nil {
return err
}
func migrate(ctx context.Context, shouldSkipMigrate bool, db *sql.DB) error {
if shouldSkipMigrate {
return nil
}
Expand All @@ -150,5 +154,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
return fmt.Errorf("failed to load migrator")
}

return migrator.Migrate(context.TODO())
return migrator.Migrate(ctx)
}
33 changes: 0 additions & 33 deletions pkg/lib/graceful/shutdown.go

This file was deleted.

0 comments on commit 6a917e9

Please sign in to comment.