Skip to content

Commit

Permalink
fix: set mountpoint RW and Propagation in internal mount label
Browse files Browse the repository at this point in the history
Signed-off-by: baijia <baijia.wr@antgroup.com>
  • Loading branch information
baijia committed Apr 26, 2024
1 parent f25ce7e commit 54b2956
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
20 changes: 20 additions & 0 deletions cmd/nerdctl/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/docker/go-connections/nat"
"gotest.tools/v3/assert"
Expand Down Expand Up @@ -156,6 +157,25 @@ func TestContainerInspectContainsLabel(t *testing.T) {
assert.Equal(base.T, "bar", lbs["bar"])
}

func TestContainerInspectContainsInternalLabel(t *testing.T) {
testutil.DockerIncompatible(t)
t.Parallel()
testContainer := testutil.Identifier(t)

base := testutil.NewBase(t)
defer base.Cmd("rm", "-f", testContainer).Run()

base.Cmd("run", "-d", "--name", testContainer, "--mount", "type=bind,src=/tmp,dst=/app,readonly=false,bind-propagation=rprivate", testutil.NginxAlpineImage).AssertOK()
base.EnsureContainerStarted(testContainer)
inspect := base.InspectContainer(testContainer)
lbs := inspect.Config.Labels

// TODO: add more internal labels testcases
labelMount := lbs[labels.Mounts]
expectedLabelMount := "[{\"Type\":\"bind\",\"Source\":\"/tmp\",\"Destination\":\"/app\",\"Mode\":\"rprivate,rbind\",\"RW\":true,\"Propagation\":\"rprivate\"}]"
assert.Equal(base.T, expectedLabelMount, labelMount)
}

func TestContainerInspectState(t *testing.T) {
t.Parallel()
testContainer := testutil.Identifier(t)
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ func dockercompatMounts(mountPoints []*mountutil.Processed) []dockercompat.Mount
Driver: "",
Mode: mp.Mode,
}
result[i].RW, result[i].Propagation = dockercompat.ParseMountProperties(strings.Split(mp.Mode, ","))

// it's an anonymous volume
if mp.AnonymousVolume != "" {
Expand Down
11 changes: 2 additions & 9 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -485,18 +484,12 @@ func parseMounts(nerdctlMounts string) ([]MountPoint, error) {
return nil, err
}

for i := range mounts {
rw, propagation := parseMountProperties(mounts[i].Mode)
mounts[i].RW = rw
mounts[i].Propagation = propagation
}

return mounts, nil
}

func parseMountProperties(option string) (rw bool, propagation string) {
func ParseMountProperties(option []string) (rw bool, propagation string) {
rw = true
for _, opt := range strings.Split(option, ",") {
for _, opt := range option {
switch opt {
case "ro", "rro":
rw = false
Expand Down

0 comments on commit 54b2956

Please sign in to comment.