Skip to content

Commit cbca58e

Browse files
Merge pull request #13320 from giuseppe/play-kube-honor-propagation-mode
kube: honor mount propagation mode
2 parents 8b24324 + ed73040 commit cbca58e

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

Diff for: pkg/specgen/generate/kube/kube.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
322322
continue
323323
}
324324

325-
dest, options, err := parseMountPath(volume.MountPath, volume.ReadOnly)
325+
dest, options, err := parseMountPath(volume.MountPath, volume.ReadOnly, volume.MountPropagation)
326326
if err != nil {
327327
return nil, err
328328
}
@@ -388,7 +388,7 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
388388
return s, nil
389389
}
390390

391-
func parseMountPath(mountPath string, readOnly bool) (string, []string, error) {
391+
func parseMountPath(mountPath string, readOnly bool, propagationMode *v1.MountPropagationMode) (string, []string, error) {
392392
options := []string{}
393393
splitVol := strings.Split(mountPath, ":")
394394
if len(splitVol) > 2 {
@@ -408,6 +408,18 @@ func parseMountPath(mountPath string, readOnly bool) (string, []string, error) {
408408
if err != nil {
409409
return "", opts, errors.Wrapf(err, "parsing MountOptions")
410410
}
411+
if propagationMode != nil {
412+
switch *propagationMode {
413+
case v1.MountPropagationNone:
414+
opts = append(opts, "private")
415+
case v1.MountPropagationHostToContainer:
416+
opts = append(opts, "rslave")
417+
case v1.MountPropagationBidirectional:
418+
opts = append(opts, "rshared")
419+
default:
420+
return "", opts, errors.Errorf("unknown propagation mode %q", *propagationMode)
421+
}
422+
}
411423
return dest, opts, nil
412424
}
413425

Diff for: pkg/specgen/generate/kube/kube_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package kube
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
v1 "k8s.io/api/core/v1"
8+
//"github.com/stretchr/testify/require"
9+
)
10+
11+
func testPropagation(t *testing.T, propagation v1.MountPropagationMode, expected string) {
12+
dest, options, err := parseMountPath("/to", false, &propagation)
13+
assert.NoError(t, err)
14+
assert.Equal(t, dest, "/to")
15+
assert.Contains(t, options, expected)
16+
}
17+
18+
func TestParseMountPathPropagation(t *testing.T) {
19+
testPropagation(t, v1.MountPropagationNone, "private")
20+
testPropagation(t, v1.MountPropagationHostToContainer, "rslave")
21+
testPropagation(t, v1.MountPropagationBidirectional, "rshared")
22+
23+
prop := v1.MountPropagationMode("SpaceWave")
24+
_, _, err := parseMountPath("/to", false, &prop)
25+
assert.Error(t, err)
26+
27+
_, options, err := parseMountPath("/to", false, nil)
28+
assert.NoError(t, err)
29+
assert.NotContains(t, options, "private")
30+
assert.NotContains(t, options, "rslave")
31+
assert.NotContains(t, options, "rshared")
32+
}
33+
34+
func TestParseMountPathRO(t *testing.T) {
35+
_, options, err := parseMountPath("/to", true, nil)
36+
assert.NoError(t, err)
37+
assert.Contains(t, options, "ro")
38+
39+
_, options, err = parseMountPath("/to", false, nil)
40+
assert.NoError(t, err)
41+
assert.NotContains(t, options, "ro")
42+
}

0 commit comments

Comments
 (0)