Skip to content

Commit

Permalink
Inline setup function for install commands
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
twz123 committed Jan 7, 2025
1 parent 8558f0b commit 9cb9616
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 58 deletions.
29 changes: 21 additions & 8 deletions cmd/install/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand All @@ -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
},
}
Expand Down
34 changes: 0 additions & 34 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
1 change: 0 additions & 1 deletion cmd/install/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/spf13/cobra"

"github.com/spf13/pflag"
)

Expand Down
17 changes: 10 additions & 7 deletions cmd/install/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
13 changes: 5 additions & 8 deletions pkg/install/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package install

import (
"errors"
"fmt"

"github.com/kardianos/service"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -114,19 +113,17 @@ 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()
if err != nil && !errors.Is(err, service.ErrNotInstalled) {
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 {
Expand Down

0 comments on commit 9cb9616

Please sign in to comment.