From 9cb9616d4c048a0363ccc9e36d25150d3531e1ab Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Tue, 7 Jan 2025 10:17:41 +0100 Subject: [PATCH] Inline setup function for install commands Configuration loading, validation, and user setup are required only for controllers. Inline this functionality into their commands, making the worker command lighter. Signed-off-by: Tom Wieczorek --- cmd/install/controller.go | 29 +++++++++++++++++++++-------- cmd/install/install.go | 34 ---------------------------------- cmd/install/util.go | 1 - cmd/install/worker.go | 17 ++++++++++------- pkg/install/service.go | 13 +++++-------- 5 files changed, 36 insertions(+), 58 deletions(-) diff --git a/cmd/install/controller.go b/cmd/install/controller.go index 36e62c01b252..d27ab6f94fa6 100644 --- a/cmd/install/controller.go +++ b/cmd/install/controller.go @@ -19,8 +19,11 @@ package install import ( "errors" "fmt" + "os" + "testing/iotest" "github.com/k0sproject/k0s/pkg/config" + "github.com/k0sproject/k0s/pkg/install" "github.com/spf13/cobra" ) @@ -38,14 +41,17 @@ With the controller subcommand you can setup a single node cluster by running: `, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { - opts, err := config.GetCmdOpts(cmd) - if err != nil { - return err + if os.Geteuid() != 0 { + return errors.New("this command must be run as root") } - c := (*command)(opts) + cmd.SetIn(iotest.ErrReader(errors.New("cannot read configuration from standard input when installing k0s"))) + k0sVars, err := config.NewCfgVars(cmd) + if err != nil { + return fmt.Errorf("failed to initialize configuration variables: %w", err) + } - nodeConfig, err := c.K0sVars.NodeConfig() + nodeConfig, err := k0sVars.NodeConfig() if err != nil { return fmt.Errorf("failed to load node config: %w", err) } @@ -59,10 +65,17 @@ With the controller subcommand you can setup a single node cluster by running: return err } - flagsAndVals = append([]string{"controller"}, flagsAndVals...) - if err := c.setup("controller", flagsAndVals, installFlags); err != nil { - return err + systemUsers := nodeConfig.Spec.Install.SystemUsers + homeDir := k0sVars.DataDir + if err := install.EnsureControllerUsers(systemUsers, homeDir); err != nil { + return fmt.Errorf("failed to create controller users: %w", err) + } + + args := append([]string{"controller"}, flagsAndVals...) + if err := install.InstallService(args, installFlags.envVars, installFlags.force); err != nil { + return fmt.Errorf("failed to install controller service: %w", err) } + return nil }, } diff --git a/cmd/install/install.go b/cmd/install/install.go index 11bd266faa30..1b2d89c49613 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -17,18 +17,11 @@ limitations under the License. package install import ( - "errors" - "fmt" - "os" - "github.com/k0sproject/k0s/pkg/config" - "github.com/k0sproject/k0s/pkg/install" "github.com/spf13/cobra" ) -type command config.CLIOptions - type installFlags struct { force bool envVars []string @@ -51,30 +44,3 @@ func NewInstallCmd() *cobra.Command { cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet()) return cmd } - -// The setup functions: -// - Ensures that the proper users are created. -// - Sets up startup and logging for k0s. -func (c *command) setup(role string, args []string, installFlags *installFlags) error { - if os.Geteuid() != 0 { - return errors.New("this command must be run as root") - } - - nodeConfig, err := c.K0sVars.NodeConfig() - if err != nil { - return err - } - - if role == "controller" { - systemUsers := nodeConfig.Spec.Install.SystemUsers - homeDir := c.K0sVars.DataDir - if err := install.EnsureControllerUsers(systemUsers, homeDir); err != nil { - return fmt.Errorf("failed to create controller users: %w", err) - } - } - err = install.EnsureService(args, installFlags.envVars, installFlags.force) - if err != nil { - return fmt.Errorf("failed to install k0s service: %w", err) - } - return nil -} diff --git a/cmd/install/util.go b/cmd/install/util.go index 6ba8e661c0bd..f71f50c46221 100644 --- a/cmd/install/util.go +++ b/cmd/install/util.go @@ -23,7 +23,6 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/spf13/pflag" ) diff --git a/cmd/install/worker.go b/cmd/install/worker.go index 55c79cb7dc8a..30f693c02a6f 100644 --- a/cmd/install/worker.go +++ b/cmd/install/worker.go @@ -17,9 +17,14 @@ limitations under the License. package install import ( + "errors" + "fmt" + "os" + "github.com/spf13/cobra" "github.com/k0sproject/k0s/pkg/config" + "github.com/k0sproject/k0s/pkg/install" ) func installWorkerCmd(installFlags *installFlags) *cobra.Command { @@ -32,20 +37,18 @@ All default values of worker command will be passed to the service stub unless o Windows flags like "--api-server", "--cidr-range" and "--cluster-dns" will be ignored since install command doesn't yet support Windows services`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { - opts, err := config.GetCmdOpts(cmd) - if err != nil { - return err + if os.Geteuid() != 0 { + return errors.New("this command must be run as root") } - c := (*command)(opts) flagsAndVals, err := cmdFlagsToArgs(cmd) if err != nil { return err } - flagsAndVals = append([]string{"worker"}, flagsAndVals...) - if err := c.setup("worker", flagsAndVals, installFlags); err != nil { - return err + args := append([]string{"worker"}, flagsAndVals...) + if err := install.InstallService(args, installFlags.envVars, installFlags.force); err != nil { + return fmt.Errorf("failed to install worker service: %w", err) } return nil diff --git a/pkg/install/service.go b/pkg/install/service.go index c7cccd5d4b1c..dd3c75238dcc 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -18,7 +18,6 @@ package install import ( "errors" - "fmt" "github.com/kardianos/service" "github.com/sirupsen/logrus" @@ -65,8 +64,8 @@ func InstalledService() (service.Service, error) { return s, errors.New("k0s has not been installed as a service") } -// EnsureService installs the k0s service, per the given arguments, and the detected platform -func EnsureService(args []string, envVars []string, force bool) error { +// InstallService installs the k0s service, per the given arguments, and the detected platform +func InstallService(args []string, envVars []string, force bool) error { var deps []string var svcConfig *service.Config @@ -114,6 +113,7 @@ func EnsureService(args []string, envVars []string, force bool) error { svcConfig.Dependencies = deps svcConfig.Arguments = args + if force { logrus.Infof("Uninstalling %s service", svcConfig.Name) err = s.Uninstall() @@ -121,12 +121,9 @@ func EnsureService(args []string, envVars []string, force bool) error { logrus.Warnf("failed to uninstall service: %v", err) } } + logrus.Infof("Installing %s service", svcConfig.Name) - err = s.Install() - if err != nil { - return fmt.Errorf("failed to install service: %w", err) - } - return nil + return s.Install() } func UninstallService(role string) error {