move go routines for api server ready beneath wait group

Signed-off-by: Luther Monson <luther.monson@gmail.com>
This commit is contained in:
Luther Monson 2021-07-20 14:25:00 -07:00 committed by Brad Davidson
parent c5832c1128
commit 37fcb61f5e
3 changed files with 15 additions and 19 deletions

View File

@ -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

View File

@ -62,29 +62,28 @@ func StartServer(ctx context.Context, config *Config) error {
return errors.Wrap(err, "starting kubernetes")
}
wg := &sync.WaitGroup{}
wg.Add(len(config.StartupHooks))
config.ControlConfig.Runtime.Handler = router(ctx, config)
if config.ControlConfig.DisableAPIServer {
go setETCDLabelsAndAnnotations(ctx, config)
} else {
go startOnAPIServerReady(ctx, config)
}
config.StartupHooksWg = &sync.WaitGroup{}
config.StartupHooksWg.Add(len(config.StartupHooks))
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()
@ -102,18 +101,18 @@ 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)
@ -121,7 +120,7 @@ func runControllers(ctx context.Context, config *Config) error {
return err
}
config.StartupHooksWg.Wait()
wg.Wait()
if err := stageFiles(ctx, sc, controlConfig); err != nil {
return err
}

View File

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