Skip to content
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ tf-*
*/*.tfvars
*/*.pem
*.tgz
main.tf
terraform-provider-rancher2
node_modules
GEMINI.md
Expand Down
8 changes: 7 additions & 1 deletion docs/resources/machine_config_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ resource "rancher2_machine_config_v2" "foo-harvester-v2" {
generate_name = "foo-harvester-v2"
harvester_config {
vm_namespace = "default"
cpu_count = "2"

cpu {
count = 2
pinning = true
isolate_emulator_thread = true
}

memory_size = "4"
disk_info = <<EOF
{
Expand Down
65 changes: 65 additions & 0 deletions examples/resources/rancher2_machine_config_v2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
terraform {
required_version = "~> v1.14.8"

required_providers {
rancher2 = {
source = "terraform.local/local/rancher2"
version = "0.0.0-dev"
}
}
}

provider "rancher2" {
api_url = "https://rancher.192.168.0.131.sslip.io"
insecure = true
access_key = "token-hf7vc"
secret_key = "mmf52l956zjbbkrhncmc4q8fvnrlbdhfftrrjdlnqwmkz5b7lrlst7"
}

resource "rancher2_machine_config_v2" "foo-harvester-v2" {
generate_name = "foo-harvester-v2"

harvester_config {
vm_namespace = "default"

cpu {
count = 2
pinning = true
isolate_emulator_thread = true
}

memory_size = "4"

disk_info = <<EOF
{
"disks": [{
"imageName": "harvester-public/image-57hzg",
"size": 40,
"bootOrder": 1
}]
}
EOF

network_info = <<EOF
{
"interfaces": [{
"networkName": "harvester-public/vlan1"
}]
}
EOF

ssh_user = "ubuntu"

user_data = <<EOF
package_update: true
packages:
- qemu-guest-agent
- iptables
runcmd:
- - systemctl
- enable
- '--now'
- qemu-guest-agent.service
EOF
}
}
37 changes: 32 additions & 5 deletions rancher2/schema_machine_config_v2_harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ import (

//Schemas

func machineConfigV2HarvesterCPUFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"count": {
Type: schema.TypeString,
Optional: true,
Default: "2",
Description: "vCPU count",
},
"pinning": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "pin vCPUs to physical cores",
},
"isolate_emulator_thread": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "isolate emulator thread from VM vCPUs on separate CPU core",
},
}
return s
}

func machineConfigV2HarvesterFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"vm_namespace": {
Expand All @@ -18,11 +42,14 @@ func machineConfigV2HarvesterFields() map[string]*schema.Schema {
Optional: true,
Description: "VM affinity, base64 is supported",
},
"cpu_count": {
Type: schema.TypeString,
Optional: true,
Default: "2",
Description: "CPU count",
"cpu": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Description: "CPU settings",
Elem: &schema.Resource{
Schema: machineConfigV2HarvesterCPUFields(),
},
},
"memory_size": {
Type: schema.TypeString,
Expand Down
87 changes: 67 additions & 20 deletions rancher2/structure_machine_config_v2_harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ const (

//Types

type MachineConfigV2HarvesterCPU struct {
Count int `json:"count,omitempty", yaml:count,omitempty"`
Pinning bool `json:"pinning,omitempty", yaml:pinning,omitempty"`
IsolateEmulatorThread bool `json:"isolateEmulatorThread,omitempty", yaml:isolateEmulatorThread,omitempty"`
}

type machineConfigV2Harvester struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
VMNamespace string `json:"vmNamespace,omitempty" yaml:"vmNamespace,omitempty"`
VMAffinity string `json:"vmAffinity,omitempty" yaml:"vmAffinity,omitempty"`
CPUCount string `json:"cpuCount,omitempty" yaml:"cpuCount,omitempty"`
MemorySize string `json:"memorySize,omitempty" yaml:"memorySize,omitempty"`
ReservedMemorySize string `json:"reservedMemorySize,omitempty" yaml:"reservedMemorySize,omitempty"`
DiskSize string `json:"diskSize,omitempty" yaml:"diskSize,omitempty"`
DiskBus string `json:"diskBus,omitempty" yaml:"diskBus,omitempty"`
ImageName string `json:"imageName,omitempty" yaml:"imageName,omitempty"`
DiskInfo string `json:"diskInfo,omitempty" yaml:"diskInfo,omitempty"`
SSHUser string `json:"sshUser,omitempty" yaml:"sshUser,omitempty"`
SSHPassword string `json:"sshPassword,omitempty" yaml:"sshPassword,omitempty"`
NetworkName string `json:"networkName,omitempty" yaml:"networkName,omitempty"`
NetworkModel string `json:"networkModel,omitempty" yaml:"networkModel,omitempty"`
NetworkInfo string `json:"networkInfo,omitempty" yaml:"networkInfo,omitempty"`
UserData string `json:"userData,omitempty" yaml:"userData,omitempty"`
NetworkData string `json:"networkData,omitempty" yaml:"networkData,omitempty"`
VMNamespace string `json:"vmNamespace,omitempty" yaml:"vmNamespace,omitempty"`
VMAffinity string `json:"vmAffinity,omitempty" yaml:"vmAffinity,omitempty"`
CPU *MachineConfigV2HarvesterCPU `json:"cpu,omitempty" yaml:"cpu,omitempty"`
MemorySize string `json:"memorySize,omitempty" yaml:"memorySize,omitempty"`
ReservedMemorySize string `json:"reservedMemorySize,omitempty" yaml:"reservedMemorySize,omitempty"`
DiskSize string `json:"diskSize,omitempty" yaml:"diskSize,omitempty"`
DiskBus string `json:"diskBus,omitempty" yaml:"diskBus,omitempty"`
ImageName string `json:"imageName,omitempty" yaml:"imageName,omitempty"`
DiskInfo string `json:"diskInfo,omitempty" yaml:"diskInfo,omitempty"`
SSHUser string `json:"sshUser,omitempty" yaml:"sshUser,omitempty"`
SSHPassword string `json:"sshPassword,omitempty" yaml:"sshPassword,omitempty"`
NetworkName string `json:"networkName,omitempty" yaml:"networkName,omitempty"`
NetworkModel string `json:"networkModel,omitempty" yaml:"networkModel,omitempty"`
NetworkInfo string `json:"networkInfo,omitempty" yaml:"networkInfo,omitempty"`
UserData string `json:"userData,omitempty" yaml:"userData,omitempty"`
NetworkData string `json:"networkData,omitempty" yaml:"networkData,omitempty"`
}

type MachineConfigV2Harvester struct {
Expand All @@ -42,6 +48,24 @@ type MachineConfigV2Harvester struct {

// Flatteners

func flattenMachineConfigV2HarvesterCPU(in *MachineConfigV2HarvesterCPU) []interface{} {
if in == nil {
return nil
}

obj := make(map[string]interface{})

if in.Count > 0 {
obj["count"] = in.Count
} else {
obj["count"] = 2
}
obj["pinning"] = in.Pinning
obj["isolateEmulatorThread"] = in.IsolateEmulatorThread

return []interface{}{obj}
}

func flattenMachineConfigV2Harvester(in *MachineConfigV2Harvester) []interface{} {
if in == nil {
return nil
Expand All @@ -57,8 +81,8 @@ func flattenMachineConfigV2Harvester(in *MachineConfigV2Harvester) []interface{}
obj["vm_affinity"] = in.VMAffinity
}

if len(in.CPUCount) > 0 {
obj["cpu_count"] = in.CPUCount
if in.CPU != nil {
obj["cpu"] = flattenMachineConfigV2HarvesterCPU(in.CPU)
}

if len(in.MemorySize) > 0 {
Expand Down Expand Up @@ -118,6 +142,29 @@ func flattenMachineConfigV2Harvester(in *MachineConfigV2Harvester) []interface{}

// Expanders

func expandMachineConfigV2HarvesterCPU(p []interface{}) *MachineConfigV2HarvesterCPU {
if p == nil || len(p) != 1 || p[0] == nil {
return nil
}
in := p[0].(map[string]interface{})

obj := MachineConfigV2HarvesterCPU{}

if v, ok := in["count"].(int); ok {
obj.Count = v
}

if v, ok := in["pinning"].(bool); ok {
obj.Pinning = v
}

if v, ok := in["isolateEmulatorThread"].(bool); ok {
obj.IsolateEmulatorThread = v
}

return &obj
}

func expandMachineConfigV2Harvester(p []interface{}, source *MachineConfigV2) *MachineConfigV2Harvester {
if p == nil || len(p) == 0 || p[0] == nil {
return nil
Expand All @@ -142,8 +189,8 @@ func expandMachineConfigV2Harvester(p []interface{}, source *MachineConfigV2) *M
obj.VMAffinity = v
}

if v, ok := in["cpu_count"].(string); ok && len(v) > 0 {
obj.CPUCount = v
if v, ok := in["cpu"].([]interface{}); ok && len(v) > 0 {
obj.CPU = expandMachineConfigV2HarvesterCPU(v)
}

if v, ok := in["memory_size"].(string); ok && len(v) > 0 {
Expand Down
77 changes: 77 additions & 0 deletions rancher2/structure_machine_config_v2_harvester_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package rancher2

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFlattenMachineConfigV2HarvesterCPU(t *testing.T) {
testcases := []struct {
Input *MachineConfigV2HarvesterCPU
Expectation []any
}{
{
// All default values
Input: &MachineConfigV2HarvesterCPU{},
Expectation: []any{map[string]interface{}{
"count": 2,
"pinning": false,
"isolateEmulatorThread": false,
}},
},
{
Input: &MachineConfigV2HarvesterCPU{
Count: 3,
Pinning: true,
IsolateEmulatorThread: true,
},
Expectation: []any{map[string]interface{}{
"count": 3,
"pinning": true,
"isolateEmulatorThread": true,
}},
},
}

for _, tc := range testcases {
output := flattenMachineConfigV2HarvesterCPU(tc.Input)
assert.Equal(t, tc.Expectation, output, "unexpected output from flattenMachineConfigV2HarvesterCPU")
}
}

func TestExpandMachineConfigV2HarvesterCPU(t *testing.T) {
testcases := []struct {
Input []any
Expectation *MachineConfigV2HarvesterCPU
}{
{
// All default values
Input: []any{map[string]interface{}{
"count": 2,
"pinning": false,
"isolateEmulatorThread": false,
}},
Expectation: &MachineConfigV2HarvesterCPU{
Count: 2,
},
},
{
Input: []any{map[string]interface{}{
"count": 3,
"pinning": true,
"isolateEmulatorThread": true,
}},
Expectation: &MachineConfigV2HarvesterCPU{
Count: 3,
Pinning: true,
IsolateEmulatorThread: true,
},
},
}

for _, tc := range testcases {
output := expandMachineConfigV2HarvesterCPU(tc.Input)
assert.Equal(t, tc.Expectation, output, "unexpected output from flattenMachineConfigV2HarvesterCPU")
}
}
Loading