Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #361: Memory handling #393

Merged
1 commit merged into from May 25, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/ovirt/go-ovirt-client v1.0.0-alpha7
github.com/ovirt/go-ovirt-client v1.0.0-alpha9
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0
github.com/posener/complete v1.2.3 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c h1:jXRFpl7+W0YZj/fg
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c/go.mod h1:Zkdj9/rW6eyuw0uOeEns6O3pP5G2ak+bI/tgkQ/tEZI=
github.com/ovirt/go-ovirt-client v1.0.0-alpha7 h1:LhQiiiPEzlcPVwzdw8K8VGPBmZWRtp4Kempxa/070m0=
github.com/ovirt/go-ovirt-client v1.0.0-alpha7/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8 h1:ATO+1zem2WLSFHKmNHBfCEZFH+GSSAZg8SFeI1GDhys=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220524160043-adf38ca8ec41 h1:pI/axwLyWaWWHQTHQVqODGKoAEx3qCPSmay83WB7J9A=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220524160043-adf38ca8ec41/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220525091503-4f83e7145820 h1:ExuHW/6Y89ZY7u3+Sm5SoLeJNGkpwCkrdJHeCYYnvK4=
github.com/ovirt/go-ovirt-client v1.0.0-alpha8.0.20220525091503-4f83e7145820/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client v1.0.0-alpha9 h1:HmO7pYokPBDswW56vwfSHepDd4Kyh5IY4RHtWT1Y+n8=
github.com/ovirt/go-ovirt-client v1.0.0-alpha9/go.mod h1:tv8E2pxUkggayDAgMLuQHzcNtzt8RFvnhO5V5b/5X4U=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0 h1:uvACVHYhYPMkNJrrgWiABcfELB6qoFfsDDUTbpb4Jv4=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0/go.mod h1:chKKxCv4lRjxezrTG+EIhkWXGhDAWByglPVXh/iYdnQ=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
70 changes: 70 additions & 0 deletions ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ var vmSchema = map[string]*schema.Schema{
Optional: true,
Description: "hostname that is set during initialization.",
},
"memory": {
Type: schema.TypeInt,
Optional: true,
Description: "Memory to assign to the VM in bytes.",
ValidateDiagFunc: validatePositiveInt,
RequiredWith: []string{"maximum_memory"},
},
"maximum_memory": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum memory to assign to the VM in the memory policy in bytes.",
ValidateDiagFunc: validatePositiveInt,
},
"memory_ballooning": {
Type: schema.TypeBool,
Optional: true,
Description: "Turn memory ballooning on or off for the VM.",
},
}

func vmAffinityValues() []string {
Expand Down Expand Up @@ -189,6 +207,8 @@ func (p *provider) vmCreate(
handleVMPlacementPolicy,
handleVMInitialization,
handleTemplateDiskAttachmentOverride,
handleVMMemory,
handleVMMemoryPolicy,
} {
diags = f(data, params, diags)
}
Expand Down Expand Up @@ -254,6 +274,56 @@ func handleTemplateDiskAttachmentOverride(
return diags
}

func handleVMMemoryPolicy(
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
diags diag.Diagnostics,
) diag.Diagnostics {
addMemoryPolicy := false
memoryPolicy := ovirtclient.NewMemoryPolicyParameters()
maxMemory, ok := data.GetOk("maximum_memory")
if ok {
var err error
_, err = memoryPolicy.WithMax(int64(maxMemory.(int)))
if err != nil {
diags = append(diags, errorToDiag("add maximum memory", err))
} else {
addMemoryPolicy = true
}
}
ballooning, ok := data.GetOk("memory_ballooning")
if ok {
var err error
_, err = memoryPolicy.WithBallooning(ballooning.(bool))
if err != nil {
diags = append(diags, errorToDiag("add ballooning", err))
} else {
addMemoryPolicy = true
}
}
if addMemoryPolicy {
params.WithMemoryPolicy(memoryPolicy)
}
return diags
}

func handleVMMemory(
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
diags diag.Diagnostics,
) diag.Diagnostics {
memory, ok := data.GetOk("memory")
if !ok {
return diags
}
var err error
_, err = params.WithMemory(int64(memory.(int)))
if err != nil {
diags = append(diags, errorToDiag("set memory", err))
}
return diags
}

func handleVMPlacementPolicy(
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
Expand Down
66 changes: 65 additions & 1 deletion ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (t *testVM) Memory() int64 {
panic("not implemented for test input")
}

func (t *testVM) MemoryPolicy() (ovirtclient.MemoryPolicy, bool) {
func (t *testVM) MemoryPolicy() ovirtclient.MemoryPolicy {
panic("not implemented for test input")
}

Expand Down Expand Up @@ -662,3 +662,67 @@ resource "ovirt_vm" "one" {
},
)
}

func TestMemory(t *testing.T) {
t.Parallel()

p := newProvider(newTestLogger(t))
testHelper := p.getTestHelper()
clusterID := testHelper.GetClusterID()
config := fmt.Sprintf(
`
provider "ovirt" {
mock = true
}

data "ovirt_blank_template" "blank" {
}

resource "ovirt_vm" "test" {
template_id = data.ovirt_blank_template.blank.id
cluster_id = "%s"
name = "%s"
memory = 1048576
maximum_memory = 2097152
memory_ballooning = false
}
`,
clusterID,
p.getTestHelper().GenerateTestResourceName(t),
)

resource.UnitTest(
t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: func(state *terraform.State) error {
client := testHelper.GetClient()
vmID := state.RootModule().Resources["ovirt_vm.test"].Primary.ID
vm, err := client.GetVM(ovirtclient.VMID(vmID))
if err != nil {
return err
}
if vm.Memory() != 1048576 {
return fmt.Errorf("incorrect amount of memory: %d", vm.Memory())
}
memoryPolicy := vm.MemoryPolicy()
if memoryPolicy.Max() == nil {
return fmt.Errorf("no maximum memory set on VM")
}
if *memoryPolicy.Max() != 2097152 {
return fmt.Errorf("incorrect maximum memory set on VM: %d", *memoryPolicy.Max())
}

return nil
},
},
{
Config: config,
Destroy: true,
},
},
},
)
}