Merge pull request #3655 from crosbymichael/shmsize

Add Opt for modifying shm size
This commit is contained in:
Phil Estes 2019-09-18 11:54:46 -04:00 committed by GitHub
commit a7e67ff9aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 1 deletions

View File

@ -1222,3 +1222,28 @@ func WithEnvFile(path string) SpecOpts {
return WithEnv(vars)(nil, nil, nil, s)
}
}
// ErrNoShmMount is returned when there is no /dev/shm mount specified in the config
// and an Opts was trying to set a configuration value on the mount.
var ErrNoShmMount = errors.New("no /dev/shm mount specified")
// WithDevShmSize sets the size of the /dev/shm mount for the container.
//
// The size value is specified in kb, kilobytes.
func WithDevShmSize(kb int64) SpecOpts {
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
for _, m := range s.Mounts {
if m.Source == "shm" && m.Type == "tmpfs" {
for i, o := range m.Options {
if strings.HasPrefix(o, "size=") {
m.Options[i] = fmt.Sprintf("size=%dk", kb)
return nil
}
}
m.Options = append(m.Options, fmt.Sprintf("size=%dk", kb))
return nil
}
}
return ErrNoShmMount
}
}

View File

@ -27,6 +27,7 @@ import (
"os"
"reflect"
"runtime"
"strings"
"testing"
"github.com/containerd/containerd/content"
@ -506,3 +507,56 @@ func TestDropCaps(t *testing.T) {
}
}
}
func TestDevShmSize(t *testing.T) {
t.Parallel()
var (
s Spec
c = containers.Container{ID: t.Name()}
ctx = namespaces.WithNamespace(context.Background(), "test")
)
err := populateDefaultUnixSpec(ctx, &s, c.ID)
if err != nil {
t.Fatal(err)
}
expected := "1024k"
if err := WithDevShmSize(1024)(nil, nil, nil, &s); err != nil {
t.Fatal(err)
}
m := getShmMount(&s)
if m == nil {
t.Fatal("no shm mount found")
}
o := getShmSize(m.Options)
if o == "" {
t.Fatal("shm size not specified")
}
parts := strings.Split(o, "=")
if len(parts) != 2 {
t.Fatal("invalid size format")
}
size := parts[1]
if size != expected {
t.Fatalf("size %s not equal %s", size, expected)
}
}
func getShmMount(s *Spec) *specs.Mount {
for _, m := range s.Mounts {
if m.Source == "shm" && m.Type == "tmpfs" {
return &m
}
}
return nil
}
func getShmSize(opts []string) string {
for _, o := range opts {
if strings.HasPrefix(o, "size=") {
return o
}
}
return ""
}

View File

@ -32,7 +32,7 @@ func TestWithImageConfigNoEnv(t *testing.T) {
t.Parallel()
var (
s Spec
c = containers.Container{ID: "TestWithImageConfigNoEnv"}
c = containers.Container{ID: t.Name()}
ctx = namespaces.WithNamespace(context.Background(), "test")
)