Skip to content

Commit

Permalink
Add isolate_emulator_thread option to vm resource
Browse files Browse the repository at this point in the history
Signed-off-by: Cooper Tseng <[email protected]>
  • Loading branch information
brandboat committed Oct 1, 2024
1 parent 4fdd56a commit 781ce8d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ func (c *Constructor) Setup() util.Processors {
return nil
},
},
{
Field: constants.FieldVirtualMachineIsolateEmulatorThread,
Parser: func(i interface{}) error {
vmBuilder.VirtualMachine.Spec.Template.Spec.Domain.CPU.IsolateEmulatorThread = i.(bool)
return nil
},
},
}
return append(processors, customProcessors...)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/virtualmachine/schema_virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ please use %s instead of this deprecated field:
Optional: true,
Default: false,
},
constants.FieldVirtualMachineIsolateEmulatorThread: {
Type: schema.TypeBool,
Description: "To enable isolate emulator thread, ensure that at least one node has the CPU manager enabled, also VM CPU pinning must be enabled. Note that enable option will allocate an additional dedicated CPU.",
Optional: true,
Default: false,
},
}
util.NamespacedSchemaWrap(s, false)
return s
Expand Down
44 changes: 38 additions & 6 deletions internal/tests/resource_virtualmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
resource %s "%s" {
%s = "%s"
%s = "%s"
%s = "%s"
cpu = 1
%s = "%s"
Expand All @@ -59,12 +60,17 @@ resource %s "%s" {
`
)

func buildVirtualMachineConfig(name, description, memory string, cpuPinning bool) string {
func buildBasicVirtualMachineConfig(name, description, memory string) string {
return buildVirtualMachineConfig(name, description, memory, false, false)
}

func buildVirtualMachineConfig(name, description, memory string, cpuPinning bool, isolateEmulatorThread bool) string {
return fmt.Sprintf(testAccVirtualMachineConfigTemplate, constants.ResourceTypeVirtualMachine, name,
constants.FieldCommonName, name,
constants.FieldCommonDescription, description,
constants.FieldVirtualMachineMemory, memory,
constants.FieldVirtualMachineCPUPinning, strconv.FormatBool(cpuPinning))
constants.FieldVirtualMachineCPUPinning, strconv.FormatBool(cpuPinning),
constants.FieldVirtualMachineIsolateEmulatorThread, strconv.FormatBool(isolateEmulatorThread))
}

func TestAccVirtualMachine_basic(t *testing.T) {
Expand All @@ -78,23 +84,25 @@ func TestAccVirtualMachine_basic(t *testing.T) {
CheckDestroy: testAccCheckVirtualMachineDestroy(ctx),
Steps: []resource.TestStep{
{
Config: buildVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemory, false),
Config: buildBasicVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemory),
Check: resource.ComposeTestCheckFunc(
testAccVirtualMachineExists(ctx, testAccVirtualMachineResourceName, vm),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonName, testAccVirtualMachineName),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonDescription, testAccVirtualMachineDescription),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineMemory, testAccVirtualMachineMemory),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineCPUPinning, "false"),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineIsolateEmulatorThread, "false"),
),
},
{
Config: buildVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemoryUpdate, false),
Config: buildBasicVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemoryUpdate),
Check: resource.ComposeTestCheckFunc(
testAccVirtualMachineExists(ctx, testAccVirtualMachineResourceName, vm),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonName, testAccVirtualMachineName),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonDescription, testAccVirtualMachineDescription),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineMemory, testAccVirtualMachineMemoryUpdate),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineCPUPinning, "false"),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineIsolateEmulatorThread, "false"),
),
},
},
Expand Down Expand Up @@ -133,19 +141,43 @@ func TestAccVirtualMachine_cpu_pinning(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: buildVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemory, true),
Config: buildVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemory, true, false),
Check: resource.ComposeTestCheckFunc(
testAccVirtualMachineExists(ctx, testAccVirtualMachineResourceName, vm),
func(s *terraform.State) error {
if vm.Spec.Template == nil || vm.Spec.Template.Spec.Domain.CPU == nil || !vm.Spec.Template.Spec.Domain.CPU.DedicatedCPUPlacement {
return errors.New("DedicatedCPUPlacement should be true")
}
if vm.Spec.Template == nil || vm.Spec.Template.Spec.Domain.CPU == nil || vm.Spec.Template.Spec.Domain.CPU.IsolateEmulatorThread {
return errors.New("IsolateEmulatorThread should be false")
}
return nil
},
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonName, testAccVirtualMachineName),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonDescription, testAccVirtualMachineDescription),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineMemory, testAccVirtualMachineMemory),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineCPUPinning, "true"),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineIsolateEmulatorThread, "false"),
),
},
{
Config: buildVirtualMachineConfig(testAccVirtualMachineName, testAccVirtualMachineDescription, testAccVirtualMachineMemory, true, true),
Check: resource.ComposeTestCheckFunc(
testAccVirtualMachineExists(ctx, testAccVirtualMachineResourceName, vm),
func(s *terraform.State) error {
if vm.Spec.Template == nil || vm.Spec.Template.Spec.Domain.CPU == nil || !vm.Spec.Template.Spec.Domain.CPU.DedicatedCPUPlacement {
return errors.New("DedicatedCPUPlacement should be true")
}
if vm.Spec.Template == nil || vm.Spec.Template.Spec.Domain.CPU == nil || !vm.Spec.Template.Spec.Domain.CPU.IsolateEmulatorThread {
return errors.New("IsolateEmulatorThread should be true")
}
return nil
},
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonName, testAccVirtualMachineName),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldCommonDescription, testAccVirtualMachineDescription),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineMemory, testAccVirtualMachineMemory),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineCPUPinning, "true"),
resource.TestCheckResourceAttr(testAccVirtualMachineResourceName, constants.FieldVirtualMachineIsolateEmulatorThread, "true"),
),
},
},
Expand Down Expand Up @@ -287,7 +319,7 @@ func waitForCPUMangerLabel(ctx context.Context, c *client.Client, nodeName strin
return waitUntil(func() (bool, error) {
node, err := c.KubeClient.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil {
return false, fmt.Errorf("failed to get node: %v", err)
return false, nil
}

expectedValue := strconv.FormatBool(enableCPUManager)
Expand Down
37 changes: 19 additions & 18 deletions pkg/constants/constants_virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ package constants
const (
ResourceTypeVirtualMachine = "harvester_virtualmachine"

FieldVirtualMachineMachineType = "machine_type"
FieldVirtualMachineHostname = "hostname"
FieldVirtualMachineReservedMemory = "reserved_memory"
FieldVirtualMachineRestartAfterUpdate = "restart_after_update"
FieldVirtualMachineStart = "start"
FieldVirtualMachineRunStrategy = "run_strategy"
FieldVirtualMachineCPU = "cpu"
FieldVirtualMachineMemory = "memory"
FieldVirtualMachineSSHKeys = "ssh_keys"
FieldVirtualMachineCloudInit = "cloudinit"
FieldVirtualMachineDisk = "disk"
FieldVirtualMachineNetworkInterface = "network_interface"
FieldVirtualMachineInput = "input"
FieldVirtualMachineTPM = "tpm"
FieldVirtualMachineInstanceNodeName = "node_name"
FieldVirtualMachineEFI = "efi"
FieldVirtualMachineSecureBoot = "secure_boot"
FieldVirtualMachineCPUPinning = "cpu_pinning"
FieldVirtualMachineMachineType = "machine_type"
FieldVirtualMachineHostname = "hostname"
FieldVirtualMachineReservedMemory = "reserved_memory"
FieldVirtualMachineRestartAfterUpdate = "restart_after_update"
FieldVirtualMachineStart = "start"
FieldVirtualMachineRunStrategy = "run_strategy"
FieldVirtualMachineCPU = "cpu"
FieldVirtualMachineMemory = "memory"
FieldVirtualMachineSSHKeys = "ssh_keys"
FieldVirtualMachineCloudInit = "cloudinit"
FieldVirtualMachineDisk = "disk"
FieldVirtualMachineNetworkInterface = "network_interface"
FieldVirtualMachineInput = "input"
FieldVirtualMachineTPM = "tpm"
FieldVirtualMachineInstanceNodeName = "node_name"
FieldVirtualMachineEFI = "efi"
FieldVirtualMachineSecureBoot = "secure_boot"
FieldVirtualMachineCPUPinning = "cpu_pinning"
FieldVirtualMachineIsolateEmulatorThread = "isolate_emulator_thread"

StateVirtualMachineStarting = "Starting"
StateVirtualMachineRunning = "Running"
Expand Down
47 changes: 26 additions & 21 deletions pkg/importer/resource_virtualmachine_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (v *VMImporter) DedicatedCPUPlacement() bool {
return bool(v.VirtualMachine.Spec.Template.Spec.Domain.CPU.DedicatedCPUPlacement)
}

func (v *VMImporter) IsolateEmulatorThread() bool {
return bool(v.VirtualMachine.Spec.Template.Spec.Domain.CPU.IsolateEmulatorThread)
}

func (v *VMImporter) EFI() bool {
firmware := v.VirtualMachine.Spec.Template.Spec.Domain.Firmware
return firmware != nil && firmware.Bootloader != nil && firmware.Bootloader.EFI != nil
Expand Down Expand Up @@ -365,27 +369,28 @@ func ResourceVirtualMachineStateGetter(vm *kubevirtv1.VirtualMachine, vmi *kubev
Name: vm.Name,
ResourceType: constants.ResourceTypeVirtualMachine,
States: map[string]interface{}{
constants.FieldCommonNamespace: vm.Namespace,
constants.FieldCommonName: vm.Name,
constants.FieldCommonDescription: GetDescriptions(vm.Annotations),
constants.FieldCommonTags: GetTags(vm.Labels),
constants.FieldCommonState: vmImporter.State(networkInterface, oldInstanceUID),
constants.FieldVirtualMachineCPU: vmImporter.CPU(),
constants.FieldVirtualMachineMemory: vmImporter.Memory(),
constants.FieldVirtualMachineHostname: vmImporter.HostName(),
constants.FieldVirtualMachineReservedMemory: vmImporter.ReservedMemory(),
constants.FieldVirtualMachineMachineType: vmImporter.MachineType(),
constants.FieldVirtualMachineRunStrategy: string(runStrategy),
constants.FieldVirtualMachineNetworkInterface: networkInterface,
constants.FieldVirtualMachineDisk: disk,
constants.FieldVirtualMachineInput: input,
constants.FieldVirtualMachineTPM: vmImporter.TPM(),
constants.FieldVirtualMachineCloudInit: cloudInit,
constants.FieldVirtualMachineSSHKeys: sshKeys,
constants.FieldVirtualMachineInstanceNodeName: vmImporter.NodeName(),
constants.FieldVirtualMachineEFI: vmImporter.EFI(),
constants.FieldVirtualMachineSecureBoot: vmImporter.SecureBoot(),
constants.FieldVirtualMachineCPUPinning: vmImporter.DedicatedCPUPlacement(),
constants.FieldCommonNamespace: vm.Namespace,
constants.FieldCommonName: vm.Name,
constants.FieldCommonDescription: GetDescriptions(vm.Annotations),
constants.FieldCommonTags: GetTags(vm.Labels),
constants.FieldCommonState: vmImporter.State(networkInterface, oldInstanceUID),
constants.FieldVirtualMachineCPU: vmImporter.CPU(),
constants.FieldVirtualMachineMemory: vmImporter.Memory(),
constants.FieldVirtualMachineHostname: vmImporter.HostName(),
constants.FieldVirtualMachineReservedMemory: vmImporter.ReservedMemory(),
constants.FieldVirtualMachineMachineType: vmImporter.MachineType(),
constants.FieldVirtualMachineRunStrategy: string(runStrategy),
constants.FieldVirtualMachineNetworkInterface: networkInterface,
constants.FieldVirtualMachineDisk: disk,
constants.FieldVirtualMachineInput: input,
constants.FieldVirtualMachineTPM: vmImporter.TPM(),
constants.FieldVirtualMachineCloudInit: cloudInit,
constants.FieldVirtualMachineSSHKeys: sshKeys,
constants.FieldVirtualMachineInstanceNodeName: vmImporter.NodeName(),
constants.FieldVirtualMachineEFI: vmImporter.EFI(),
constants.FieldVirtualMachineSecureBoot: vmImporter.SecureBoot(),
constants.FieldVirtualMachineCPUPinning: vmImporter.DedicatedCPUPlacement(),
constants.FieldVirtualMachineIsolateEmulatorThread: vmImporter.IsolateEmulatorThread(),
},
}, nil
}

0 comments on commit 781ce8d

Please sign in to comment.