Skip to content

Commit

Permalink
move go routines for api server ready beneath wait group
Browse files Browse the repository at this point in the history
Signed-off-by: Luther Monson <[email protected]>
  • Loading branch information
luthermonson authored and brandond committed Jul 21, 2021
1 parent c5832c1 commit 37fcb61
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
3 changes: 1 addition & 2 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ const (
)

type StartupHookArgs struct {
Wg *sync.WaitGroup
APIServerReady <-chan struct{}
KubeConfigAdmin string
Skips map[string]bool
Disables map[string]bool
}

type StartupHook func(context.Context, StartupHookArgs) error
type StartupHook func(context.Context, *sync.WaitGroup, StartupHookArgs) error

type Server struct {
ClusterCIDR cli.StringSlice
Expand Down
29 changes: 14 additions & 15 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,28 @@ func StartServer(ctx context.Context, config *Config) error {
return errors.Wrap(err, "starting kubernetes")
}

config.ControlConfig.Runtime.Handler = router(ctx, config)

if config.ControlConfig.DisableAPIServer {
go setETCDLabelsAndAnnotations(ctx, config)
} else {
go startOnAPIServerReady(ctx, config)
}
wg := &sync.WaitGroup{}
wg.Add(len(config.StartupHooks))

config.StartupHooksWg = &sync.WaitGroup{}
config.StartupHooksWg.Add(len(config.StartupHooks))
config.ControlConfig.Runtime.Handler = router(ctx, config)
shArgs := cmds.StartupHookArgs{
Wg: config.StartupHooksWg,
APIServerReady: config.ControlConfig.Runtime.APIServerReady,
KubeConfigAdmin: config.ControlConfig.Runtime.KubeConfigAdmin,
Skips: config.ControlConfig.Skips,
Disables: config.ControlConfig.Disables,
}
for _, hook := range config.StartupHooks {
if err := hook(ctx, shArgs); err != nil {
if err := hook(ctx, wg, shArgs); err != nil {
return errors.Wrap(err, "startup hook")
}
}

if config.ControlConfig.DisableAPIServer {
go setETCDLabelsAndAnnotations(ctx, config)
} else {
go startOnAPIServerReady(ctx, wg, config)
}

ip := net2.ParseIP(config.ControlConfig.BindAddress)
if ip == nil {
hostIP, err := net.ChooseHostInterface()
Expand All @@ -102,26 +101,26 @@ func StartServer(ctx context.Context, config *Config) error {
return writeKubeConfig(config.ControlConfig.Runtime.ServerCA, config)
}

func startOnAPIServerReady(ctx context.Context, config *Config) {
func startOnAPIServerReady(ctx context.Context, wg *sync.WaitGroup, config *Config) {
select {
case <-ctx.Done():
return
case <-config.ControlConfig.Runtime.APIServerReady:
if err := runControllers(ctx, config); err != nil {
if err := runControllers(ctx, wg, config); err != nil {
logrus.Fatalf("failed to start controllers: %v", err)
}
}
}

func runControllers(ctx context.Context, config *Config) error {
func runControllers(ctx context.Context, wg *sync.WaitGroup, config *Config) error {
controlConfig := &config.ControlConfig

sc, err := NewContext(ctx, controlConfig.Runtime.KubeConfigAdmin)
if err != nil {
return err
}

config.StartupHooksWg.Wait()
wg.Wait()
if err := stageFiles(ctx, sc, controlConfig); err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"context"
"sync"

"github.com/rancher/k3s/pkg/cli/cmds"
"github.com/rancher/k3s/pkg/daemons/config"
Expand All @@ -15,7 +14,6 @@ type Config struct {
Rootless bool
SupervisorPort int
StartupHooks []cmds.StartupHook
StartupHooksWg *sync.WaitGroup
LeaderControllers CustomControllers
Controllers CustomControllers
}
Expand Down

0 comments on commit 37fcb61

Please sign in to comment.