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.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Dec 24, 2024
1 parent 9d89909 commit c84a9bf
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 98 deletions.
36 changes: 0 additions & 36 deletions cmd/backup/backup_windows.go

This file was deleted.

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
2 changes: 2 additions & 0 deletions cmd/install/install.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/worker.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/reset/reset.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2021 k0s authors
Expand Down
35 changes: 0 additions & 35 deletions cmd/restore/restore_windows.go

This file was deleted.

23 changes: 2 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,15 @@ 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"
"github.com/k0sproject/k0s/cmd/validate"
Expand Down Expand Up @@ -83,25 +75,12 @@ 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())
cmd.AddCommand(validate.NewValidateCmd()) // hidden+deprecated
Expand All @@ -112,6 +91,8 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(newDefaultConfigCmd()) // hidden+deprecated
cmd.AddCommand(newDocsCmd())

addPlatformSpecificCommands(cmd)

cmd.DisableAutoGenTag = true
longDesc = "k0s - The zero friction Kubernetes - https://k0sproject.io"
if build.EulaNotice != "" {
Expand Down
39 changes: 39 additions & 0 deletions cmd/root_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2024 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/install"
"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/spf13/cobra"
)

func addPlatformSpecificCommands(root *cobra.Command) {
root.AddCommand(backup.NewBackupCmd())
root.AddCommand(install.NewInstallCmd())
root.AddCommand(reset.NewResetCmd())
root.AddCommand(restore.NewRestoreCmd())
root.AddCommand(start.NewStartCmd())
root.AddCommand(status.NewStatusCmd())
root.AddCommand(stop.NewStopCmd())
}
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 2024 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 */ }
5 changes: 4 additions & 1 deletion 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 @@ -77,10 +78,12 @@ func TestUnknownSubCommandsAreRejected(t *testing.T) {
commandsWithArguments := []string{
"controller",
"kubeconfig create",
"restore",
"token invalidate",
"worker",
}
if runtime.GOOS == "linux" {
commandsWithArguments = append(commandsWithArguments, "restore")
}
t.Cleanup(func() {
if !t.Failed() {
assert.Empty(t, commandsWithArguments, "Some sub-commands are listed unnecessarily")
Expand Down
2 changes: 2 additions & 0 deletions cmd/start/start.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/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
2 changes: 2 additions & 0 deletions cmd/stop/stop.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 internal/pkg/users/lookup_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2022 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/cleanup/cleanup.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 pkg/cleanup/services.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 pkg/cleanup/users.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2021 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/install/linux_openrc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2022 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/install/linux_systemd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2024 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/install/linux_sysv.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2022 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/install/linux_upstart.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2022 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/install/service.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

/*
Copyright 2020 k0s authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/install/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func DeleteControllerUsers(systemUsers *v1beta1.SystemUser) error {
if err := deleteUser(userName); err != nil {
errs = append(errs, err)
}
} else if !errors.Is(err, users.ErrNotExist) {
errs = append(errs, err)
}
}

Expand Down

0 comments on commit c84a9bf

Please sign in to comment.