Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit fb722f5

Browse files
authored
Merge pull request #814 from Random-Liu/cherrypick-#811
Fix empty volume ownership.
2 parents 4cf084d + 654cee2 commit fb722f5

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2018 The Containerd Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM busybox
16+
RUN mkdir -p /test_dir && \
17+
chown -R nobody:nogroup /test_dir
18+
VOLUME /test_dir
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2018 The Containerd Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
all: build
16+
17+
PROJ=gcr.io/k8s-cri-containerd
18+
VERSION=1.0
19+
IMAGE=$(PROJ)/volume-ownership:$(VERSION)
20+
21+
build:
22+
docker build -t $(IMAGE) .
23+
24+
push:
25+
gcloud docker -- push $(IMAGE)
26+
27+
.PHONY: build push

integration/volume_copy_up_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestVolumeCopyUp(t *testing.T) {
7070
assert.Equal(t, "test_content\n", string(stdout))
7171

7272
t.Logf("Check host path of the volume")
73-
hostCmd := fmt.Sprintf("ls %s/containers/%s/volumes/*/test_file | xargs cat", *criRoot, cn)
73+
hostCmd := fmt.Sprintf("find %s/containers/%s/volumes/*/test_file | xargs cat", *criRoot, cn)
7474
output, err := exec.Command("sh", "-c", hostCmd).CombinedOutput()
7575
require.NoError(t, err)
7676
assert.Equal(t, "test_content\n", string(output))
@@ -88,3 +88,51 @@ func TestVolumeCopyUp(t *testing.T) {
8888
require.NoError(t, err)
8989
assert.Equal(t, "new_content\n", string(output))
9090
}
91+
92+
func TestVolumeOwnership(t *testing.T) {
93+
const (
94+
testImage = "gcr.io/k8s-cri-containerd/volume-ownership:1.0"
95+
execTimeout = time.Minute
96+
)
97+
98+
t.Logf("Create a sandbox")
99+
sbConfig := PodSandboxConfig("sandbox", "volume-ownership")
100+
sb, err := runtimeService.RunPodSandbox(sbConfig)
101+
require.NoError(t, err)
102+
defer func() {
103+
assert.NoError(t, runtimeService.StopPodSandbox(sb))
104+
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
105+
}()
106+
107+
t.Logf("Pull test image")
108+
_, err = imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil)
109+
require.NoError(t, err)
110+
111+
t.Logf("Create a container with volume-ownership test image")
112+
cnConfig := ContainerConfig(
113+
"container",
114+
testImage,
115+
WithCommand("tail", "-f", "/dev/null"),
116+
)
117+
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
118+
require.NoError(t, err)
119+
120+
t.Logf("Start the container")
121+
require.NoError(t, runtimeService.StartContainer(cn))
122+
123+
// gcr.io/k8s-cri-containerd/volume-ownership:1.0 contains a test_dir
124+
// volume, which is owned by nobody:nogroup.
125+
t.Logf("Check ownership of test directory inside container")
126+
stdout, stderr, err := runtimeService.ExecSync(cn, []string{
127+
"stat", "-c", "%U:%G", "/test_dir",
128+
}, execTimeout)
129+
require.NoError(t, err)
130+
assert.Empty(t, stderr)
131+
assert.Equal(t, "nobody:nogroup\n", string(stdout))
132+
133+
t.Logf("Check ownership of test directory on the host")
134+
hostCmd := fmt.Sprintf("find %s/containers/%s/volumes/* | xargs stat -c %%U:%%G", *criRoot, cn)
135+
output, err := exec.Command("sh", "-c", hostCmd).CombinedOutput()
136+
require.NoError(t, err)
137+
assert.Equal(t, "nobody:nogroup\n", string(output))
138+
}

pkg/containerd/opts/container.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func WithNewSnapshot(id string, i containerd.Image) containerd.NewContainerOpts
5353
// WithVolumes copies ownership of volume in rootfs to its corresponding host path.
5454
// It doesn't update runtime spec.
5555
// The passed in map is a host path to container path map for all volumes.
56-
// TODO(random-liu): Figure out whether we need to copy volume content.
5756
func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
5857
return func(ctx context.Context, client *containerd.Client, c *containers.Container) (err error) {
5958
if c.Snapshotter == "" {
@@ -108,14 +107,6 @@ func WithVolumes(volumeMounts map[string]string) containerd.NewContainerOpts {
108107
// copyExistingContents copies from the source to the destination and
109108
// ensures the ownership is appropriately set.
110109
func copyExistingContents(source, destination string) error {
111-
srcList, err := ioutil.ReadDir(source)
112-
if err != nil {
113-
return err
114-
}
115-
if len(srcList) == 0 {
116-
// Skip copying if source directory is empty.
117-
return nil
118-
}
119110
dstList, err := ioutil.ReadDir(destination)
120111
if err != nil {
121112
return err

0 commit comments

Comments
 (0)