Skip to content

Commit 8ba8533

Browse files
committed
pkg/cri/opts.WithoutRunMount -> oci.WithoutRunMount
Move `pkg/cri/opts.WithoutRunMount` function to `oci.WithoutRunMount` so that it can be used without dependency on CRI. Also add `oci.WithoutMounts(dests ...string)` for generality. Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 85041ff commit 8ba8533

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
lines changed

oci/spec_opts.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,28 @@ func WithMounts(mounts []specs.Mount) SpecOpts {
273273
}
274274
}
275275

276+
// WithoutMounts removes mounts
277+
func WithoutMounts(dests ...string) SpecOpts {
278+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
279+
var (
280+
mounts []specs.Mount
281+
current = s.Mounts
282+
)
283+
mLoop:
284+
for _, m := range current {
285+
mDestination := filepath.Clean(m.Destination)
286+
for _, dest := range dests {
287+
if mDestination == dest {
288+
continue mLoop
289+
}
290+
}
291+
mounts = append(mounts, m)
292+
}
293+
s.Mounts = mounts
294+
return nil
295+
}
296+
}
297+
276298
// WithHostNamespace allows a task to run inside the host's linux namespace
277299
func WithHostNamespace(ns specs.LinuxNamespaceType) SpecOpts {
278300
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {

oci/spec_opts_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,8 @@ var WithAllKnownCapabilities = func(ctx context.Context, client Client, c *conta
247247
caps := cap.Known()
248248
return WithCapabilities(caps)(ctx, client, c, s)
249249
}
250+
251+
// WithoutRunMount removes the `/run` inside the spec
252+
func WithoutRunMount(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
253+
return WithoutMounts("/run")(ctx, client, c, s)
254+
}

oci/spec_opts_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io/ioutil"
2626
"log"
2727
"os"
28+
"path/filepath"
2829
"reflect"
2930
"runtime"
3031
"strings"
@@ -601,3 +602,59 @@ func getShmSize(opts []string) string {
601602
}
602603
return ""
603604
}
605+
606+
func TestWithoutMounts(t *testing.T) {
607+
t.Parallel()
608+
var s Spec
609+
610+
x := func(s string) string {
611+
if runtime.GOOS == "windows" {
612+
return filepath.Join("C:\\", filepath.Clean(s))
613+
}
614+
return s
615+
}
616+
opts := []SpecOpts{
617+
WithMounts([]specs.Mount{
618+
{
619+
Destination: x("/dst1"),
620+
Source: x("/src1"),
621+
},
622+
{
623+
Destination: x("/dst2"),
624+
Source: x("/src2"),
625+
},
626+
{
627+
Destination: x("/dst3"),
628+
Source: x("/src3"),
629+
},
630+
}),
631+
WithoutMounts(x("/dst2"), x("/dst3")),
632+
WithMounts([]specs.Mount{
633+
{
634+
Destination: x("/dst4"),
635+
Source: x("/src4"),
636+
},
637+
}),
638+
}
639+
640+
expected := []specs.Mount{
641+
{
642+
Destination: x("/dst1"),
643+
Source: x("/src1"),
644+
},
645+
{
646+
Destination: x("/dst4"),
647+
Source: x("/src4"),
648+
},
649+
}
650+
651+
for _, opt := range opts {
652+
if err := opt(nil, nil, nil, &s); err != nil {
653+
t.Fatal(err)
654+
}
655+
}
656+
657+
if !reflect.DeepEqual(expected, s.Mounts) {
658+
t.Fatalf("expected %+v, got %+v", expected, s.Mounts)
659+
}
660+
}

pkg/cri/opts/spec_linux.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,6 @@ func mergeGids(gids1, gids2 []uint32) []uint32 {
7676
return gids
7777
}
7878

79-
// WithoutRunMount removes the `/run` inside the spec
80-
func WithoutRunMount(_ context.Context, _ oci.Client, c *containers.Container, s *runtimespec.Spec) error {
81-
var (
82-
mounts []runtimespec.Mount
83-
current = s.Mounts
84-
)
85-
for _, m := range current {
86-
if filepath.Clean(m.Destination) == "/run" {
87-
continue
88-
}
89-
mounts = append(mounts, m)
90-
}
91-
s.Mounts = mounts
92-
return nil
93-
}
94-
9579
// WithoutDefaultSecuritySettings removes the default security settings generated on a spec
9680
func WithoutDefaultSecuritySettings(_ context.Context, _ oci.Client, c *containers.Container, s *runtimespec.Spec) error {
9781
if s.Process == nil {

pkg/cri/server/container_create_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (c *criService) containerSpec(
122122
ociRuntime config.Runtime,
123123
) (_ *runtimespec.Spec, retErr error) {
124124
specOpts := []oci.SpecOpts{
125-
customopts.WithoutRunMount,
125+
oci.WithoutRunMount,
126126
}
127127
// only clear the default security settings if the runtime does not have a custom
128128
// base runtime spec spec. Admins can use this functionality to define

pkg/cri/server/sandbox_run_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (c *criService) sandboxContainerSpec(id string, config *runtime.PodSandboxC
4141
// Creates a spec Generator with the default spec.
4242
// TODO(random-liu): [P1] Compare the default settings with docker and containerd default.
4343
specOpts := []oci.SpecOpts{
44-
customopts.WithoutRunMount,
44+
oci.WithoutRunMount,
4545
customopts.WithoutDefaultSecuritySettings,
4646
customopts.WithRelativeRoot(relativeRootfsPath),
4747
oci.WithEnv(imageConfig.Env),

0 commit comments

Comments
 (0)