Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
create: support oci spec memory limit
Browse files Browse the repository at this point in the history
We should run VM(container) regarding oci config.json memory limit.

Fixes #381

Signed-off-by: WANG Chao <chao.wang@ucloud.cn>
  • Loading branch information
wcwxyz committed Aug 11, 2017
1 parent 939f7e3 commit cd5cdf3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
29 changes: 29 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
goruntime "runtime"
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd5cdf3

Please sign in to comment.