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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/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 linux

/*
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
5 changes: 3 additions & 2 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 All @@ -20,7 +22,6 @@ import (
"encoding/json"
"fmt"
"io"
"path/filepath"

"github.com/k0sproject/k0s/pkg/component/status"
"github.com/k0sproject/k0s/pkg/config"
Expand Down Expand Up @@ -56,7 +57,7 @@ func NewStatusCmd() *cobra.Command {
}

cmd.PersistentFlags().StringVarP(&output, "out", "o", "", "sets type of output to json or yaml")
cmd.PersistentFlags().StringVar(&config.StatusSocket, "status-socket", filepath.Join(config.K0sVars.RunDir, "status.sock"), "Full file path to the socket file.")
cmd.PersistentFlags().String("status-socket", "", "Full file path to the socket file. (default: <rundir>/status.sock)")
cmd.AddCommand(NewStatusSubCmdComponents())
return cmd
}
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//go:build !unix

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

package backup
package users

import (
"errors"

"github.com/spf13/cobra"
"fmt"
"runtime"
)

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 LookupUID(string) (int, error) {
return 0, fmt.Errorf("%w on %s", errors.ErrUnsupported, runtime.GOOS)
}
67 changes: 67 additions & 0 deletions internal/pkg/users/lookup_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//go:build unix

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

import (
"bytes"
"errors"
"fmt"
"os/exec"
"os/user"
"strconv"
)

// Lookup looks up a user's UID by username. If the user cannot be found, the
// returned error is [ErrNotExist]. If an error is returned, the returned UID
// will be [UnknownUID].
func LookupUID(name string) (int, error) {
var uid string

if entry, err := user.Lookup(name); err != nil {
if !errors.Is(err, user.UnknownUserError(name)) {
return UnknownUID, err
}

err = ErrNotExist

// fallback to call external `id` in case NSS is used
out, idErr := exec.Command("id", "-u", name).Output()
if idErr != nil {
var exitErr *exec.ExitError
if errors.As(idErr, &exitErr) {
return UnknownUID, fmt.Errorf("%w (%w: %s)", err, idErr, bytes.TrimSpace(exitErr.Stderr))
}
return UnknownUID, fmt.Errorf("%w (%w)", err, idErr)
}

uid = string(bytes.TrimSpace(out))
} else {
uid = entry.Uid
}

parsedUID, err := strconv.Atoi(uid)
if err != nil {
return UnknownUID, fmt.Errorf("UID %q is not a decimal integer: %w", uid, err)
}
if parsedUID < 0 {
return UnknownUID, fmt.Errorf("UID is negative: %d", parsedUID)
}

return parsedUID, nil
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright 2022 k0s authors

Expand All @@ -18,17 +20,12 @@ package users

import (
"os/exec"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetUID(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("No numeric user IDs on Windows")
}

func TestLookupUID(t *testing.T) {
uid, err := LookupUID("root")
if assert.NoError(t, err, "Failed to get UID for root user") {
assert.Equal(t, 0, uid, "root's UID is not 0?")
Expand Down
Loading
Loading