Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

pkg: oci: support OCI spec memory limit #341

Merged
merged 1 commit into from Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion pkg/oci/utils.go
Expand Up @@ -286,6 +286,30 @@ func (spec *CompatOCISpec) PodID() (string, error) {
return "", fmt.Errorf("Could not find pod ID")
}

// TODO: calculate number of VCPUs from OCI spec
func vmConfig(ocispec CompatOCISpec, config RuntimeConfig) (vc.Resources, error) {
if ocispec.Linux == nil ||
ocispec.Linux.Resources == nil ||
ocispec.Linux.Resources.Memory == nil ||
ocispec.Linux.Resources.Memory.Limit == nil {
return config.VMConfig, nil
}

memBytes := *ocispec.Linux.Resources.Memory.Limit

if memBytes <= 0 {
return vc.Resources{}, fmt.Errorf("Invalid OCI memory limit %d", memBytes)
}

// round up memory to 1MB
mem := uint((memBytes + (1024*1024 - 1)) / (1024 * 1024))

return vc.Resources{
VCPUs: config.VMConfig.VCPUs,
Memory: mem,
}, nil
}

// PodConfig converts an OCI compatible runtime configuration file
// to a virtcontainers pod configuration structure.
func PodConfig(ocispec CompatOCISpec, runtime RuntimeConfig, bundlePath, cid, console string, detach bool) (vc.PodConfig, error) {
Expand All @@ -301,12 +325,17 @@ func PodConfig(ocispec CompatOCISpec, runtime RuntimeConfig, bundlePath, cid, co
return vc.PodConfig{}, err
}

resources, err := vmConfig(ocispec, runtime)
if err != nil {
return vc.PodConfig{}, err
}

podConfig := vc.PodConfig{
ID: cid,

Hooks: containerHooks(ocispec),

VMConfig: runtime.VMConfig,
VMConfig: resources,

HypervisorType: runtime.HypervisorType,
HypervisorConfig: runtime.HypervisorConfig,
Expand Down
43 changes: 43 additions & 0 deletions pkg/oci/utils_test.go
Expand Up @@ -161,6 +161,49 @@ func TestMinimalPodConfig(t *testing.T) {
}
}

func TestVmConfig(t *testing.T) {
var limitBytes int64 = 128 * 1024 * 1024

config := RuntimeConfig{
VMConfig: vc.Resources{
Memory: 2048,
},
}

expectedResources := vc.Resources{
Memory: 128,
}

ocispec := CompatOCISpec{
Spec: specs.Spec{
Linux: &specs.Linux{
Resources: &specs.LinuxResources{
Memory: &specs.LinuxMemory{
Limit: &limitBytes,
},
},
},
},
}

resources, err := vmConfig(ocispec, config)
if err != nil {
t.Fatal(err)
}

if reflect.DeepEqual(resources, expectedResources) == false {
t.Fatalf("Got %v\n expecting %v", resources, expectedResources)
}

limitBytes = -128 * 1024 * 1024
ocispec.Linux.Resources.Memory.Limit = &limitBytes

resources, err = vmConfig(ocispec, config)
if err == nil {
t.Fatalf("Got %v\n expecting error", resources)
}
}

func testStatusToOCIStateSuccessful(t *testing.T, cStatus vc.ContainerStatus, expected specs.State) {
ociState, err := StatusToOCIState(cStatus)
if err != nil {
Expand Down