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