Repository: firecracker-microvm/firecracker-containerd
Pull request #372 (2019-12-24 09:22:47)

Made vcpuCount configuration same as memSizeMb
Signed-off-by: Boris Popovschi <zyqsempai@mail.ru>

Issue #359 

*Description of changes:*
Made vcpuCount configuration same as memSizeMb:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


Zyqsempai: @xibz @nmeyerhans  PTAL
xibz: @Zyqsempai - Thank you for the PR. Much appreciated! This looks good to me, with one minor comment. In the `config.json.example`, `cpu_count` is still present. Let's remove that. Otherwise this looks good.
Zyqsempai: @xibz Hi, sorry, currently I am on vacation, without laptop, will be back on 4th January and will fix it. Thanks.

Patch:
diff --git a/config/config.go b/config/config.go
index b5bc667..d16b5e6 100644
--- a/config/config.go
+++ b/config/config.go
@@ -31,7 +31,6 @@ const (
 	defaultFilesPath   = "/var/lib/firecracker-containerd/runtime/"
 	defaultKernelPath  = defaultFilesPath + "default-vmlinux.bin"
 	defaultRootfsPath  = defaultFilesPath + "default-rootfs.img"
-	defaultCPUCount    = 1
 	defaultCPUTemplate = models.CPUTemplateT2
 	defaultShimBaseDir = "/var/lib/firecracker-containerd/shim-base"
 )
@@ -42,7 +41,6 @@ type Config struct {
 	KernelImagePath       string `json:"kernel_image_path"`
 	KernelArgs            string `json:"kernel_args"`
 	RootDrive             string `json:"root_drive"`
-	CPUCount              int    `json:"cpu_count"`
 	CPUTemplate           string `json:"cpu_template"`
 	LogLevel              string `json:"log_level"`
 	HtEnabled             bool   `json:"ht_enabled"`
@@ -83,7 +81,6 @@ func LoadConfig(path string) (*Config, error) {
 		KernelArgs:      defaultKernelArgs,
 		KernelImagePath: defaultKernelPath,
 		RootDrive:       defaultRootfsPath,
-		CPUCount:        defaultCPUCount,
 		CPUTemplate:     string(defaultCPUTemplate),
 		ShimBaseDir:     defaultShimBaseDir,
 	}
diff --git a/config/config.json.example b/config/config.json.example
index 1395ad3..fbee8e6 100644
--- a/config/config.json.example
+++ b/config/config.json.example
@@ -3,7 +3,6 @@
   "kernel_image_path": "vmlinux",
   "kernel_args": "console=ttyS0 noapic reboot=k panic=1 pci=off nomodules rw",
   "root_drive": "./vsock.img",
-  "cpu_count": 1,
   "cpu_template": "T2",
   "log_level": "Debug",
   "ht_enabled": false
diff --git a/runtime/helpers.go b/runtime/helpers.go
index fbae9bc..a8a61da 100644
--- a/runtime/helpers.go
+++ b/runtime/helpers.go
@@ -28,12 +28,13 @@ import (
 
 const (
 	defaultMemSizeMb = 128
+	defaultCPUCount  = 1
 )
 
 func machineConfigurationFromProto(cfg *config.Config, req *proto.FirecrackerMachineConfiguration) models.MachineConfiguration {
 	config := models.MachineConfiguration{
 		CPUTemplate: models.CPUTemplate(cfg.CPUTemplate),
-		VcpuCount:   firecracker.Int64(int64(cfg.CPUCount)),
+		VcpuCount:   firecracker.Int64(defaultCPUCount),
 		MemSizeMib:  firecracker.Int64(defaultMemSizeMb),
 		HtEnabled:   firecracker.Bool(cfg.HtEnabled),
 	}


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index bf577a1..ceeb4e7 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -32,7 +32,6 @@ func TestLoadConfigDefaults(t *testing.T) {
 	assert.Equal(t, defaultKernelArgs, cfg.KernelArgs, "expected default kernel args")
 	assert.Equal(t, defaultKernelPath, cfg.KernelImagePath, "expected default kernel path")
 	assert.Equal(t, defaultRootfsPath, cfg.RootDrive, "expected default rootfs path")
-	assert.Equal(t, defaultCPUCount, cfg.CPUCount, "expected default CPU count")
 	assert.Equal(t, string(defaultCPUTemplate), cfg.CPUTemplate, "expected default CPU template")
 }
 
@@ -58,7 +57,6 @@ func TestLoadConfigOverrides(t *testing.T) {
 	assert.Equal(t, overrideKernelArgs, cfg.KernelArgs, "expected overridden kernel args")
 	assert.Equal(t, overrideKernelPath, cfg.KernelImagePath, "expected overridden kernel path")
 	assert.Equal(t, overrideRootfsPath, cfg.RootDrive, "expected overridden rootfs path")
-	assert.Equal(t, overrideCPUCount, cfg.CPUCount, "expected overridden CPU count")
 	assert.Equal(t, overrideCPUTemplate, cfg.CPUTemplate, "expected overridden CPU template")
 }
 
diff --git a/runtime/helpers_test.go b/runtime/helpers_test.go
index 73b4617..a726dde 100644
--- a/runtime/helpers_test.go
+++ b/runtime/helpers_test.go
@@ -60,12 +60,11 @@ func TestMachineConfigurationFromProto(t *testing.T) {
 			name: "ConfigOnly",
 			config: &config.Config{
 				CPUTemplate: "C3",
-				CPUCount:    vcpuCount,
 			},
 			proto: &proto.FirecrackerMachineConfiguration{},
 			expectedMachineConfig: models.MachineConfiguration{
 				CPUTemplate: models.CPUTemplateC3,
-				VcpuCount:   firecracker.Int64(vcpuCount),
+				VcpuCount:   firecracker.Int64(defaultCPUCount),
 				MemSizeMib:  firecracker.Int64(defaultMemSizeMb),
 				HtEnabled:   firecracker.Bool(false),
 			},
@@ -74,11 +73,10 @@ func TestMachineConfigurationFromProto(t *testing.T) {
 			name: "NilProto",
 			config: &config.Config{
 				CPUTemplate: "C3",
-				CPUCount:    vcpuCount,
 			},
 			expectedMachineConfig: models.MachineConfiguration{
 				CPUTemplate: models.CPUTemplateC3,
-				VcpuCount:   firecracker.Int64(vcpuCount),
+				VcpuCount:   firecracker.Int64(defaultCPUCount),
 				MemSizeMib:  firecracker.Int64(defaultMemSizeMb),
 				HtEnabled:   firecracker.Bool(false),
 			},
@@ -87,7 +85,6 @@ func TestMachineConfigurationFromProto(t *testing.T) {
 			name: "Overrides",
 			config: &config.Config{
 				CPUTemplate: "T2",
-				CPUCount:    vcpuCount + 1,
 			},
 			proto: &proto.FirecrackerMachineConfiguration{
 				CPUTemplate: string(models.CPUTemplateC3),
diff --git a/runtime/integ_test.go b/runtime/integ_test.go
index c09081c..b773098 100644
--- a/runtime/integ_test.go
+++ b/runtime/integ_test.go
@@ -36,7 +36,6 @@ var defaultRuntimeConfig = config.Config{
 	KernelImagePath:       "/var/lib/firecracker-containerd/runtime/default-vmlinux.bin",
 	KernelArgs:            "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules systemd.journald.forward_to_console systemd.log_color=false systemd.unit=firecracker.target init=/sbin/overlay-init",
 	RootDrive:             "/var/lib/firecracker-containerd/runtime/default-rootfs.img",
-	CPUCount:              1,
 	CPUTemplate:           "T2",
 	LogLevel:              "Debug",
 	Debug:                 true,
diff --git a/runtime/service_test.go b/runtime/service_test.go
index a322526..dfa71e8 100644
--- a/runtime/service_test.go
+++ b/runtime/service_test.go
@@ -54,7 +54,6 @@ func TestBuildVMConfiguration(t *testing.T) {
 				KernelImagePath: "KERNEL IMAGE",
 				RootDrive:       "ROOT DRIVE",
 				CPUTemplate:     "C3",
-				CPUCount:        2,
 			},
 			expectedCfg: &firecracker.Config{
 				KernelArgs:      "KERNEL ARGS",
@@ -69,7 +68,7 @@ func TestBuildVMConfiguration(t *testing.T) {
 				},
 				MachineCfg: models.MachineConfiguration{
 					CPUTemplate: models.CPUTemplateC3,
-					VcpuCount:   firecracker.Int64(2),
+					VcpuCount:   firecracker.Int64(defaultCPUCount),
 					MemSizeMib:  firecracker.Int64(defaultMemSizeMb),
 					HtEnabled:   firecracker.Bool(false),
 				},
@@ -129,7 +128,6 @@ func TestBuildVMConfiguration(t *testing.T) {
 				KernelArgs:      "KERNEL ARGS",
 				KernelImagePath: "KERNEL IMAGE",
 				CPUTemplate:     "C3",
-				CPUCount:        2,
 			},
 			expectedCfg: &firecracker.Config{
 				KernelArgs:      "REQUEST KERNEL ARGS",
@@ -165,7 +163,6 @@ func TestBuildVMConfiguration(t *testing.T) {
 				KernelArgs:      "KERNEL ARGS",
 				KernelImagePath: "KERNEL IMAGE",
 				CPUTemplate:     "C3",
-				CPUCount:        2,
 			},
 			expectedCfg: &firecracker.Config{
 				KernelArgs:      "REQUEST KERNEL ARGS",
@@ -180,7 +177,7 @@ func TestBuildVMConfiguration(t *testing.T) {
 				},
 				MachineCfg: models.MachineConfiguration{
 					CPUTemplate: models.CPUTemplateC3,
-					VcpuCount:   firecracker.Int64(2),
+					VcpuCount:   firecracker.Int64(defaultCPUCount),
 					MemSizeMib:  firecracker.Int64(defaultMemSizeMb),
 					HtEnabled:   firecracker.Bool(false),
 				},
@@ -195,7 +192,6 @@ func TestBuildVMConfiguration(t *testing.T) {
 				KernelImagePath: "KERNEL IMAGE",
 				RootDrive:       "ROOT DRIVE",
 				CPUTemplate:     "C3",
-				CPUCount:        2,
 			},
 			expectedCfg: &firecracker.Config{
 				KernelArgs:      "KERNEL ARGS",
@@ -210,7 +206,7 @@ func TestBuildVMConfiguration(t *testing.T) {
 				},
 				MachineCfg: models.MachineConfiguration{
 					CPUTemplate: models.CPUTemplateC3,
-					VcpuCount:   firecracker.Int64(2),
+					VcpuCount:   firecracker.Int64(defaultCPUCount),
 					MemSizeMib:  firecracker.Int64(defaultMemSizeMb),
 					HtEnabled:   firecracker.Bool(false),
 				},