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

Commit

Permalink
virtcontainers: remove sandboxConfig.VMConfig
Browse files Browse the repository at this point in the history
We can just use hyprvisor config to specify the memory size
of a guest. There is no need to maintain the extra place just
for memory size.

Fixes: #692

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Sep 6, 2018
1 parent 56ba8ad commit ce28865
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 100 deletions.
2 changes: 0 additions & 2 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ func updateRuntimeConfig(configPath string, tomlConf tomlConfig, config *oci.Run
return fmt.Errorf("%v: %v", configPath, err)
}

config.VMConfig.Memory = uint(hConfig.DefaultMemSz)

config.HypervisorConfig = hConfig
}
}
Expand Down
12 changes: 3 additions & 9 deletions cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf

ShimType: defaultShim,
ShimConfig: shimConfig,

VMConfig: vc.Resources{
Memory: uint(defaultMemSize),
},
}

config = testRuntimeConfig{
Expand Down Expand Up @@ -1196,12 +1192,10 @@ func TestUpdateRuntimeConfigurationVMConfig(t *testing.T) {
assert := assert.New(t)

vcpus := uint(2)
mem := uint(2048)
mem := uint32(2048)

config := oci.RuntimeConfig{}
expectedVMConfig := vc.Resources{
Memory: mem,
}
expectedVMConfig := mem

tomlConf := tomlConfig{
Hypervisor: map[string]hypervisor{
Expand All @@ -1219,7 +1213,7 @@ func TestUpdateRuntimeConfigurationVMConfig(t *testing.T) {
err := updateRuntimeConfig("", tomlConf, &config)
assert.NoError(err)

assert.Equal(expectedVMConfig, config.VMConfig)
assert.Equal(expectedVMConfig, config.HypervisorConfig.DefaultMemSz)
}

func TestUpdateRuntimeConfigurationFactoryConfig(t *testing.T) {
Expand Down
8 changes: 1 addition & 7 deletions virtcontainers/example_pod_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,17 @@ func Example_createAndStartSandbox() {
KernelPath: "/usr/share/kata-containers/vmlinux.container",
ImagePath: "/usr/share/kata-containers/kata-containers.img",
HypervisorPath: "/usr/bin/qemu-system-x86_64",
DefaultMemSz: 1024,
}

// Use hyperstart default values for the agent.
agConfig := vc.HyperConfig{}

// VM resources
vmConfig := vc.Resources{
Memory: 1024,
}

// The sandbox configuration:
// - One container
// - Hypervisor is QEMU
// - Agent is hyperstart
sandboxConfig := vc.SandboxConfig{
VMConfig: vmConfig,

HypervisorType: vc.QemuHypervisor,
HypervisorConfig: hypervisorConfig,

Expand Down
8 changes: 2 additions & 6 deletions virtcontainers/hack/virtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func buildSandboxConfig(context *cli.Context) (vc.SandboxConfig, error) {
KernelPath: kernelPath,
ImagePath: "/usr/share/clear-containers/clear-containers.img",
HypervisorMachineType: machineType,
DefaultMemSz: uint32(vmMemory),
}

if err := buildKernelParams(&hypervisorConfig); err != nil {
Expand All @@ -195,19 +196,14 @@ func buildSandboxConfig(context *cli.Context) (vc.SandboxConfig, error) {

shimConfig := getShimConfig(*shimType, shimPath)

vmConfig := vc.Resources{
Memory: vmMemory,
}

id := context.String("id")
if id == "" {
// auto-generate sandbox name
id = uuid.Generate().String()
}

sandboxConfig := vc.SandboxConfig{
ID: id,
VMConfig: vmConfig,
ID: id,

HypervisorType: vc.QemuHypervisor,
HypervisorConfig: hypervisorConfig,
Expand Down
3 changes: 1 addition & 2 deletions virtcontainers/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ type HypervisorConfig struct {
DefaultMaxVCPUs uint32

// DefaultMem specifies default memory size in MiB for the VM.
// Sandbox configuration VMConfig.Memory overwrites this.
DefaultMemSz uint32

// DefaultBridges specifies default number of bridges for the VM.
Expand Down Expand Up @@ -546,7 +545,7 @@ func RunningOnVMM(cpuInfoPath string) (bool, error) {
// hypervisor is the virtcontainers hypervisor interface.
// The default hypervisor implementation is Qemu.
type hypervisor interface {
init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, vmConfig Resources, storage resourceStorage) error
init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, storage resourceStorage) error

createSandbox() error
startSandbox() error
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/mock_hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type mockHypervisor struct {
vCPUs uint32
}

func (m *mockHypervisor) init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, vmConfig Resources, storage resourceStorage) error {
func (m *mockHypervisor) init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, storage resourceStorage) error {
err := hypervisorConfig.valid()
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/mock_hypervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMockHypervisorInit(t *testing.T) {
ctx := context.Background()

// wrong config
if err := m.init(ctx, sandbox.config.ID, &sandbox.config.HypervisorConfig, sandbox.config.VMConfig, sandbox.storage); err == nil {
if err := m.init(ctx, sandbox.config.ID, &sandbox.config.HypervisorConfig, sandbox.storage); err == nil {
t.Fatal()
}

Expand All @@ -40,7 +40,7 @@ func TestMockHypervisorInit(t *testing.T) {
}

// right config
if err := m.init(ctx, sandbox.config.ID, &sandbox.config.HypervisorConfig, sandbox.config.VMConfig, sandbox.storage); err != nil {
if err := m.init(ctx, sandbox.config.ID, &sandbox.config.HypervisorConfig, sandbox.storage); err != nil {
t.Fatal(err)
}
}
Expand Down
18 changes: 6 additions & 12 deletions virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ type FactoryConfig struct {

// RuntimeConfig aggregates all runtime specific settings
type RuntimeConfig struct {
VMConfig vc.Resources

HypervisorType vc.HypervisorType
HypervisorConfig vc.HypervisorConfig

Expand Down Expand Up @@ -406,25 +404,23 @@ func (spec *CompatOCISpec) SandboxID() (string, error) {
return "", fmt.Errorf("Could not find sandbox ID")
}

func vmConfig(ocispec CompatOCISpec, config RuntimeConfig) (vc.Resources, error) {
resources := config.VMConfig

func updateVMConfig(ocispec CompatOCISpec, config *RuntimeConfig) error {
if ocispec.Linux == nil || ocispec.Linux.Resources == nil {
return resources, nil
return nil
}

if ocispec.Linux.Resources.Memory != nil &&
ocispec.Linux.Resources.Memory.Limit != nil {
memBytes := *ocispec.Linux.Resources.Memory.Limit
if memBytes <= 0 {
return vc.Resources{}, fmt.Errorf("Invalid OCI memory limit %d", memBytes)
return fmt.Errorf("Invalid OCI memory limit %d", memBytes)
}
// Use some math magic to round up to the nearest Mb.
// This has the side effect that we can never have <1Mb assigned.
resources.Memory = uint((memBytes + (1024*1024 - 1)) / (1024 * 1024))
config.HypervisorConfig.DefaultMemSz = uint32((memBytes + (1024*1024 - 1)) / (1024 * 1024))
}

return resources, nil
return nil
}

func addAssetAnnotations(ocispec CompatOCISpec, config *vc.SandboxConfig) {
Expand Down Expand Up @@ -465,7 +461,7 @@ func SandboxConfig(ocispec CompatOCISpec, runtime RuntimeConfig, bundlePath, cid
return vc.SandboxConfig{}, err
}

resources, err := vmConfig(ocispec, runtime)
err = updateVMConfig(ocispec, &runtime)
if err != nil {
return vc.SandboxConfig{}, err
}
Expand All @@ -480,8 +476,6 @@ func SandboxConfig(ocispec CompatOCISpec, runtime RuntimeConfig, bundlePath, cid

Hostname: ocispec.Hostname,

VMConfig: resources,

HypervisorType: runtime.HypervisorType,
HypervisorConfig: runtime.HypervisorConfig,

Expand Down
51 changes: 15 additions & 36 deletions virtcontainers/pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,17 @@ func TestMinimalSandboxConfig(t *testing.T) {
}
}

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

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

expectedResources := vc.Resources{
Memory: 128,
}
expectedMem := uint32(128)

ocispec := CompatOCISpec{
Spec: specs.Spec{
Expand All @@ -277,48 +276,28 @@ func TestVmConfig(t *testing.T) {
},
}

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)
}
err := updateVMConfig(ocispec, &config)
assert.Nil(err)
assert.Equal(config.HypervisorConfig.DefaultMemSz, expectedMem)

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)
}
err = updateVMConfig(ocispec, &config)
assert.NotNil(err)

// Test case when Memory is nil
ocispec.Spec.Linux.Resources.Memory = nil
expectedResources.Memory = config.VMConfig.Memory
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)
}
err = updateVMConfig(ocispec, &config)
assert.Nil(err)

// Test case when CPU is nil
ocispec.Spec.Linux.Resources.CPU = nil
limitBytes = 20
ocispec.Linux.Resources.Memory = &specs.LinuxMemory{Limit: &limitBytes}
expectedResources.Memory = 1
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)
}
err = updateVMConfig(ocispec, &config)
assert.Nil(err)
assert.NotEqual(config.HypervisorConfig.DefaultMemSz, expectedMem)
}

func testStatusToOCIStateSuccessful(t *testing.T, cStatus vc.ContainerStatus, expected specs.State) {
Expand Down
14 changes: 2 additions & 12 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ type QemuState struct {
type qemu struct {
id string

vmConfig Resources

storage resourceStorage

config HypervisorConfig
Expand Down Expand Up @@ -197,7 +195,7 @@ func (q *qemu) trace(name string) (opentracing.Span, context.Context) {
}

// init intializes the Qemu structure.
func (q *qemu) init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, vmConfig Resources, storage resourceStorage) error {
func (q *qemu) init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, storage resourceStorage) error {
// save
q.ctx = ctx

Expand All @@ -211,7 +209,6 @@ func (q *qemu) init(ctx context.Context, id string, hypervisorConfig *Hypervisor

q.id = id
q.storage = storage
q.vmConfig = vmConfig
q.config = *hypervisorConfig
q.arch = newQemuArch(q.config)

Expand Down Expand Up @@ -273,9 +270,6 @@ func (q *qemu) memoryTopology() (govmmQemu.Memory, error) {
}

memMb := uint64(q.config.DefaultMemSz)
if q.vmConfig.Memory > 0 {
memMb = uint64(q.vmConfig.Memory)
}

return q.arch.memoryTopology(memMb, hostMemMb), nil
}
Expand Down Expand Up @@ -1039,11 +1033,7 @@ func (q *qemu) hotplugMemory(memDev *memoryDevice, op operation) error {
}

// calculate current memory
currentMemory := int(q.config.DefaultMemSz)
if q.vmConfig.Memory > 0 {
currentMemory = int(q.vmConfig.Memory)
}
currentMemory += q.state.HotpluggedMemory
currentMemory := int(q.config.DefaultMemSz) + q.state.HotpluggedMemory

// Don't exceed the maximum amount of memory
if currentMemory+memDev.sizeMB > int(maxMem) {
Expand Down
8 changes: 2 additions & 6 deletions virtcontainers/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestQemuInit(t *testing.T) {
t.Fatalf("Could not create parent directory %s: %v", parentDir, err)
}

if err := q.init(context.Background(), sandbox.id, &sandbox.config.HypervisorConfig, sandbox.config.VMConfig, sandbox.storage); err != nil {
if err := q.init(context.Background(), sandbox.id, &sandbox.config.HypervisorConfig, sandbox.storage); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -118,7 +118,7 @@ func TestQemuInitMissingParentDirFail(t *testing.T) {
t.Fatal(err)
}

if err := q.init(context.Background(), sandbox.id, &sandbox.config.HypervisorConfig, sandbox.config.VMConfig, sandbox.storage); err != nil {
if err := q.init(context.Background(), sandbox.id, &sandbox.config.HypervisorConfig, sandbox.storage); err != nil {
t.Fatalf("Qemu init() is not expected to fail because of missing parent directory for storage: %v", err)
}
}
Expand Down Expand Up @@ -168,10 +168,6 @@ func TestQemuMemoryTopology(t *testing.T) {
MaxMem: memMax,
}

q.vmConfig = Resources{
Memory: uint(mem),
}

memory, err := q.memoryTopology()
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 1 addition & 4 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@ type SandboxConfig struct {

Hostname string

// VMConfig is the VM configuration to set for this sandbox.
VMConfig Resources

HypervisorType HypervisorType
HypervisorConfig HypervisorConfig

Expand Down Expand Up @@ -830,7 +827,7 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor
}
}()

if err = s.hypervisor.init(ctx, s.id, &sandboxConfig.HypervisorConfig, sandboxConfig.VMConfig, s.storage); err != nil {
if err = s.hypervisor.init(ctx, s.id, &sandboxConfig.HypervisorConfig, s.storage); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) {
}
}()

if err = hypervisor.init(ctx, id, &config.HypervisorConfig, Resources{}, &filesystem{}); err != nil {
if err = hypervisor.init(ctx, id, &config.HypervisorConfig, &filesystem{}); err != nil {
return nil, err
}

Expand Down

0 comments on commit ce28865

Please sign in to comment.