Skip to content

Commit c9b8a3f

Browse files
authored
fix(vm)!: revert cpu.units default to use PVE server default (#2402)
* fix(vm)!: revert `cpu.units` default to use PVE server default Fixed a regression introduced in v0.85.0 where `cpu.units` default was changed from 1024 to 100, causing unexpected plan diffs for existing VMs. The `cpu.units` attribute is now computed from the Proxmox server instead of using a hardcoded provider default. When not explicitly set, PVE will use its own default (1024 for cgroups v1 or 100 for cgroups v2), and the provider will read and store the actual value from the server. Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> * acceptance tests Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> * cleanup Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --------- Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
1 parent 0c7fad9 commit c9b8a3f

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

docs/resources/virtual_environment_vm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ output "ubuntu_vm_public_key" {
266266
See <https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels>
267267
- `custom-<model>` - Custom CPU model. All `custom-<model>` values
268268
should be defined in `/etc/pve/virtual-guest/cpu-models.conf` file.
269-
- `units` - (Optional) The CPU units (defaults to `1024`).
269+
- `units` - (Optional) The CPU units. PVE default is `1024` for cgroups v1 and `100` for cgroups v2.
270270
- `affinity` - (Optional) The CPU cores that are used to run the VM’s vCPU. The
271271
value is a list of CPU IDs, separated by commas. The CPU IDs are zero-based.
272272
For example, `0,1,2,3` (which also can be shortened to `0-3`) means that the VM’s vCPUs are run on the first four

fwprovider/nodes/vm/cpu/resource_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ func TestAccResourceVM2CPU(t *testing.T) {
206206
}),
207207
),
208208
}}},
209+
// regression test for https://github.com/bpg/terraform-provider-proxmox/issues/2353
210+
{"create VM without cpu.units and verify no drift", []resource.TestStep{
211+
{
212+
Config: te.RenderConfig(`
213+
resource "proxmox_virtual_environment_vm2" "test_vm" {
214+
node_name = "{{.NodeName}}"
215+
name = "test-cpu-units-default"
216+
}`),
217+
},
218+
{
219+
RefreshState: true,
220+
},
221+
}},
209222
}
210223

211224
for _, tt := range tests {

fwprovider/test/resource_vm_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ func TestAccResourceVM(t *testing.T) {
150150
),
151151
},
152152
}},
153+
// regression test for https://github.com/bpg/terraform-provider-proxmox/issues/2353
154+
{"create VM without cpu.units and verify no drift", []resource.TestStep{
155+
{
156+
Config: te.RenderConfig(`
157+
resource "proxmox_virtual_environment_vm" "test_cpu_units" {
158+
node_name = "{{.NodeName}}"
159+
started = false
160+
cpu {
161+
cores = 2
162+
}
163+
}`),
164+
},
165+
{
166+
RefreshState: true,
167+
},
168+
}},
153169
{"set cpu.architecture as non root is not supported", []resource.TestStep{
154170
{
155171
Config: te.RenderConfig(`

proxmoxtf/resource/vm/vm.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const (
7373
dvCPUNUMA = false
7474
dvCPUSockets = 1
7575
dvCPUType = "qemu64"
76-
dvCPUUnits = 100
7776
dvCPUAffinity = ""
7877
dvDescription = ""
7978

@@ -634,7 +633,7 @@ func VM() *schema.Resource {
634633
mkCPUNUMA: dvCPUNUMA,
635634
mkCPUSockets: dvCPUSockets,
636635
mkCPUType: dvCPUType,
637-
mkCPUUnits: dvCPUUnits,
636+
mkCPUUnits: 0,
638637
mkCPUAffinity: dvCPUAffinity,
639638
},
640639
}, nil
@@ -704,7 +703,7 @@ func VM() *schema.Resource {
704703
Type: schema.TypeInt,
705704
Description: "The CPU units",
706705
Optional: true,
707-
Default: dvCPUUnits,
706+
Computed: true,
708707
ValidateDiagFunc: validation.ToDiagFunc(
709708
validation.IntBetween(1, 262144),
710709
),
@@ -2186,7 +2185,6 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m any) diag.Diag
21862185
}
21872186
updateBody.NUMAEnabled = &cpuNUMA
21882187
updateBody.CPUSockets = ptr.Ptr(int64(cpuSockets))
2189-
updateBody.CPUUnits = ptr.Ptr(int64(cpuUnits))
21902188

21912189
if cpuAffinity != "" {
21922190
updateBody.CPUAffinity = &cpuAffinity
@@ -2199,6 +2197,10 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m any) diag.Diag
21992197
if cpuLimit > 0 {
22002198
updateBody.CPULimit = ptr.Ptr(int64(cpuLimit))
22012199
}
2200+
2201+
if cpuUnits > 0 {
2202+
updateBody.CPUUnits = ptr.Ptr(int64(cpuUnits))
2203+
}
22022204
}
22032205

22042206
vmConfig, err := vmAPI.GetVM(ctx)
@@ -2890,7 +2892,6 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m any) diag.Dia
28902892
Type: cpuType,
28912893
},
28922894
CPUSockets: ptr.Ptr(int64(cpuSockets)),
2893-
CPUUnits: ptr.Ptr(int64(cpuUnits)),
28942895
DedicatedMemory: &memoryDedicated,
28952896
DeletionProtection: &protection,
28962897
EFIDisk: efiDisk,
@@ -2931,6 +2932,10 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m any) diag.Dia
29312932
createBody.CPULimit = ptr.Ptr(int64(cpuLimit))
29322933
}
29332934

2935+
if cpuUnits > 0 {
2936+
createBody.CPUUnits = ptr.Ptr(int64(cpuUnits))
2937+
}
2938+
29342939
if cpuAffinity != "" {
29352940
createBody.CPUAffinity = &cpuAffinity
29362941
}
@@ -4118,8 +4123,7 @@ func vmReadCustom(
41184123
if vmConfig.CPUUnits != nil {
41194124
cpu[mkCPUUnits] = int(*vmConfig.CPUUnits)
41204125
} else {
4121-
// Default value of "cpuunits" is "1024" according to the API documentation.
4122-
cpu[mkCPUUnits] = 1024
4126+
cpu[mkCPUUnits] = 0
41234127
}
41244128

41254129
if vmConfig.CPUAffinity != nil {
@@ -4143,7 +4147,7 @@ func vmReadCustom(
41434147
cpu[mkCPULimit] != dvCPULimit ||
41444148
cpu[mkCPUSockets] != dvCPUSockets ||
41454149
cpu[mkCPUType] != dvCPUType ||
4146-
cpu[mkCPUUnits] != dvCPUUnits {
4150+
cpu[mkCPUUnits] != 0 {
41474151
err := d.Set(mkCPU, []any{cpu})
41484152
diags = append(diags, diag.FromErr(err)...)
41494153
}
@@ -5541,9 +5545,12 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnosti
55415545

55425546
updateBody.CPUCores = ptr.Ptr(int64(cpuCores))
55435547
updateBody.CPUSockets = ptr.Ptr(int64(cpuSockets))
5544-
updateBody.CPUUnits = ptr.Ptr(int64(cpuUnits))
55455548
updateBody.NUMAEnabled = &cpuNUMA
55465549

5550+
if cpuUnits > 0 {
5551+
updateBody.CPUUnits = ptr.Ptr(int64(cpuUnits))
5552+
}
5553+
55475554
// CPU affinity is a special case, only root can change it.
55485555
// we can't even have it in the delete list, as PVE will return an error for non-root.
55495556
// Hence, checking explicitly if it has changed.

0 commit comments

Comments
 (0)