optimize config package

Signed-off-by: x893675 <x893675@icloud.com>
This commit is contained in:
x893675
2022-02-22 10:20:11 +08:00
parent 2a521bb157
commit f5bcbda0c9
5 changed files with 90 additions and 82 deletions

View File

@@ -220,6 +220,8 @@ func (s *KubeSphereControllerManagerOptions) bindLeaderElectionFlags(l *leaderel
"of a leadership. This is only applicable if leader election is enabled.")
}
// MergeConfig merge new config without validation
// When misconfigured, the app should just crash directly
func (s *KubeSphereControllerManagerOptions) MergeConfig(cfg *controllerconfig.Config) {
s.KubernetesOptions = cfg.KubernetesOptions
s.DevopsOptions = cfg.DevopsOptions

View File

@@ -48,7 +48,7 @@ import (
func NewControllerManagerCommand() *cobra.Command {
s := options.NewKubeSphereControllerManagerOptions()
conf, configCh, err := controllerconfig.TryLoadFromDisk()
conf, err := controllerconfig.TryLoadFromDisk()
if err == nil {
// make sure LeaderElection is not nil
s = &options.KubeSphereControllerManagerOptions{
@@ -78,8 +78,7 @@ func NewControllerManagerCommand() *cobra.Command {
klog.Error(utilerrors.NewAggregate(errs))
os.Exit(1)
}
if err = Run(s, configCh, signals.SetupSignalHandler()); err != nil {
if err = Run(s, controllerconfig.WatchConfigChange(), signals.SetupSignalHandler()); err != nil {
klog.Error(err)
os.Exit(1)
}
@@ -115,31 +114,34 @@ func NewControllerManagerCommand() *cobra.Command {
}
func Run(s *options.KubeSphereControllerManagerOptions, configCh <-chan controllerconfig.Config, ctx context.Context) error {
ictx := controllerconfig.Context{}
ictx.RenewContext(context.TODO())
ictx, cancelFunc := context.WithCancel(context.TODO())
errCh := make(chan error)
defer close(errCh)
go func() {
if err := run(s, ictx.GetContext()); err != nil {
if err := run(s, ictx); err != nil {
errCh <- err
}
}()
// The ctx (signals.SetupSignalHandler()) is to control the entire program life cycle,
// The ictx(internal context) is created here to control the life cycle of the controller-manager(all controllers, sharedInformer, webhook etc.)
// when config changed, stop server and renew context, start new server
for {
select {
case <-ctx.Done():
ictx.CancelContext()
cancelFunc()
return nil
case cfg := <-configCh:
ictx.CancelContext()
cancelFunc()
s.MergeConfig(&cfg)
ictx.RenewContext(context.TODO())
ictx, cancelFunc = context.WithCancel(context.TODO())
go func() {
if err := run(s, ictx.GetContext()); err != nil {
if err := run(s, ictx); err != nil {
errCh <- err
}
}()
case err := <-errCh:
cancelFunc()
return err
}
}

View File

@@ -38,7 +38,7 @@ func NewAPIServerCommand() *cobra.Command {
s := options.NewServerRunOptions()
// Load configuration from file
conf, configCh, err := apiserverconfig.TryLoadFromDisk()
conf, err := apiserverconfig.TryLoadFromDisk()
if err == nil {
s = &options.ServerRunOptions{
GenericServerRunOptions: s.GenericServerRunOptions,
@@ -57,7 +57,7 @@ cluster's shared state through which all other components interact.`,
if errs := s.Validate(); len(errs) != 0 {
return utilerrors.NewAggregate(errs)
}
return Run(s, configCh, signals.SetupSignalHandler())
return Run(s, apiserverconfig.WatchConfigChange(), signals.SetupSignalHandler())
},
SilenceUsage: true,
}
@@ -89,31 +89,34 @@ cluster's shared state through which all other components interact.`,
}
func Run(s *options.ServerRunOptions, configCh <-chan apiserverconfig.Config, ctx context.Context) error {
ictx := apiserverconfig.Context{}
ictx.RenewContext(context.TODO())
ictx, cancelFunc := context.WithCancel(context.TODO())
errCh := make(chan error)
defer close(errCh)
go func() {
if err := run(s, ictx.GetContext()); err != nil {
if err := run(s, ictx); err != nil {
errCh <- err
}
}()
// The ctx (signals.SetupSignalHandler()) is to control the entire program life cycle,
// The ictx(internal context) is created here to control the life cycle of the ks-apiserver(http server, sharedInformer etc.)
// when config change, stop server and renew context, start new server
for {
select {
case <-ctx.Done():
ictx.CancelContext()
cancelFunc()
return nil
case cfg := <-configCh:
ictx.CancelContext()
cancelFunc()
s.Config = &cfg
ictx.RenewContext(context.TODO())
ictx, cancelFunc = context.WithCancel(context.TODO())
go func() {
if err := run(s, ictx.GetContext()); err != nil {
if err := run(s, ictx); err != nil {
errCh <- err
}
}()
case err := <-errCh:
cancelFunc()
return err
}
}