From 869b98bc4c93fa7edb16a31b59fa7e65ce085d59 Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Wed, 28 Jul 2021 13:56:59 -0700 Subject: [PATCH] Sync DisableKubeProxy into control struct Sync DisableKubeProxy from cfg into control before sending control to clients, as it may have been modified by a startup hook. Signed-off-by: Brad Davidson --- pkg/cli/server/server.go | 2 +- pkg/server/router.go | 12 +++++++++--- pkg/server/server.go | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index 12646849e2..c6653d8738 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -368,7 +368,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont ctx := signals.SetupSignalHandler(context.Background()) - if err := server.StartServer(ctx, &serverConfig); err != nil { + if err := server.StartServer(ctx, &serverConfig, cfg); err != nil { return err } diff --git a/pkg/server/router.go b/pkg/server/router.go index 6f2d636031..15ac798da4 100644 --- a/pkg/server/router.go +++ b/pkg/server/router.go @@ -19,6 +19,7 @@ import ( "github.com/pkg/errors" certutil "github.com/rancher/dynamiclistener/cert" "github.com/rancher/k3s/pkg/bootstrap" + "github.com/rancher/k3s/pkg/cli/cmds" "github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/nodepassword" "github.com/rancher/k3s/pkg/version" @@ -31,7 +32,7 @@ const ( staticURL = "/static/" ) -func router(ctx context.Context, config *Config) http.Handler { +func router(ctx context.Context, config *Config, cfg *cmds.Server) http.Handler { serverConfig := &config.ControlConfig nodeAuth := passwordBootstrap(ctx, config) @@ -45,7 +46,7 @@ func router(ctx context.Context, config *Config) http.Handler { authed.Path(prefix + "/client-" + version.Program + "-controller.crt").Handler(fileHandler(serverConfig.Runtime.ClientK3sControllerCert, serverConfig.Runtime.ClientK3sControllerKey)) authed.Path(prefix + "/client-ca.crt").Handler(fileHandler(serverConfig.Runtime.ClientCA)) authed.Path(prefix + "/server-ca.crt").Handler(fileHandler(serverConfig.Runtime.ServerCA)) - authed.Path(prefix + "/config").Handler(configHandler(serverConfig)) + authed.Path(prefix + "/config").Handler(configHandler(serverConfig, cfg)) authed.Path(prefix + "/readyz").Handler(readyzHandler(serverConfig)) nodeAuthed := mux.NewRouter() @@ -256,12 +257,17 @@ func fileHandler(fileName ...string) http.Handler { }) } -func configHandler(server *config.Control) http.Handler { +func configHandler(server *config.Control, cfg *cmds.Server) http.Handler { return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { if req.TLS == nil { resp.WriteHeader(http.StatusNotFound) return } + // Startup hooks may read and modify cmds.Server in a goroutine, but as these are copied into + // config.Control before the startup hooks are called, any modifications need to be sync'd back + // into the struct before it is sent to agents. + // At this time we don't sync all the fields, just those known to be touched by startup hooks. + server.DisableKubeProxy = cfg.DisableKubeProxy resp.Header().Set("content-type", "application/json") json.NewEncoder(resp).Encode(server) }) diff --git a/pkg/server/server.go b/pkg/server/server.go index 2732e9b84d..7cdee43586 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -49,7 +49,7 @@ func ResolveDataDir(dataDir string) (string, error) { return filepath.Join(dataDir, "server"), err } -func StartServer(ctx context.Context, config *Config) error { +func StartServer(ctx context.Context, config *Config, cfg *cmds.Server) error { if err := setupDataDirAndChdir(&config.ControlConfig); err != nil { return err } @@ -65,7 +65,7 @@ func StartServer(ctx context.Context, config *Config) error { wg := &sync.WaitGroup{} wg.Add(len(config.StartupHooks)) - config.ControlConfig.Runtime.Handler = router(ctx, config) + config.ControlConfig.Runtime.Handler = router(ctx, config, cfg) shArgs := cmds.StartupHookArgs{ APIServerReady: config.ControlConfig.Runtime.APIServerReady, KubeConfigAdmin: config.ControlConfig.Runtime.KubeConfigAdmin,