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

K3s extracts to wrong dir when embedded subcommands (kubectl, ctr, crictl) are run with --help or --version flags #11235

Closed
brandond opened this issue Nov 5, 2024 Discussed in #11209 · 3 comments
Assignees
Labels
kind/bug Something isn't working

Comments

@brandond
Copy link
Member

brandond commented Nov 5, 2024

Discussed in #11209

Originally posted by dreia2k November 4, 2024
Hello,

when calling kubectl as non root user with an OverrideFlag, as defined in func MustFindString, dataDir gets changed to the current working directory.

This is called from the cmd/k3s/main.go main function which calls func findDataDir

// in cmd/k3s/main.go main()
dataDir := findDataDir(os.Args)

// in cmd/k3s/main.go findDataDir()
dataDir = configfilearg.MustFindString(args, "data-dir") // this finds the OverrideFlag and returns it

So every time --version or --help is passed as an option, a directory with the name of the flag gets created in e.g.: ./--version.
Is this an intended behaviour?
It seems weird to me and causes these folders littering my current directory.

@brandond
Copy link
Member Author

brandond commented Nov 5, 2024

Example:

opensuse@systemd-node-1:~> K3S_DEBUG=true k3s ctr --help
INFO[0000] Acquiring lock file /home/opensuse/--help/data/.lock
INFO[0000] Preparing data dir /home/opensuse/--help/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
...
DEBU[0002] Asset dir /home/opensuse/--help/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
DEBU[0002] Running /home/opensuse/--help/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d/bin/ctr [ctr --help]
NAME:
   ctr -
        __
  _____/ /______
 / ___/ __/ ___/
/ /__/ /_/ /
\___/\__/_/

containerd CLI


USAGE:
   ctr [global options] command [command options] [arguments...]

VERSION:
   v1.7.22-k3s1

DESCRIPTION:

ctr is an unsupported debug and administrative client for interacting
with the containerd daemon. Because it is unsupported, the commands,
options, and operations are not guaranteed to be backward compatible or
stable from release to release of the containerd project.

COMMANDS:
   plugins, plugin            Provides information about containerd plugins
   version                    Print the client and server versions
   containers, c, container   Manage containers
   content                    Manage content
   events, event              Display containerd events
   images, image, i           Manage images
   leases                     Manage leases
   namespaces, namespace, ns  Manage namespaces
   pprof                      Provide golang pprof outputs for containerd
   run                        Run a container
   snapshots, snapshot        Manage snapshots
   tasks, t, task             Manage tasks
   install                    Install a new package
   oci                        OCI tools
   sandboxes, sandbox, sb, s  Manage sandboxes
   info                       Print the server info
   deprecations
   shim                       Interact with a shim directly
   help, h                    Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --debug                      Enable debug output in logs
   --address value, -a value    Address for containerd's GRPC server (default: "/run/k3s/containerd/containerd.sock") [$CONTAINERD_ADDRESS]
   --timeout value              Total timeout for ctr commands (default: 0s)
   --connect-timeout value      Timeout for connecting to containerd (default: 0s)
   --namespace value, -n value  Namespace to use with commands (default: "k8s.io") [$CONTAINERD_NAMESPACE]
   --help, -h                   show help
   --version, -v                print the version

opensuse@systemd-node-1:~> K3S_DEBUG=true k3s kubectl --help
DEBU[0000] Asset dir /home/opensuse/--help/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
DEBU[0000] Running /home/opensuse/--help/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d/bin/kubectl [kubectl --help]
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

...

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

opensuse@systemd-node-1:~> K3S_DEBUG=true k3s ctr --version
INFO[0000] Acquiring lock file /home/opensuse/--version/data/.lock
INFO[0000] Preparing data dir /home/opensuse/--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
...
DEBU[0002] Asset dir /home/opensuse/--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
DEBU[0002] Running /home/opensuse/--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d/bin/ctr [ctr --version]
ctr github.com/k3s-io/containerd v1.7.22-k3s1

opensuse@systemd-node-1:~> K3S_DEBUG=true k3s kubectl --version
DEBU[0000] Asset dir /home/opensuse/--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
DEBU[0000] Running /home/opensuse/--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d/bin/kubectl [kubectl --version]
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

...

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

@brandond
Copy link
Member Author

brandond commented Nov 5, 2024

The same thing will actually happen for root as well:

root@systemd-node-1:/# K3S_DEBUG=true k3s ctr --version
INFO[0000] Acquiring lock file /--version/data/.lock
INFO[0000] Preparing data dir /--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
...
DEBU[0002] Asset dir /--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
DEBU[0002] Running /--version/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d/bin/ctr [ctr --version]
ctr github.com/k3s-io/containerd v1.7.22-k3s1

This only happens if the default data-dir, or the data-dir specified by the config file, doesn't exist. After starting k3s, it uses the correct existing data dir:

root@systemd-node-1:/# K3S_DEBUG=true k3s ctr --version
DEBU[0000] Asset dir /var/lib/rancher/k3s/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d
DEBU[0000] Running /var/lib/rancher/k3s/data/5aa71fa53ccd126a59cca0667c10675f13cae44186cd95944e782ee84022b62d/bin/ctr [ctr --version]
ctr github.com/k3s-io/containerd v1.7.22-k3s1

So this is somewhere in the user data-dir code.

@aganesh-suse
Copy link

Closing based on results from:
#11252

@github-project-automation github-project-automation bot moved this from To Test to Done Issue in K3s Development Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
Status: Done Issue
Development

No branches or pull requests

2 participants