From cd5cdf3872e386e922ce3ded90840ca91ed50a5e Mon Sep 17 00:00:00 2001 From: WANG Chao Date: Wed, 9 Aug 2017 19:51:11 +0800 Subject: [PATCH] create: support oci spec memory limit We should run VM(container) regarding oci config.json memory limit. Fixes #381 Signed-off-by: WANG Chao --- config.go | 29 +++++++++++++++++++++++++++++ create.go | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/config.go b/config.go index 99e0c786..2cfc2154 100644 --- a/config.go +++ b/config.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "io/ioutil" + "math" "os" "path/filepath" goruntime "runtime" @@ -376,3 +377,31 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat return resolved, logfilePath, config, nil } + +func ociResourceUpdateRuntimeConfig(ocispec oci.CompatOCISpec, config *oci.RuntimeConfig) error { + if ocispec.Linux == nil { + return nil + } + + if ocispec.Linux.Resources == nil { + return nil + } + + if ocispec.Linux.Resources.Memory == nil { + return nil + } + + if ocispec.Linux.Resources.Memory.Limit == nil { + return nil + } + + memBytes := *ocispec.Linux.Resources.Memory.Limit + + mem := int(math.Ceil(float64(memBytes) / 1024 / 1024)) + if mem <= 0 { + return fmt.Errorf("Invalid OCI memory limit %d", memBytes) + } + config.VMConfig.Memory = uint(mem) + + return nil +} diff --git a/create.go b/create.go index 0aa67ded..ed09bcf7 100644 --- a/create.go +++ b/create.go @@ -98,6 +98,10 @@ func create(containerID, bundlePath, console, pidFilePath string, detach bool, return err } + if err := ociResourceUpdateRuntimeConfig(ociSpec, &runtimeConfig); err != nil { + return err + } + containerType, err := ociSpec.ContainerType() if err != nil { return err