Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide several sub-commands only on Linux #5381

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2021 k0s authors

Expand Down
5 changes: 5 additions & 0 deletions cmd/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controller_test

import (
"runtime"
"strconv"
"strings"
"testing"
Expand All @@ -28,6 +29,10 @@ import (
)

func TestControllerCmd_Help(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("Running controllers is only supported on Linux")
}

defaultConfigPath := strconv.Quote(constant.K0sConfigPathDefault)
defaultDataDir := strconv.Quote(constant.DataDirDefault)

Expand Down
31 changes: 23 additions & 8 deletions cmd/install/controller.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2021 k0s authors

Expand All @@ -19,8 +21,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 +43,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 +67,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
2 changes: 2 additions & 0 deletions cmd/install/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2024 k0s authors

Expand Down
37 changes: 2 additions & 35 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 @@ -44,37 +37,11 @@ func NewInstallCmd() *cobra.Command {
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
}

cmd.AddCommand(installControllerCmd(&installFlags))
cmd.AddCommand(installWorkerCmd(&installFlags))
addPlatformSpecificCommands(cmd, &installFlags)

cmd.PersistentFlags().BoolVar(&installFlags.force, "force", false, "force init script creation")
cmd.PersistentFlags().StringArrayVarP(&installFlags.envVars, "env", "e", nil, "set environment variable")
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
}
18 changes: 4 additions & 14 deletions cmd/restore/restore_windows.go → cmd/install/install_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 k0s authors
Copyright 2025 k0s authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,22 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package restore
package install

import (
"errors"

"github.com/spf13/cobra"
)

var restoredConfigPath string

func NewRestoreCmd() *cobra.Command {
return &cobra.Command{
Use: "restore",
Short: "restore k0s state from given backup archive. Not supported in Windows OS",
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("unsupported Operating System for this command")
},
}
func addPlatformSpecificCommands(install *cobra.Command, installFlags *installFlags) {
install.AddCommand(installControllerCmd(installFlags))
}
25 changes: 25 additions & 0 deletions cmd/install/install_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build !linux

/*
Copyright 2025 k0s authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package install

import "github.com/spf13/cobra"

func addPlatformSpecificCommands(*cobra.Command, *installFlags) {
// no-op
}
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
18 changes: 11 additions & 7 deletions cmd/install/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ limitations under the License.
package install

import (
"errors"
"fmt"
"os"
"runtime"

"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 +38,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 runtime.GOOS != "windows" && 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
2 changes: 2 additions & 0 deletions cmd/reset/reset.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2021 k0s authors

Expand Down
19 changes: 2 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ import (
"errors"
"net/http"
"os"
"runtime"

"github.com/k0sproject/k0s/cmd/airgap"
"github.com/k0sproject/k0s/cmd/api"
"github.com/k0sproject/k0s/cmd/backup"
configcmd "github.com/k0sproject/k0s/cmd/config"
"github.com/k0sproject/k0s/cmd/controller"
"github.com/k0sproject/k0s/cmd/ctr"
"github.com/k0sproject/k0s/cmd/etcd"
"github.com/k0sproject/k0s/cmd/install"
"github.com/k0sproject/k0s/cmd/kubeconfig"
"github.com/k0sproject/k0s/cmd/kubectl"
"github.com/k0sproject/k0s/cmd/reset"
"github.com/k0sproject/k0s/cmd/restore"
"github.com/k0sproject/k0s/cmd/start"
"github.com/k0sproject/k0s/cmd/status"
"github.com/k0sproject/k0s/cmd/stop"
"github.com/k0sproject/k0s/cmd/sysinfo"
"github.com/k0sproject/k0s/cmd/token"
Expand Down Expand Up @@ -82,24 +76,13 @@ func NewRootCmd() *cobra.Command {

cmd.AddCommand(airgap.NewAirgapCmd())
cmd.AddCommand(api.NewAPICmd())
cmd.AddCommand(backup.NewBackupCmd())
cmd.AddCommand(controller.NewControllerCmd())
cmd.AddCommand(ctr.NewCtrCommand())
cmd.AddCommand(configcmd.NewConfigCmd())
cmd.AddCommand(etcd.NewEtcdCmd())
cmd.AddCommand(install.NewInstallCmd())
cmd.AddCommand(kubeconfig.NewKubeConfigCmd())
cmd.AddCommand(kubectl.NewK0sKubectlCmd())
if runtime.GOOS == "linux" {
// Currently only supported on Linux
cmd.AddCommand(reset.NewResetCmd())
}
cmd.AddCommand(restore.NewRestoreCmd())
cmd.AddCommand(start.NewStartCmd())
if runtime.GOOS == "linux" {
// Currently only supported on Linux
cmd.AddCommand(status.NewStatusCmd())
}
cmd.AddCommand(stop.NewStopCmd())
cmd.AddCommand(sysinfo.NewSysinfoCmd())
cmd.AddCommand(token.NewTokenCmd())
Expand All @@ -109,6 +92,8 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(newCompletionCmd())
cmd.AddCommand(newDocsCmd())

addPlatformSpecificCommands(cmd)

cmd.DisableAutoGenTag = true
longDesc = "k0s - The zero friction Kubernetes - https://k0sproject.io"
if build.EulaNotice != "" {
Expand Down
35 changes: 35 additions & 0 deletions cmd/root_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2025 k0s authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"github.com/k0sproject/k0s/cmd/backup"
"github.com/k0sproject/k0s/cmd/controller"
"github.com/k0sproject/k0s/cmd/reset"
"github.com/k0sproject/k0s/cmd/restore"
"github.com/k0sproject/k0s/cmd/status"

"github.com/spf13/cobra"
)

func addPlatformSpecificCommands(root *cobra.Command) {
root.AddCommand(backup.NewBackupCmd())
root.AddCommand(controller.NewControllerCmd())
root.AddCommand(reset.NewResetCmd())
root.AddCommand(restore.NewRestoreCmd())
root.AddCommand(status.NewStatusCmd())
}
10 changes: 5 additions & 5 deletions pkg/cleanup/bridge_other.go → cmd/root_other.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !linux

/*
Copyright 2021 k0s authors
Copyright 2025 k0s authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package cleanup
package cmd

func newBridgeStep() Step {
return nil
}
import "github.com/spf13/cobra"

func addPlatformSpecificCommands(root *cobra.Command) { /* no-op */ }
Loading
Loading