From 357113b4dea385befe7be40d0336a8375bc6ae91 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Mon, 18 Sep 2023 19:53:45 +0900 Subject: [PATCH] nerdctl version: skip inspecting rootless daemon if the daemon is not running Fix issue 2455 Signed-off-by: Akihiro Suda --- cmd/nerdctl/main_linux.go | 4 ++-- cmd/nerdctl/version.go | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/nerdctl/main_linux.go b/cmd/nerdctl/main_linux.go index 1fa33e6ca73..d4c81209421 100644 --- a/cmd/nerdctl/main_linux.go +++ b/cmd/nerdctl/main_linux.go @@ -37,10 +37,10 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { return true } switch commands[1] { - // completion, login, logout: false, because it shouldn't require the daemon to be running + // completion, login, logout, version: false, because it shouldn't require the daemon to be running // apparmor: false, because it requires the initial mount namespace to access /sys/kernel/security // cp, compose cp: false, because it requires the initial mount namespace to inspect file owners - case "", "completion", "login", "logout", "apparmor", "cp": + case "", "completion", "login", "logout", "apparmor", "cp", "version": return false case "container": if len(commands) < 3 { diff --git a/cmd/nerdctl/version.go b/cmd/nerdctl/version.go index f74a9d44846..3ce1cfe28ba 100644 --- a/cmd/nerdctl/version.go +++ b/cmd/nerdctl/version.go @@ -23,11 +23,12 @@ import ( "os" "text/template" - "github.com/containerd/nerdctl/pkg/api/types" "github.com/containerd/nerdctl/pkg/clientutil" "github.com/containerd/nerdctl/pkg/formatter" "github.com/containerd/nerdctl/pkg/infoutil" "github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat" + "github.com/containerd/nerdctl/pkg/rootlessutil" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -66,7 +67,17 @@ func versionAction(cmd *cobra.Command, args []string) error { } } - v, vErr := versionInfo(cmd, globalOptions) + address := globalOptions.Address + // rootless `nerdctl version` runs in the host namespaces, so the address is different + if rootlessutil.IsRootless() { + address, err = rootlessutil.RootlessContainredSockAddress() + if err != nil { + logrus.WithError(err).Warning("failed to inspect the rootless containerd socket address") + address = "" + } + } + + v, vErr := versionInfo(cmd, globalOptions.Namespace, address) if tmpl != nil { var b bytes.Buffer if err := tmpl.Execute(&b, v); err != nil { @@ -102,13 +113,16 @@ func versionAction(cmd *cobra.Command, args []string) error { return vErr } -// versionInfo may return partial VersionInfo on error -func versionInfo(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (dockercompat.VersionInfo, error) { - +// versionInfo may return partial VersionInfo on error. +// Address can be empty to skip inspecting the server. +func versionInfo(cmd *cobra.Command, ns, address string) (dockercompat.VersionInfo, error) { v := dockercompat.VersionInfo{ Client: infoutil.ClientVersion(), } - client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) + if address == "" { + return v, nil + } + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), ns, address) if err != nil { return v, err }