From fe1563eca5452afb81eb39d7d09cf27b417b6d94 Mon Sep 17 00:00:00 2001 From: Dmitry Voytik Date: Fri, 7 Jul 2017 16:58:17 +0200 Subject: [PATCH 1/2] vendor: Update virtcontainers vendoring Shortlog since last vendoring: ceb467e hypervisor: Add Default{VCPUs, MemSz} to HypervisorConfig e6862a1 1.0.0-rc.3 release The change is required to fix #164. Signed-off-by: Dmitry Voytik --- Gopkg.lock | 2 +- .../github.com/containers/virtcontainers/NEWS | 6 +++++ .../containers/virtcontainers/hypervisor.go | 22 +++++++++++++++++++ .../virtcontainers/hypervisor_test.go | 20 +++++++++++++++++ .../containers/virtcontainers/qemu.go | 15 +++++-------- .../containers/virtcontainers/qemu_test.go | 2 ++ 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 4a43c741..96633be3 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -47,7 +47,7 @@ branch = "master" name = "github.com/containers/virtcontainers" packages = [".","pkg/cni","pkg/hyperstart","pkg/oci"] - revision = "fd5f0dd25a809c61cd4f7f6dfa3d55864961cb80" + revision = "e01ebd015f77b3e822b597088e6efc102a7e6d9a" [[projects]] branch = "master" diff --git a/vendor/github.com/containers/virtcontainers/NEWS b/vendor/github.com/containers/virtcontainers/NEWS index 651c7d67..9f1e6e3b 100644 --- a/vendor/github.com/containers/virtcontainers/NEWS +++ b/vendor/github.com/containers/virtcontainers/NEWS @@ -1,3 +1,9 @@ +1.0.0-rc.3: + Added support for assigning the container rootfs underlying block device to the VM (when using devicemapper) + Changed qemu to use virtio-net vhost by default + Changed qemu parameters to not be Clear Containers specific + Fixed MTU passing to hyperstart + 1.0.0-rc.2: Added support for transitioning from Ready to Stopped Fixed pod container creation diff --git a/vendor/github.com/containers/virtcontainers/hypervisor.go b/vendor/github.com/containers/virtcontainers/hypervisor.go index 96b735d0..14ca8c2a 100644 --- a/vendor/github.com/containers/virtcontainers/hypervisor.go +++ b/vendor/github.com/containers/virtcontainers/hypervisor.go @@ -31,6 +31,12 @@ const ( MockHypervisor HypervisorType = "mock" ) +const ( + defaultVCPUs = 1 + // 2 GiB + defaultMemSzMiB = 2048 +) + // deviceType describes a virtualized device type. type deviceType int @@ -128,6 +134,14 @@ type HypervisorConfig struct { // Debug changes the default hypervisor and kernel parameters to // enable debug output where available. Debug bool + + // DefaultVCPUs specifies default number of vCPUs for the VM. + // Pod configuration VMConfig.VCPUs overwrites this. + DefaultVCPUs uint32 + + // DefaultMem specifies default memory size in MiB for the VM. + // Pod configuration VMConfig.Memory overwrites this. + DefaultMemSz uint32 } func (conf *HypervisorConfig) valid() (bool, error) { @@ -139,6 +153,14 @@ func (conf *HypervisorConfig) valid() (bool, error) { return false, fmt.Errorf("Missing image path") } + if conf.DefaultVCPUs == 0 { + conf.DefaultVCPUs = defaultVCPUs + } + + if conf.DefaultMemSz == 0 { + conf.DefaultMemSz = defaultMemSzMiB + } + return true, nil } diff --git a/vendor/github.com/containers/virtcontainers/hypervisor_test.go b/vendor/github.com/containers/virtcontainers/hypervisor_test.go index 3534325b..7a8400e0 100644 --- a/vendor/github.com/containers/virtcontainers/hypervisor_test.go +++ b/vendor/github.com/containers/virtcontainers/hypervisor_test.go @@ -162,6 +162,26 @@ func TestHypervisorConfigIsValid(t *testing.T) { testHypervisorConfigValid(t, hypervisorConfig, true) } +func TestHypervisorConfigDefaults(t *testing.T) { + hypervisorConfig := &HypervisorConfig{ + KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel), + ImagePath: fmt.Sprintf("%s/%s", testDir, testImage), + HypervisorPath: "", + } + testHypervisorConfigValid(t, hypervisorConfig, true) + + hypervisorConfigDefaultsExpected := &HypervisorConfig{ + KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel), + ImagePath: fmt.Sprintf("%s/%s", testDir, testImage), + HypervisorPath: "", + DefaultVCPUs: defaultVCPUs, + DefaultMemSz: defaultMemSzMiB, + } + if reflect.DeepEqual(hypervisorConfig, hypervisorConfigDefaultsExpected) == false { + t.Fatal() + } +} + func TestAppendParams(t *testing.T) { paramList := []Param{ { diff --git a/vendor/github.com/containers/virtcontainers/qemu.go b/vendor/github.com/containers/virtcontainers/qemu.go index db31e0fe..0ce81066 100644 --- a/vendor/github.com/containers/virtcontainers/qemu.go +++ b/vendor/github.com/containers/virtcontainers/qemu.go @@ -22,7 +22,6 @@ import ( "os" "path/filepath" "regexp" - "runtime" "strings" "sync" "time" @@ -88,8 +87,6 @@ const ( ) const ( - defaultMemSize = "2G" - defaultMemMax = "3G" defaultMemSlots uint8 = 2 ) @@ -448,14 +445,14 @@ func (q *qemu) qmpMonitor(connectedCh chan struct{}) { } func (q *qemu) setCPUResources(podConfig PodConfig) ciaoQemu.SMP { - vcpus := uint(runtime.NumCPU()) + vcpus := q.config.DefaultVCPUs if podConfig.VMConfig.VCPUs > 0 { - vcpus = podConfig.VMConfig.VCPUs + vcpus = uint32(podConfig.VMConfig.VCPUs) } smp := ciaoQemu.SMP{ - CPUs: uint32(vcpus), - Cores: uint32(vcpus), + CPUs: vcpus, + Cores: vcpus, Sockets: defaultSockets, Threads: defaultThreads, } @@ -464,8 +461,8 @@ func (q *qemu) setCPUResources(podConfig PodConfig) ciaoQemu.SMP { } func (q *qemu) setMemoryResources(podConfig PodConfig) ciaoQemu.Memory { - mem := defaultMemSize - memMax := defaultMemMax + mem := fmt.Sprintf("%dM", q.config.DefaultMemSz) + memMax := fmt.Sprintf("%dM", int(float64(q.config.DefaultMemSz)*1.5)) if podConfig.VMConfig.Memory > 0 { mem = fmt.Sprintf("%dM", podConfig.VMConfig.Memory) intMemMax := int(float64(podConfig.VMConfig.Memory) * 1.5) diff --git a/vendor/github.com/containers/virtcontainers/qemu_test.go b/vendor/github.com/containers/virtcontainers/qemu_test.go index 61d518a5..fae075cb 100644 --- a/vendor/github.com/containers/virtcontainers/qemu_test.go +++ b/vendor/github.com/containers/virtcontainers/qemu_test.go @@ -32,6 +32,8 @@ func newQemuConfig() HypervisorConfig { KernelPath: testQemuKernelPath, ImagePath: testQemuImagePath, HypervisorPath: testQemuPath, + DefaultVCPUs: defaultVCPUs, + DefaultMemSz: defaultMemSzMiB, } } From d14ce992928ea557f7677b542eedd02dcf49493c Mon Sep 17 00:00:00 2001 From: Dmitry Voytik Date: Mon, 3 Jul 2017 16:54:52 +0200 Subject: [PATCH 2/2] config: Add default_{vcpus, memory} configurations 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 --- Makefile | 12 ++++++++++++ config.go | 35 ++++++++++++++++++++++++++++++++--- config/configuration.toml.in | 8 ++++++++ config_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 808b9fbd..1558555f 100644 --- a/Makefile +++ b/Makefile @@ -92,6 +92,11 @@ PAUSEBINRELPATH := bin/pause GLOBALLOGPATH := $(PKGLIBDIR)/runtime/runtime.log +# Default number of vCPUs +DEFVCPUS := 1 +# Default memory size in MiB +DEFMEMSZ := 2048 + SED = sed SOURCES := $(shell find . 2>&1 | grep -E '.*\.(c|h|go)$$') @@ -139,6 +144,8 @@ USER_VARS += SHAREDIR USER_VARS += SHIMPATH USER_VARS += SYSCONFDIR USER_VARS += PAUSEDESTDIR +USER_VARS += DEFVCPUS +USER_VARS += DEFMEMSZ V = @ @@ -178,6 +185,9 @@ const defaultRuntimeRun = "$(PKGRUNDIR)" const defaultShimPath = "$(SHIMPATH)" const pauseBinRelativePath = "$(PAUSEBINRELPATH)" +const defaultVCPUCount uint32 = $(DEFVCPUS) +const defaultMemSize uint32 = $(DEFMEMSZ) // MiB + // Required to be modifiable (for the tests) var defaultRuntimeConfiguration = "$(DESTCONFIG)" var defaultProxyPath = "$(PROXYPATH)" @@ -223,6 +233,8 @@ $(CONFIG): $(CONFIG_IN) -e "s|@QEMUPATH@|$(QEMUPATH)|g" \ -e "s|@SHIMPATH@|$(SHIMPATH)|g" \ -e "s|@GLOBALLOGPATH@|$(GLOBALLOGPATH)|g" \ + -e "s|@DEFVCPUS@|$(DEFVCPUS)|g" \ + -e "s|@DEFMEMSZ@|$(DEFMEMSZ)|g" \ $< > $@ generate-config: $(CONFIG) diff --git a/config.go b/config.go index 938f410a..6dac4d51 100644 --- a/config.go +++ b/config.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "os" "path/filepath" + goruntime "runtime" "github.com/BurntSushi/toml" vc "github.com/containers/virtcontainers" @@ -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 { @@ -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 defaultVCPUCount + } + if h.DefaultVCPUs > 255 { // qemu supports max 255 + return 255 + } + + return uint32(h.DefaultVCPUs) +} + +func (h hypervisor) defaultMemSz() uint32 { + if h.DefaultMemSz < 8 { + return defaultMemSize // MiB + } + + return h.DefaultMemSz +} + func (p proxy) url() string { if p.URL == "" { return defaultProxyURL @@ -158,6 +183,8 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { HypervisorPath: hypervisor, KernelPath: kernel, ImagePath: image, + DefaultVCPUs: h.defaultVCPUs(), + DefaultMemSz: h.defaultMemSz(), }, nil } @@ -264,6 +291,8 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat HypervisorPath: defaultHypervisorPath, KernelPath: defaultKernelPath, ImagePath: defaultImagePath, + DefaultVCPUs: defaultVCPUCount, + DefaultMemSz: defaultMemSize, } defaultAgentConfig := vc.HyperConfig{ diff --git a/config/configuration.toml.in b/config/configuration.toml.in index 6d9f2e39..187139cd 100644 --- a/config/configuration.toml.in +++ b/config/configuration.toml.in @@ -4,6 +4,14 @@ path = "@QEMUPATH@" kernel = "@KERNELPATH@" image = "@IMAGEPATH@" +# Default number of vCPUs per POD/VM: +# unspecified or 0 --> will be set to @DEFVCPUS@ +# < 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 @DEFMEMSZ@ MiB. +#default_memory = @DEFMEMSZ@ [proxy.cc] url = "@PROXYURL@" diff --git a/config_test.go b/config_test.go index 9e65638e..1384e742 100644 --- a/config_test.go +++ b/config_test.go @@ -21,6 +21,8 @@ import ( "path" "path/filepath" "reflect" + goruntime "runtime" + "strconv" "strings" "syscall" "testing" @@ -49,6 +51,8 @@ func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath path = "` + hypervisorPath + `" kernel = "` + kernelPath + `" image = "` + imagePath + `" + default_vcpus = ` + strconv.FormatUint(uint64(defaultVCPUCount), 10) + ` + default_memory = ` + strconv.FormatUint(uint64(defaultMemSize), 10) + ` [proxy.cc] url = "` + proxyURL + `" @@ -131,6 +135,8 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf HypervisorPath: hypervisorPath, KernelPath: kernelPath, ImagePath: imagePath, + DefaultVCPUs: defaultVCPUCount, + DefaultMemSz: defaultMemSize, } agentConfig := vc.HyperConfig{ @@ -586,6 +592,8 @@ func TestMinimalRuntimeConfig(t *testing.T) { HypervisorPath: defaultHypervisorPath, KernelPath: defaultKernelPath, ImagePath: defaultImagePath, + DefaultVCPUs: defaultVCPUCount, + DefaultMemSz: defaultMemSize, } expectedAgentConfig := vc.HyperConfig{ @@ -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(), defaultVCPUCount, "default vCPU number is wrong") + assert.Equal(t, h.defaultMemSz(), defaultMemSize, "default memory size is wrong") path := "/foo" h.Path = path @@ -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) {