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

Commit

Permalink
config: Add default_{vcpus, memory} configurations
Browse files Browse the repository at this point in the history
Introduce two new configuration settings:
 * default_vcpus - defines default vCPU number for newly created PODs
 * default_memory - defines default memory size for newly created PODs

Fixes #164.

Signed-off-by: Dmitry Voytik <[email protected]>
  • Loading branch information
Dmitry Voytik committed Jul 13, 2017
1 parent 9eb5477 commit 5682afc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ PAUSEBINRELPATH := bin/pause

GLOBALLOGPATH := $(PKGLIBDIR)/runtime/runtime.log

DEFDEFVCPUS := 1
DEFDEFMEMSZ := 2048

SED = sed

SOURCES := $(shell find . 2>&1 | grep -E '.*\.(c|h|go)$$')
Expand Down Expand Up @@ -139,6 +142,8 @@ USER_VARS += SHAREDIR
USER_VARS += SHIMPATH
USER_VARS += SYSCONFDIR
USER_VARS += PAUSEDESTDIR
USER_VARS += DEFDEFVCPUS
USER_VARS += DEFDEFMEMSZ


V = @
Expand Down Expand Up @@ -178,6 +183,9 @@ const defaultRuntimeRun = "$(PKGRUNDIR)"
const defaultShimPath = "$(SHIMPATH)"
const pauseBinRelativePath = "$(PAUSEBINRELPATH)"

const defaultDefaultVCPUs uint32 = $(DEFDEFVCPUS)
const defaultDefaultMemSz uint32 = $(DEFDEFMEMSZ)

// Required to be modifiable (for the tests)
var defaultRuntimeConfiguration = "$(DESTCONFIG)"
var defaultProxyPath = "$(PROXYPATH)"
Expand Down Expand Up @@ -223,6 +231,8 @@ $(CONFIG): $(CONFIG_IN)
-e "s|@QEMUPATH@|$(QEMUPATH)|g" \
-e "s|@SHIMPATH@|$(SHIMPATH)|g" \
-e "s|@GLOBALLOGPATH@|$(GLOBALLOGPATH)|g" \
-e "s|@DEFDEFVCPUS@|$(DEFDEFVCPUS)|g" \
-e "s|@DEFDEFMEMSZ@|$(DEFDEFMEMSZ)|g" \
$< > $@

generate-config: $(CONFIG)
Expand Down
35 changes: 32 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
goruntime "runtime"

"github.com/BurntSushi/toml"
vc "github.com/containers/virtcontainers"
Expand Down Expand Up @@ -73,9 +74,11 @@ type tomlConfig struct {
}

type hypervisor struct {
Path string
Kernel string
Image string
Path string
Kernel string
Image string
DefaultVCPUs int32 `toml:"default_vcpus"`
DefaultMemSz uint32 `toml:"default_memory"`
}

type proxy struct {
Expand Down Expand Up @@ -118,6 +121,28 @@ func (h hypervisor) image() string {
return h.Image
}

func (h hypervisor) defaultVCPUs() uint32 {
if h.DefaultVCPUs < 0 {
return uint32(goruntime.NumCPU())
}
if h.DefaultVCPUs == 0 { // or unspecified
return defaultDefaultVCPUs
}
if h.DefaultVCPUs > 255 { // qemu supports max 255
return 255
}

return uint32(h.DefaultVCPUs)
}

func (h hypervisor) defaultMemSz() uint32 {
if h.DefaultMemSz < 8 {
return defaultDefaultMemSz // MiB
}

return h.DefaultMemSz
}

func (p proxy) url() string {
if p.URL == "" {
return defaultProxyURL
Expand Down Expand Up @@ -158,6 +183,8 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
HypervisorPath: hypervisor,
KernelPath: kernel,
ImagePath: image,
DefaultVCPUs: h.defaultVCPUs(),
DefaultMemSz: h.defaultMemSz(),
}, nil
}

Expand Down Expand Up @@ -264,6 +291,8 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat
HypervisorPath: defaultHypervisorPath,
KernelPath: defaultKernelPath,
ImagePath: defaultImagePath,
DefaultVCPUs: defaultDefaultVCPUs,
DefaultMemSz: defaultDefaultMemSz,
}

defaultAgentConfig := vc.HyperConfig{
Expand Down
8 changes: 8 additions & 0 deletions config/configuration.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
path = "@QEMUPATH@"
kernel = "@KERNELPATH@"
image = "@IMAGEPATH@"
# Default vCPU number per POD/VM:
# unspecified or 0 --> will be set to @DEFDEFVCPUS@
# < 0 --> will be set to the actual number of physical cores
# > 255 --> will be set to 255.
default_vcpus = -1
# Default memory size in MiB for POD/VM.
# If unspecified then it will be set @DEFDEFMEMSZ@
#default_memory = @DEFDEFMEMSZ@

[proxy.cc]
url = "@PROXYURL@"
Expand Down
24 changes: 24 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"path"
"path/filepath"
"reflect"
goruntime "runtime"
"strconv"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -49,6 +51,8 @@ func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath
path = "` + hypervisorPath + `"
kernel = "` + kernelPath + `"
image = "` + imagePath + `"
default_vcpus = ` + strconv.FormatUint(uint64(defaultDefaultVCPUs), 10) + `
default_memory = ` + strconv.FormatUint(uint64(defaultDefaultMemSz), 10) + `
[proxy.cc]
url = "` + proxyURL + `"
Expand Down Expand Up @@ -131,6 +135,8 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf
HypervisorPath: hypervisorPath,
KernelPath: kernelPath,
ImagePath: imagePath,
DefaultVCPUs: defaultDefaultVCPUs,
DefaultMemSz: defaultDefaultMemSz,
}

agentConfig := vc.HyperConfig{
Expand Down Expand Up @@ -586,6 +592,8 @@ func TestMinimalRuntimeConfig(t *testing.T) {
HypervisorPath: defaultHypervisorPath,
KernelPath: defaultKernelPath,
ImagePath: defaultImagePath,
DefaultVCPUs: defaultDefaultVCPUs,
DefaultMemSz: defaultDefaultMemSz,
}

expectedAgentConfig := vc.HyperConfig{
Expand Down Expand Up @@ -770,6 +778,8 @@ func TestHypervisorDefaults(t *testing.T) {
assert.Equal(t, h.path(), defaultHypervisorPath, "default hypervisor path wrong")
assert.Equal(t, h.kernel(), defaultKernelPath, "default hypervisor kernel wrong")
assert.Equal(t, h.image(), defaultImagePath, "default hypervisor image wrong")
assert.Equal(t, h.defaultVCPUs(), defaultDefaultVCPUs, "default vCPU number is wrong")
assert.Equal(t, h.defaultMemSz(), defaultDefaultMemSz, "default memory size is wrong")

path := "/foo"
h.Path = path
Expand All @@ -782,6 +792,20 @@ func TestHypervisorDefaults(t *testing.T) {
image := "foo"
h.Image = image
assert.Equal(t, h.image(), image, "custom hypervisor image wrong")

// auto inferring
h.DefaultVCPUs = -1
assert.Equal(t, h.defaultVCPUs(), uint32(goruntime.NumCPU()), "default vCPU number is wrong")

h.DefaultVCPUs = 2
assert.Equal(t, h.defaultVCPUs(), uint32(2), "default vCPU number is wrong")

// qemu supports max 255
h.DefaultVCPUs = 8086
assert.Equal(t, h.defaultVCPUs(), uint32(255), "default vCPU number is wrong")

h.DefaultMemSz = 1024
assert.Equal(t, h.defaultMemSz(), uint32(1024), "default memory size is wrong")
}

func TestProxyDefaults(t *testing.T) {
Expand Down

0 comments on commit 5682afc

Please sign in to comment.