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 Jan 7, 2025
1 parent 9cb9616 commit 3e409de
Show file tree
Hide file tree
Showing 27 changed files with 155 additions and 85 deletions.
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
19 changes: 4 additions & 15 deletions cmd/backup/backup_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,23 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package backup
package install

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(install *cobra.Command, installFlags *installFlags) {
install.AddCommand(installControllerCmd(installFlags))
}
24 changes: 7 additions & 17 deletions cmd/restore/restore_windows.go → cmd/install/install_other.go
Original file line number Diff line number Diff line change
@@ -1,5 +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 @@ -14,22 +16,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package restore

import (
"errors"

"github.com/spf13/cobra"
)
package install

var restoredConfigPath string
import "github.com/spf13/cobra"

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(*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 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/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 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
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 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 1 addition & 26 deletions pkg/install/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func InstalledService() (service.Service, 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

prg := &Program{}
Expand All @@ -82,36 +81,12 @@ func InstallService(args []string, envVars []string, force bool) error {
return err
}

// fetch service type
svcType := s.Platform()
switch svcType {
case "linux-openrc":
deps = []string{"need cgroups", "need net", "use dns", "after firewall"}
svcConfig.Option = map[string]interface{}{
"OpenRCScript": openRCScript,
}
case "linux-upstart":
svcConfig.Option = map[string]interface{}{
"UpstartScript": upstartScript,
}
case "unix-systemv":
svcConfig.Option = map[string]interface{}{
"SysVScript": sysvScript,
}
case "linux-systemd":
deps = []string{"After=network-online.target", "Wants=network-online.target"}
svcConfig.Option = map[string]interface{}{
"SystemdScript": systemdScript,
"LimitNOFILE": 999999,
}
default:
}
configureServicePlatform(s, svcConfig)

if len(envVars) > 0 {
svcConfig.Option["Environment"] = envVars
}

svcConfig.Dependencies = deps
svcConfig.Arguments = args

if force {
Expand Down
Loading

0 comments on commit 3e409de

Please sign in to comment.