Skip to content

Commit

Permalink
Merge pull request #1072 from Random-Liu/clean-path
Browse files Browse the repository at this point in the history
Use clean path for map and comparison.
  • Loading branch information
Random-Liu committed Mar 4, 2019
2 parents 210e802 + 0464298 commit 95f564f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pkg/server/container_create.go
Expand Up @@ -186,7 +186,7 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
if len(volumeMounts) > 0 {
mountMap := make(map[string]string)
for _, v := range volumeMounts {
mountMap[v.HostPath] = v.ContainerPath
mountMap[filepath.Clean(v.HostPath)] = v.ContainerPath
}
opts = append(opts, customopts.WithVolumes(mountMap))
}
Expand Down Expand Up @@ -750,7 +750,7 @@ func setOCIBindMountsPrivileged(g *generator) {
spec := g.Config
// clear readonly for /sys and cgroup
for i, m := range spec.Mounts {
if spec.Mounts[i].Destination == "/sys" {
if filepath.Clean(spec.Mounts[i].Destination) == "/sys" {
clearReadOnly(&spec.Mounts[i])
}
if m.Type == "cgroup" {
Expand Down Expand Up @@ -908,7 +908,7 @@ func defaultRuntimeSpec(id string) (*runtimespec.Spec, error) {
// TODO(random-liu): Mount tmpfs for /run and handle copy-up.
var mounts []runtimespec.Mount
for _, mount := range spec.Mounts {
if mount.Destination == "/run" {
if filepath.Clean(mount.Destination) == "/run" {
continue
}
mounts = append(mounts, mount)
Expand Down
20 changes: 18 additions & 2 deletions pkg/server/container_create_test.go
Expand Up @@ -307,7 +307,8 @@ func TestContainerSpecWithExtraMounts(t *testing.T) {
config, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
c := newTestCRIService()
mountInConfig := &runtime.Mount{
ContainerPath: "test-container-path",
// Test cleanpath
ContainerPath: "test-container-path/",
HostPath: "test-host-path",
Readonly: false,
}
Expand All @@ -334,7 +335,7 @@ func TestContainerSpecWithExtraMounts(t *testing.T) {
specCheck(t, testID, testSandboxID, testPid, spec)
var mounts, sysMounts, devMounts []runtimespec.Mount
for _, m := range spec.Mounts {
if m.Destination == "test-container-path" {
if strings.HasPrefix(m.Destination, "test-container-path") {
mounts = append(mounts, m)
} else if m.Destination == "/sys" {
sysMounts = append(sysMounts, m)
Expand Down Expand Up @@ -499,6 +500,21 @@ func TestGenerateVolumeMounts(t *testing.T) {
"/test-volume-2",
},
},
"should compare and return cleanpath": {
criMounts: []*runtime.Mount{
{
ContainerPath: "/test-volume-1",
HostPath: "/test-hostpath-1",
},
},
imageVolumes: map[string]struct{}{
"/test-volume-1/": {},
"/test-volume-2/": {},
},
expectedMountDest: []string{
"/test-volume-2/",
},
},
} {
t.Logf("TestCase %q", desc)
config := &imagespec.ImageConfig{
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/helpers.go
Expand Up @@ -374,7 +374,7 @@ func checkSelinuxLevel(level string) (bool, error) {
// isInCRIMounts checks whether a destination is in CRI mount list.
func isInCRIMounts(dst string, mounts []*runtime.Mount) bool {
for _, m := range mounts {
if m.ContainerPath == dst {
if filepath.Clean(m.ContainerPath) == filepath.Clean(dst) {
return true
}
}
Expand Down

0 comments on commit 95f564f

Please sign in to comment.