Skip to content

Commit

Permalink
Use Context to Drive Server Shutdown (#2774)
Browse files Browse the repository at this point in the history
Turns out k8s apiserver supports both and we can get the core context to
listen on instead of just getting the close channel.
  • Loading branch information
martinmaly authored Feb 10, 2022
1 parent b725aa5 commit c60c96c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
4 changes: 2 additions & 2 deletions porch/apiserver/cmd/porch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func run() int {
http.DefaultTransport = otelhttp.NewTransport(http.DefaultClient.Transport)
http.DefaultClient.Transport = http.DefaultTransport

stopCh := genericapiserver.SetupSignalHandler()
ctx := genericapiserver.SetupSignalContext()

options := server.NewPorchServerOptions(os.Stdout, os.Stderr)
cmd := server.NewCommandStartPorchServer(options, stopCh)
cmd := server.NewCommandStartPorchServer(ctx, options)
code := cli.Run(cmd)
return code
}
Expand Down
7 changes: 4 additions & 3 deletions porch/apiserver/pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package apiserver

import (
"context"
"fmt"

"github.com/GoogleContainerTools/kpt/porch/api/porch/install"
Expand Down Expand Up @@ -188,7 +189,7 @@ func (c completedConfig) New() (*PorchServer, error) {
return s, nil
}

func (s *PorchServer) Run(stopCh <-chan struct{}) error {
porch.RunBackground(s.coreClient, s.cache, stopCh)
return s.GenericAPIServer.PrepareRun().Run(stopCh)
func (s *PorchServer) Run(ctx context.Context) error {
porch.RunBackground(ctx, s.coreClient, s.cache)
return s.GenericAPIServer.PrepareRun().Run(ctx.Done())
}
9 changes: 5 additions & 4 deletions porch/apiserver/pkg/cmd/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package server

import (
"context"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -80,7 +81,7 @@ func NewPorchServerOptions(out, errOut io.Writer) *PorchServerOptions {

// NewCommandStartPorchServer provides a CLI handler for 'start master' command
// with a default PorchServerOptions.
func NewCommandStartPorchServer(defaults *PorchServerOptions, stopCh <-chan struct{}) *cobra.Command {
func NewCommandStartPorchServer(ctx context.Context, defaults *PorchServerOptions) *cobra.Command {
o := *defaults
cmd := &cobra.Command{
Short: "Launch a porch API server",
Expand All @@ -92,7 +93,7 @@ func NewCommandStartPorchServer(defaults *PorchServerOptions, stopCh <-chan stru
if err := o.Validate(args); err != nil {
return err
}
if err := o.RunPorchServer(stopCh); err != nil {
if err := o.RunPorchServer(ctx); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -187,7 +188,7 @@ func (o *PorchServerOptions) Config() (*apiserver.Config, error) {
}

// RunPorchServer starts a new PorchServer given PorchServerOptions
func (o PorchServerOptions) RunPorchServer(stopCh <-chan struct{}) error {
func (o PorchServerOptions) RunPorchServer(ctx context.Context) error {
config, err := o.Config()
if err != nil {
return err
Expand All @@ -206,7 +207,7 @@ func (o PorchServerOptions) RunPorchServer(stopCh <-chan struct{}) error {
})
}

return server.Run(stopCh)
return server.Run(ctx)
}

func (o *PorchServerOptions) AddFlags(fs *pflag.FlagSet) {
Expand Down
13 changes: 4 additions & 9 deletions porch/apiserver/pkg/registry/porch/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ import (
"github.com/GoogleContainerTools/kpt/porch/repository/pkg/cache"
)

func RunBackground(coreClient client.WithWatch, cache *cache.Cache, stopCh <-chan struct{}) {
func RunBackground(ctx context.Context, coreClient client.WithWatch, cache *cache.Cache) {
b := background{
coreClient: coreClient,
cache: cache,
}
ctx := context.Background()
go b.run(ctx, stopCh)
go b.run(ctx)
}

// background manages background tasks
Expand All @@ -43,8 +42,8 @@ type background struct {
cache *cache.Cache
}

// run will run until ctx is done or stopCh is closed
func (b *background) run(ctx context.Context, stopCh <-chan struct{}) {
// run will run until ctx is done
func (b *background) run(ctx context.Context) {
klog.Infof("Background routine starting ...")

// Repository watch.
Expand Down Expand Up @@ -111,10 +110,6 @@ loop:
klog.Infof("Background routine exiting; context done")
}
break loop

case <-stopCh:
klog.Info("Background routine exiting; stop channel closed")
break loop
}
}
}
Expand Down

0 comments on commit c60c96c

Please sign in to comment.