From a4b2953017643b4a9a53bc5382554477069b0976 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Wed, 19 Aug 2020 13:30:51 -0700 Subject: [PATCH 1/3] add setup hook capabilities for rke2 Signed-off-by: Brian Downs --- pkg/cli/cmds/server.go | 2 ++ pkg/cli/server/server.go | 2 ++ pkg/server/server.go | 4 ++++ pkg/server/types.go | 1 + 4 files changed, 9 insertions(+) diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index fbd3c8d8b3..077f21e320 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -1,6 +1,7 @@ package cmds import ( + "github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/version" "github.com/rancher/spur/cli" "github.com/rancher/spur/cli/altsrc" @@ -54,6 +55,7 @@ type Server struct { ClusterInit bool ClusterReset bool EncryptSecrets bool + SetupHooks []func(config.Control) error } var ServerConfig Server diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index c911b7ed6e..b9a441c309 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -193,6 +193,8 @@ func run(app *cli.Context, cfg *cmds.Server) error { return errors.Wrap(err, "Invalid tls-min-version") } + serverConfig.SetupHooks = append(serverConfig.SetupHooks, cfg.SetupHooks...) + // TLS config based on mozilla ssl-config generator // https://ssl-config.mozilla.org/#server=golang&version=1.13.6&config=intermediate&guideline=5.4 // Need to disable the TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 Cipher for TLS1.2 diff --git a/pkg/server/server.go b/pkg/server/server.go index 7c397e647d..f44f9e8c21 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -60,6 +60,10 @@ func StartServer(ctx context.Context, config *Config) error { return errors.Wrap(err, "starting tls server") } + for _, hook := range config.SetupHooks { + hook(config.ControlConfig) + } + ip := net2.ParseIP(config.ControlConfig.BindAddress) if ip == nil { hostIP, err := net.ChooseHostInterface() diff --git a/pkg/server/types.go b/pkg/server/types.go index 9bcc2f3639..204ec93053 100644 --- a/pkg/server/types.go +++ b/pkg/server/types.go @@ -10,4 +10,5 @@ type Config struct { ControlConfig config.Control Rootless bool SupervisorPort int + SetupHooks []func(config.Control) error } From fa2c1422b3e12b383adcef99dd5361a9d418b671 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Wed, 19 Aug 2020 14:30:53 -0700 Subject: [PATCH 2/3] change name of variable Signed-off-by: Brian Downs --- pkg/cli/cmds/server.go | 2 +- pkg/cli/server/server.go | 2 +- pkg/server/server.go | 2 +- pkg/server/types.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index 077f21e320..9ebc003349 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -55,7 +55,7 @@ type Server struct { ClusterInit bool ClusterReset bool EncryptSecrets bool - SetupHooks []func(config.Control) error + StartupHooks []func(config.Control) error } var ServerConfig Server diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index b9a441c309..058ad7abc4 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -193,7 +193,7 @@ func run(app *cli.Context, cfg *cmds.Server) error { return errors.Wrap(err, "Invalid tls-min-version") } - serverConfig.SetupHooks = append(serverConfig.SetupHooks, cfg.SetupHooks...) + serverConfig.StartupHooks = append(serverConfig.StartupHooks, cfg.StartupHooks...) // TLS config based on mozilla ssl-config generator // https://ssl-config.mozilla.org/#server=golang&version=1.13.6&config=intermediate&guideline=5.4 diff --git a/pkg/server/server.go b/pkg/server/server.go index f44f9e8c21..90019ff6c1 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -60,7 +60,7 @@ func StartServer(ctx context.Context, config *Config) error { return errors.Wrap(err, "starting tls server") } - for _, hook := range config.SetupHooks { + for _, hook := range config.StartupHooks { hook(config.ControlConfig) } diff --git a/pkg/server/types.go b/pkg/server/types.go index 204ec93053..b92f11330a 100644 --- a/pkg/server/types.go +++ b/pkg/server/types.go @@ -10,5 +10,5 @@ type Config struct { ControlConfig config.Control Rootless bool SupervisorPort int - SetupHooks []func(config.Control) error + StartupHooks []func(config.Control) error } From 324bb55986ecc30d8cbb0ab2f55d6635611f0bc8 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Wed, 19 Aug 2020 16:54:58 -0700 Subject: [PATCH 3/3] add ctx to hook, handle hook errors Signed-off-by: Brian Downs --- pkg/cli/cmds/server.go | 4 +++- pkg/server/server.go | 4 +++- pkg/server/types.go | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index 9ebc003349..5746ac59d6 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -1,6 +1,8 @@ package cmds import ( + "context" + "github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/version" "github.com/rancher/spur/cli" @@ -55,7 +57,7 @@ type Server struct { ClusterInit bool ClusterReset bool EncryptSecrets bool - StartupHooks []func(config.Control) error + StartupHooks []func(context.Context, config.Control) error } var ServerConfig Server diff --git a/pkg/server/server.go b/pkg/server/server.go index 90019ff6c1..12bb4a0cc2 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -61,7 +61,9 @@ func StartServer(ctx context.Context, config *Config) error { } for _, hook := range config.StartupHooks { - hook(config.ControlConfig) + if err := hook(ctx, config.ControlConfig); err != nil { + return errors.Wrap(err, "startup hook") + } } ip := net2.ParseIP(config.ControlConfig.BindAddress) diff --git a/pkg/server/types.go b/pkg/server/types.go index b92f11330a..69f96389e1 100644 --- a/pkg/server/types.go +++ b/pkg/server/types.go @@ -1,6 +1,8 @@ package server import ( + "context" + "github.com/rancher/k3s/pkg/daemons/config" ) @@ -10,5 +12,5 @@ type Config struct { ControlConfig config.Control Rootless bool SupervisorPort int - StartupHooks []func(config.Control) error + StartupHooks []func(context.Context, config.Control) error }