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 cpu quota/period #344

Merged
merged 1 commit into from Aug 17, 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
32 changes: 25 additions & 7 deletions pkg/oci/utils.go
Expand Up @@ -286,13 +286,14 @@ 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) {
resources := config.VMConfig

if ocispec.Linux == nil ||
ocispec.Linux.Resources == nil ||
ocispec.Linux.Resources.Memory == nil ||
ocispec.Linux.Resources.Memory.Limit == nil {
return config.VMConfig, nil
return resources, nil
}

memBytes := *ocispec.Linux.Resources.Memory.Limit
Expand All @@ -302,12 +303,29 @@ func vmConfig(ocispec CompatOCISpec, config RuntimeConfig) (vc.Resources, error)
}

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

if ocispec.Linux.Resources.CPU == nil ||
ocispec.Linux.Resources.CPU.Quota == nil ||
ocispec.Linux.Resources.CPU.Period == nil {
return resources, nil
}

quota := *ocispec.Linux.Resources.CPU.Quota
period := *ocispec.Linux.Resources.CPU.Period

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

if period == 0 {
return vc.Resources{}, fmt.Errorf("Invalid OCI cpu period %d", period)
}

// round up to 1 CPU
resources.VCPUs = uint((uint64(quota) + (period - 1)) / period)

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

// PodConfig converts an OCI compatible runtime configuration file
Expand Down
25 changes: 25 additions & 0 deletions pkg/oci/utils_test.go
Expand Up @@ -163,6 +163,8 @@ func TestMinimalPodConfig(t *testing.T) {

func TestVmConfig(t *testing.T) {
var limitBytes int64 = 128 * 1024 * 1024
var quota int64 = 200000
var period uint64 = 100000

config := RuntimeConfig{
VMConfig: vc.Resources{
Expand All @@ -172,6 +174,7 @@ func TestVmConfig(t *testing.T) {

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

ocispec := CompatOCISpec{
Expand All @@ -181,6 +184,10 @@ func TestVmConfig(t *testing.T) {
Memory: &specs.LinuxMemory{
Limit: &limitBytes,
},
CPU: &specs.LinuxCPU{
Quota: &quota,
Period: &period,
},
},
},
},
Expand All @@ -202,6 +209,24 @@ func TestVmConfig(t *testing.T) {
if err == nil {
t.Fatalf("Got %v\n expecting error", resources)
}

limitBytes = 128 * 1024 * 1024
quota = -1
ocispec.Linux.Resources.CPU.Quota = &quota

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

quota = 100000
period = 0
ocispec.Linux.Resources.CPU.Quota = &quota

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) {
Expand Down