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

Feat: Add CPU model and topology support #1131

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
62 changes: 62 additions & 0 deletions libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const (
domainStateConfLeaseDone = resourceStateConfDone
)

const (
hostPassthroughCPUMode = "host-passthrough"
customCPUMode = "custom"
noneCPUMode = "none"
)

func domainLeaseStateRefreshFunc(_ context.Context,
virConn *libvirt.Libvirt, domain libvirt.Domain,
waitForLeases []*libvirtxml.DomainInterface, rd *schema.ResourceData) retry.StateRefreshFunc {
Expand Down Expand Up @@ -236,6 +242,62 @@ func newDiskForCloudInit(virConn *libvirt.Libvirt, volumeKey string) (libvirtxml
return disk, nil
}

func setCPU(d *schema.ResourceData, domainDef *libvirtxml.Domain) error {
if cpuMode, ok := d.GetOk("cpu.0.mode"); ok {
mode := cpuMode.(string)
if mode != hostPassthroughCPUMode && mode != customCPUMode && mode != noneCPUMode {
return fmt.Errorf("invalid CPU mode: %s.Must be one of 'host-passthrough', 'custom', 'none'", mode)
}

var model *libvirtxml.DomainCPUModel
if modelConfig, ok := d.GetOk("cpu.0.model"); ok {
if models, ok := modelConfig.([]interface{}); ok && len(models) > 0 {
if mode != customCPUMode {
return fmt.Errorf("CPU model can only be defined when the CPU mode is set to 'custom'")
}
cpuModel := models[0].(map[string]interface{})

model = &libvirtxml.DomainCPUModel{
Fallback: cpuModel["fallback"].(string),
Value: cpuModel["value"].(string),
VendorID: cpuModel["vendor_id"].(string),
}
}
}

var topology *libvirtxml.DomainCPUTopology
if topologyConfig, ok := d.GetOk("cpu.0.topology"); ok {
if topologies, ok := topologyConfig.([]interface{}); ok && len(topologies) > 0 {
cpuTopology := topologies[0].(map[string]interface{})

sockets := cpuTopology["sockets"].(int)
cores := cpuTopology["cores"].(int)
threads := cpuTopology["threads"].(int)

if (sockets * cores * threads) > int(domainDef.VCPU.Value) {
return fmt.Errorf(
"(%d sockets * %d cores * %d threads) is more than the vCPU count (%d)",
sockets, cores, threads, domainDef.VCPU.Value,
)
}

topology = &libvirtxml.DomainCPUTopology{
Sockets: sockets,
Cores: cores,
Threads: threads,
}
}
}

domainDef.CPU = &libvirtxml.DomainCPU{
Mode: mode,
Model: model,
Topology: topology,
}
}
return nil
}

func setCoreOSIgnition(d *schema.ResourceData, domainDef *libvirtxml.Domain, arch string) error {
if ignition, ok := d.GetOk("coreos_ignition"); ok {
ignitionKey, err := getIgnitionVolumeKeyFromTerraformID(ignition.(string))
Expand Down
93 changes: 80 additions & 13 deletions libvirt/resource_libvirt_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,57 @@ func resourceLibvirtDomain() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"model": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fallback": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vendor_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
"topology": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sockets": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"cores": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"threads": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
},
},
},
},
Expand Down Expand Up @@ -494,24 +545,25 @@ func resourceLibvirtDomainCreate(ctx context.Context, d *schema.ResourceData, me
domainDef.Name = name.(string)
}

if cpuMode, ok := d.GetOk("cpu.0.mode"); ok {
domainDef.CPU = &libvirtxml.DomainCPU{
Mode: cpuMode.(string),
}
domainDef.VCPU = &libvirtxml.DomainVCPU{
Value: uint(d.Get("vcpu").(int)),
}

if err := setCPU(d, &domainDef); err != nil {
return diag.FromErr(err)
}

domainDef.Memory = &libvirtxml.DomainMemory{
Value: uint(d.Get("memory").(int)),
Unit: "MiB",
}
domainDef.VCPU = &libvirtxml.DomainVCPU{
Value: uint(d.Get("vcpu").(int)),
}

domainDef.Description = d.Get("description").(string)

domainDef.OS.Kernel = d.Get("kernel").(string)
domainDef.OS.Initrd = d.Get("initrd").(string)
domainDef.OS.Type.Arch = d.Get("arch").(string)
domainDef.OS.Type.Machine = d.Get("machine").(string)

domainDef.Devices.Emulator = d.Get("emulator").(string)

Expand Down Expand Up @@ -837,15 +889,30 @@ func resourceLibvirtDomainRead(ctx context.Context, d *schema.ResourceData, meta
}

if domainDef.CPU != nil {
cpu := make(map[string]interface{})
var cpus []map[string]interface{}
cpu := map[string]interface{}{}

if domainDef.CPU.Mode != "" {
cpu["mode"] = domainDef.CPU.Mode

if domainDef.CPU.Model != nil {
model := map[string]interface{}{}
model["fallback"] = domainDef.CPU.Model.Fallback
model["value"] = domainDef.CPU.Model.Value
model["vendor_id"] = domainDef.CPU.Model.VendorID

cpu["model"] = []map[string]interface{}{model}
}

if domainDef.CPU.Topology != nil {
topology := map[string]interface{}{}
topology["sockets"] = domainDef.CPU.Topology.Sockets
topology["cores"] = domainDef.CPU.Topology.Cores
topology["threads"] = domainDef.CPU.Topology.Threads

cpu["topology"] = []map[string]interface{}{topology}
}
}
if len(cpu) > 0 {
cpus = append(cpus, cpu)
d.Set("cpu", cpus)
}
d.Set("cpu", []map[string]interface{}{cpu})
}

d.Set("arch", domainDef.OS.Type.Arch)
Expand Down
104 changes: 94 additions & 10 deletions libvirt/resource_libvirt_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"testing"

testhelper "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/test"
Expand Down Expand Up @@ -850,27 +851,110 @@ func TestAccLibvirtDomain_Cpu(t *testing.T) {
var domain libvirt.Domain
randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

config := fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
cpu {
mode = "custom"
}
}`, randomDomainName, randomDomainName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtDomainDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
vcpu = 2
cpu {
mode = "host-passthrough"
}
}`, randomDomainName, randomDomainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.mode", "custom"),
"libvirt_domain."+randomDomainName, "cpu.0.mode", hostPassthroughCPUMode),
),
},
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
vcpu = 2
cpu {
mode = "host-passthrough"
topology {
sockets = 1
cores = 1
threads = 2
}
}
}`, randomDomainName, randomDomainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.mode", hostPassthroughCPUMode),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.topology.0.sockets", "1"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.topology.0.cores", "1"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.topology.0.threads", "2"),
),
},
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
vcpu = 2
cpu {
mode = "host-passthrough"
topology {
sockets = 1
cores = 2
threads = 2
}
}
}`, randomDomainName, randomDomainName),
ExpectError: regexp.MustCompile(`\(1 sockets \* 2 cores \* 2 threads\) is more than the vCPU count \(2\)`),
},
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
vcpu = 2
cpu {
mode = "custom"
model {
fallback = "forbid"
value = "EPYC"
vendor_id = "AuthenticAMD"
}
}
}`, randomDomainName, randomDomainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.mode", customCPUMode),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.model.0.value", "EPYC"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.model.0.vendor_id", "AuthenticAMD"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "cpu.0.model.0.fallback", "forbid"),
),
},
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
vcpu = 2
cpu {
mode = "host-passthrough"
model {
fallback = "forbid"
value = "EPYC"
vendor_id = "AuthenticAMD"
}
}
}`, randomDomainName, randomDomainName),
ExpectError: regexp.MustCompile("CPU model can only be defined when the CPU mode is set to 'custom'"),
},
},
})
}
Expand Down