Skip to content

Commit

Permalink
Provide several sub-commands only on Linux
Browse files Browse the repository at this point in the history
They won't work on non-Linux systems anyways.

Use build flags to add platform-specific sub-commands instead of runtime
checks. Also add build tag to files that only make sense on specific
platforms. This includes Autopilot, as this relies on the status socket,
which is not available on Windows at the moment.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Jan 7, 2025
1 parent 414a9ce commit cd7b466
Show file tree
Hide file tree
Showing 52 changed files with 388 additions and 162 deletions.
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
2 changes: 2 additions & 0 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 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
3 changes: 2 additions & 1 deletion cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ 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())
Expand Down
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
}
3 changes: 2 additions & 1 deletion cmd/install/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"os"
"runtime"

"github.com/spf13/cobra"

Expand All @@ -37,7 +38,7 @@ 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 {
if os.Geteuid() != 0 {
if runtime.GOOS != "windows" && os.Geteuid() != 0 {
return errors.New("this command must be run as root")
}

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 */ }
9 changes: 7 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"io"
"runtime"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -75,12 +76,16 @@ func TestRootCmd_Flags(t *testing.T) {

func TestUnknownSubCommandsAreRejected(t *testing.T) {
commandsWithArguments := []string{
"controller",
"kubeconfig create",
"restore",
"token invalidate",
"worker",
}
if runtime.GOOS == "linux" {
commandsWithArguments = append(commandsWithArguments,
"controller",
"restore",
)
}
t.Cleanup(func() {
if !t.Failed() {
assert.Empty(t, commandsWithArguments, "Some sub-commands are listed unnecessarily")
Expand Down
3 changes: 2 additions & 1 deletion cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package start
import (
"errors"
"os"
"runtime"

"github.com/k0sproject/k0s/pkg/install"

Expand All @@ -32,7 +33,7 @@ func NewStartCmd() *cobra.Command {
Short: "Start the k0s service configured on this host. Must be run as root (or with sudo)",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if os.Geteuid() != 0 {
if runtime.GOOS != "windows" && os.Geteuid() != 0 {
return errors.New("this command must be run as root")
}
svc, err := install.InstalledService()
Expand Down
2 changes: 2 additions & 0 deletions cmd/status/status.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2021 k0s authors
Expand Down
3 changes: 2 additions & 1 deletion cmd/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package stop
import (
"errors"
"os"
"runtime"

"github.com/k0sproject/k0s/pkg/install"

Expand All @@ -32,7 +33,7 @@ func NewStopCmd() *cobra.Command {
Short: "Stop the k0s service configured on this host. Must be run as root (or with sudo)",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if os.Geteuid() != 0 {
if runtime.GOOS != "windows" && os.Geteuid() != 0 {
return errors.New("this command must be run as root")
}
svc, err := install.InstalledService()
Expand Down
2 changes: 2 additions & 0 deletions cmd/token/create.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2021 k0s authors
Expand Down
3 changes: 2 additions & 1 deletion cmd/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ func NewTokenCmd() *cobra.Command {
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
}

cmd.AddCommand(tokenCreateCmd())
cmd.AddCommand(tokenListCmd())
cmd.AddCommand(tokenInvalidateCmd())
cmd.AddCommand(preSharedCmd())
addPlatformSpecificCommands(cmd)

return cmd
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/token/token_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build !unix

/*
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 token

import (
"github.com/spf13/cobra"
)

func addPlatformSpecificCommands(*cobra.Command) { /* no-op */ }
21 changes: 6 additions & 15 deletions cmd/backup/backup_windows.go → cmd/token/token_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//go:build unix

/*
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,23 +16,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package backup
package token

import (
"errors"

"github.com/spf13/cobra"
)

var savePath string

func NewBackupCmd() *cobra.Command {
return &cobra.Command{
Use: "backup",
Short: "Back-Up k0s configuration. Not supported on Windows OS",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("unsupported Operating System for this command")
},
}
func addPlatformSpecificCommands(token *cobra.Command) {
token.AddCommand(tokenCreateCmd())
}
Loading

0 comments on commit cd7b466

Please sign in to comment.