Merge pull request #5312 from AkihiroSuda/expose-WithoutRunMount

pkg/cri/opts.WithoutRunMount -> oci.WithoutRunMount
This commit is contained in:
Maksym Pavlenko 2021-04-07 12:06:10 -07:00 committed by GitHub
commit a4bc81779d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 18 deletions

View File

@ -273,6 +273,28 @@ func WithMounts(mounts []specs.Mount) SpecOpts {
}
}
// WithoutMounts removes mounts
func WithoutMounts(dests ...string) SpecOpts {
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
var (
mounts []specs.Mount
current = s.Mounts
)
mLoop:
for _, m := range current {
mDestination := filepath.Clean(m.Destination)
for _, dest := range dests {
if mDestination == dest {
continue mLoop
}
}
mounts = append(mounts, m)
}
s.Mounts = mounts
return nil
}
}
// WithHostNamespace allows a task to run inside the host's linux namespace
func WithHostNamespace(ns specs.LinuxNamespaceType) SpecOpts {
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {

View File

@ -247,3 +247,8 @@ var WithAllKnownCapabilities = func(ctx context.Context, client Client, c *conta
caps := cap.Known()
return WithCapabilities(caps)(ctx, client, c, s)
}
// WithoutRunMount removes the `/run` inside the spec
func WithoutRunMount(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
return WithoutMounts("/run")(ctx, client, c, s)
}

View File

@ -25,6 +25,7 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
@ -601,3 +602,59 @@ func getShmSize(opts []string) string {
}
return ""
}
func TestWithoutMounts(t *testing.T) {
t.Parallel()
var s Spec
x := func(s string) string {
if runtime.GOOS == "windows" {
return filepath.Join("C:\\", filepath.Clean(s))
}
return s
}
opts := []SpecOpts{
WithMounts([]specs.Mount{
{
Destination: x("/dst1"),
Source: x("/src1"),
},
{
Destination: x("/dst2"),
Source: x("/src2"),
},
{
Destination: x("/dst3"),
Source: x("/src3"),
},
}),
WithoutMounts(x("/dst2"), x("/dst3")),
WithMounts([]specs.Mount{
{
Destination: x("/dst4"),
Source: x("/src4"),
},
}),
}
expected := []specs.Mount{
{
Destination: x("/dst1"),
Source: x("/src1"),
},
{
Destination: x("/dst4"),
Source: x("/src4"),
},
}
for _, opt := range opts {
if err := opt(nil, nil, nil, &s); err != nil {
t.Fatal(err)
}
}
if !reflect.DeepEqual(expected, s.Mounts) {
t.Fatalf("expected %+v, got %+v", expected, s.Mounts)
}
}

View File

@ -76,22 +76,6 @@ func mergeGids(gids1, gids2 []uint32) []uint32 {
return gids
}
// WithoutRunMount removes the `/run` inside the spec
func WithoutRunMount(_ context.Context, _ oci.Client, c *containers.Container, s *runtimespec.Spec) error {
var (
mounts []runtimespec.Mount
current = s.Mounts
)
for _, m := range current {
if filepath.Clean(m.Destination) == "/run" {
continue
}
mounts = append(mounts, m)
}
s.Mounts = mounts
return nil
}
// WithoutDefaultSecuritySettings removes the default security settings generated on a spec
func WithoutDefaultSecuritySettings(_ context.Context, _ oci.Client, c *containers.Container, s *runtimespec.Spec) error {
if s.Process == nil {

View File

@ -122,7 +122,7 @@ func (c *criService) containerSpec(
ociRuntime config.Runtime,
) (_ *runtimespec.Spec, retErr error) {
specOpts := []oci.SpecOpts{
customopts.WithoutRunMount,
oci.WithoutRunMount,
}
// only clear the default security settings if the runtime does not have a custom
// base runtime spec spec. Admins can use this functionality to define

View File

@ -41,7 +41,7 @@ func (c *criService) sandboxContainerSpec(id string, config *runtime.PodSandboxC
// Creates a spec Generator with the default spec.
// TODO(random-liu): [P1] Compare the default settings with docker and containerd default.
specOpts := []oci.SpecOpts{
customopts.WithoutRunMount,
oci.WithoutRunMount,
customopts.WithoutDefaultSecuritySettings,
customopts.WithRelativeRoot(relativeRootfsPath),
oci.WithEnv(imageConfig.Env),