Skip to content

Commit 66733c7

Browse files
authored
explicitly handle SIGINT and SIGTERM (#1448)
1 parent 0daf90f commit 66733c7

File tree

6 files changed

+68
-78
lines changed

6 files changed

+68
-78
lines changed

cmd/configmap-server/main.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/operator-framework/operator-registry/pkg/api"
1919
"github.com/operator-framework/operator-registry/pkg/lib/dns"
20-
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
2120
"github.com/operator-framework/operator-registry/pkg/lib/log"
2221
"github.com/operator-framework/operator-registry/pkg/registry"
2322
"github.com/operator-framework/operator-registry/pkg/server"
@@ -59,6 +58,9 @@ func main() {
5958
}
6059

6160
func runCmdFunc(cmd *cobra.Command, args []string) error {
61+
ctx, cancel := context.WithCancel(cmd.Context())
62+
defer cancel()
63+
6264
// Immediately set up termination log
6365
terminationLogPath, err := cmd.Flags().GetString("termination-log")
6466
if err != nil {
@@ -99,7 +101,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
99101
logger := logrus.WithFields(logrus.Fields{"configMapName": configMapName, "configMapNamespace": configMapNamespace, "port": port})
100102

101103
client := NewClientFromConfig(kubeconfig, logger.Logger)
102-
configMap, err := client.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
104+
configMap, err := client.CoreV1().ConfigMaps(configMapNamespace).Get(ctx, configMapName, metav1.GetOptions{})
103105
if err != nil {
104106
logger.Fatalf("error getting configmap: %s", err)
105107
}
@@ -113,7 +115,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
113115
if err != nil {
114116
return err
115117
}
116-
if err := sqlLoader.Migrate(context.TODO()); err != nil {
118+
if err := sqlLoader.Migrate(ctx); err != nil {
117119
return err
118120
}
119121

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

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

157-
logger.Info("serving registry")
158-
return graceful.Shutdown(logger, func() error {
159-
return s.Serve(lis)
160-
}, func() {
159+
go func() {
160+
<-ctx.Done()
161+
logger.Info("shutting down server")
161162
s.GracefulStop()
162-
})
163+
}()
164+
165+
logger.Info("serving registry")
166+
return s.Serve(lis)
163167
}
164168

165169
// NewClient creates a kubernetes client or bails out on on failures.

cmd/opm/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package main
22

33
import (
4+
"context"
45
"os"
6+
"os/signal"
7+
"syscall"
58

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

@@ -12,7 +15,11 @@ import (
1215
func main() {
1316
showAlphaHelp := os.Getenv("HELP_ALPHA") == "true"
1417
cmd := root.NewCmd(showAlphaHelp)
15-
if err := cmd.Execute(); err != nil {
18+
19+
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
20+
defer cancel()
21+
22+
if err := cmd.ExecuteContext(ctx); err != nil {
1623
agg, ok := err.(utilerrors.Aggregate)
1724
if !ok {
1825
os.Exit(1)

cmd/opm/registry/serve.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/operator-framework/operator-registry/pkg/api"
1919
"github.com/operator-framework/operator-registry/pkg/lib/dns"
20-
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
2120
"github.com/operator-framework/operator-registry/pkg/lib/log"
2221
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
2322
"github.com/operator-framework/operator-registry/pkg/server"
@@ -54,6 +53,9 @@ func newRegistryServeCmd() *cobra.Command {
5453
}
5554

5655
func serveFunc(cmd *cobra.Command, _ []string) error {
56+
ctx, cancel := context.WithCancel(cmd.Context())
57+
defer cancel()
58+
5759
// Immediately set up termination log
5860
terminationLogPath, err := cmd.Flags().GetString("termination-log")
5961
if err != nil {
@@ -93,19 +95,23 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
9395
return err
9496
}
9597

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

100102
// migrate to the latest version
101-
if err := migrate(cmd, db); err != nil {
103+
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
104+
if err != nil {
105+
return err
106+
}
107+
if err := migrate(ctx, shouldSkipMigrate, db); err != nil {
102108
logger.WithError(err).Warnf("couldn't migrate db")
103109
}
104110

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

107113
// sanity check that the db is available
108-
tables, err := store.ListTables(context.TODO())
114+
tables, err := store.ListTables(ctx)
109115
if err != nil {
110116
logger.WithError(err).Warnf("couldn't list tables in db")
111117
}
@@ -142,19 +148,18 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
142148
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
143149
health.RegisterHealthServer(s, server.NewHealthServer())
144150
reflection.Register(s)
145-
logger.Info("serving registry")
146-
return graceful.Shutdown(logger, func() error {
147-
return s.Serve(lis)
148-
}, func() {
151+
152+
go func() {
153+
<-ctx.Done()
154+
logger.Info("shutting down server")
149155
s.GracefulStop()
150-
})
156+
}()
157+
158+
logger.Info("serving registry")
159+
return s.Serve(lis)
151160
}
152161

153-
func migrate(cmd *cobra.Command, db *sql.DB) error {
154-
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
155-
if err != nil {
156-
return err
157-
}
162+
func migrate(ctx context.Context, shouldSkipMigrate bool, db *sql.DB) error {
158163
if shouldSkipMigrate {
159164
return nil
160165
}
@@ -167,5 +172,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
167172
return fmt.Errorf("failed to load migrator")
168173
}
169174

170-
return migrator.Migrate(context.TODO())
175+
return migrator.Migrate(ctx)
171176
}

cmd/opm/serve/serve.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/operator-framework/operator-registry/pkg/api"
2424
"github.com/operator-framework/operator-registry/pkg/cache"
2525
"github.com/operator-framework/operator-registry/pkg/lib/dns"
26-
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
2726
"github.com/operator-framework/operator-registry/pkg/lib/log"
2827
"github.com/operator-framework/operator-registry/pkg/server"
2928
)
@@ -91,6 +90,9 @@ will not be reflected in the served content.
9190
}
9291

9392
func (s *serve) run(ctx context.Context) error {
93+
ctx, cancel := context.WithCancel(ctx)
94+
defer cancel()
95+
9496
mainLogger := s.logger.Dup()
9597
p := newProfilerInterface(s.pprofAddr, mainLogger)
9698
if err := p.startEndpoint(); err != nil {
@@ -169,15 +171,16 @@ func (s *serve) run(ctx context.Context) error {
169171
mainLogger.Info("serving registry")
170172
p.stopCpuProfileCache()
171173

172-
return graceful.Shutdown(s.logger, func() error {
173-
return grpcServer.Serve(lis)
174-
}, func() {
174+
go func() {
175+
<-ctx.Done()
176+
mainLogger.Info("shutting down server")
175177
grpcServer.GracefulStop()
176178
if err := p.stopEndpoint(ctx); err != nil {
177179
mainLogger.Warnf("error shutting down pprof server: %v", err)
178180
}
179-
})
181+
}()
180182

183+
return grpcServer.Serve(lis)
181184
}
182185

183186
// manages an HTTP pprof endpoint served by `server`,

cmd/registry-server/main.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/operator-framework/operator-registry/pkg/api"
1717
"github.com/operator-framework/operator-registry/pkg/lib/dns"
18-
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
1918
"github.com/operator-framework/operator-registry/pkg/lib/log"
2019
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
2120
"github.com/operator-framework/operator-registry/pkg/server"
@@ -58,6 +57,9 @@ func main() {
5857
}
5958

6059
func runCmdFunc(cmd *cobra.Command, args []string) error {
60+
ctx, cancel := context.WithCancel(cmd.Context())
61+
defer cancel()
62+
6163
// Immediately set up termination log
6264
terminationLogPath, err := cmd.Flags().GetString("termination-log")
6365
if err != nil {
@@ -95,19 +97,23 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
9597
return err
9698
}
9799

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

102104
// migrate to the latest version
103-
if err := migrate(cmd, db); err != nil {
105+
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
106+
if err != nil {
107+
return err
108+
}
109+
if err := migrate(ctx, shouldSkipMigrate, db); err != nil {
104110
logger.WithError(err).Warnf("couldn't migrate db")
105111
}
106112

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

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

129-
return graceful.Shutdown(logger, func() error {
130-
return s.Serve(lis)
131-
}, func() {
134+
go func() {
135+
<-ctx.Done()
136+
logger.Info("shutting down server")
132137
s.GracefulStop()
133-
})
138+
}()
139+
140+
logger.Info("serving registry")
141+
return s.Serve(lis)
134142
}
135143

136-
func migrate(cmd *cobra.Command, db *sql.DB) error {
137-
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
138-
if err != nil {
139-
return err
140-
}
144+
func migrate(ctx context.Context, shouldSkipMigrate bool, db *sql.DB) error {
141145
if shouldSkipMigrate {
142146
return nil
143147
}
@@ -150,5 +154,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
150154
return fmt.Errorf("failed to load migrator")
151155
}
152156

153-
return migrator.Migrate(context.TODO())
157+
return migrator.Migrate(ctx)
154158
}

pkg/lib/graceful/shutdown.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)