Skip to content

Commit

Permalink
Merge pull request #2514 from AkihiroSuda/fix-2455
Browse files Browse the repository at this point in the history
nerdctl version: skip inspecting rootless daemon if the daemon is not running
  • Loading branch information
AkihiroSuda authored Sep 19, 2023
2 parents 29cf699 + 357113b commit 5f39d76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/nerdctl/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 20 additions & 6 deletions cmd/nerdctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5f39d76

Please sign in to comment.