Skip to content

Commit 5f39d76

Browse files
authored
Merge pull request #2514 from AkihiroSuda/fix-2455
nerdctl version: skip inspecting rootless daemon if the daemon is not running
2 parents 29cf699 + 357113b commit 5f39d76

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cmd/nerdctl/main_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool {
3737
return true
3838
}
3939
switch commands[1] {
40-
// completion, login, logout: false, because it shouldn't require the daemon to be running
40+
// completion, login, logout, version: false, because it shouldn't require the daemon to be running
4141
// apparmor: false, because it requires the initial mount namespace to access /sys/kernel/security
4242
// cp, compose cp: false, because it requires the initial mount namespace to inspect file owners
43-
case "", "completion", "login", "logout", "apparmor", "cp":
43+
case "", "completion", "login", "logout", "apparmor", "cp", "version":
4444
return false
4545
case "container":
4646
if len(commands) < 3 {

cmd/nerdctl/version.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
"os"
2424
"text/template"
2525

26-
"github.com/containerd/nerdctl/pkg/api/types"
2726
"github.com/containerd/nerdctl/pkg/clientutil"
2827
"github.com/containerd/nerdctl/pkg/formatter"
2928
"github.com/containerd/nerdctl/pkg/infoutil"
3029
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
30+
"github.com/containerd/nerdctl/pkg/rootlessutil"
31+
"github.com/sirupsen/logrus"
3132
"github.com/spf13/cobra"
3233
)
3334

@@ -66,7 +67,17 @@ func versionAction(cmd *cobra.Command, args []string) error {
6667
}
6768
}
6869

69-
v, vErr := versionInfo(cmd, globalOptions)
70+
address := globalOptions.Address
71+
// rootless `nerdctl version` runs in the host namespaces, so the address is different
72+
if rootlessutil.IsRootless() {
73+
address, err = rootlessutil.RootlessContainredSockAddress()
74+
if err != nil {
75+
logrus.WithError(err).Warning("failed to inspect the rootless containerd socket address")
76+
address = ""
77+
}
78+
}
79+
80+
v, vErr := versionInfo(cmd, globalOptions.Namespace, address)
7081
if tmpl != nil {
7182
var b bytes.Buffer
7283
if err := tmpl.Execute(&b, v); err != nil {
@@ -102,13 +113,16 @@ func versionAction(cmd *cobra.Command, args []string) error {
102113
return vErr
103114
}
104115

105-
// versionInfo may return partial VersionInfo on error
106-
func versionInfo(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (dockercompat.VersionInfo, error) {
107-
116+
// versionInfo may return partial VersionInfo on error.
117+
// Address can be empty to skip inspecting the server.
118+
func versionInfo(cmd *cobra.Command, ns, address string) (dockercompat.VersionInfo, error) {
108119
v := dockercompat.VersionInfo{
109120
Client: infoutil.ClientVersion(),
110121
}
111-
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
122+
if address == "" {
123+
return v, nil
124+
}
125+
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), ns, address)
112126
if err != nil {
113127
return v, err
114128
}

0 commit comments

Comments
 (0)