dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.58k stars 456 forks source link

Domains set to be incorrectly recreated to add "type=kvm" property #1062

Open rombert opened 9 months ago

rombert commented 9 months ago

System Information

Linux distribution

openSUSE Tumbleweed

Terraform version

Terraform v1.5.5

Provider and libvirt versions

v0.7.6

Checklist

Description of Issue/Question

Setup

I have a very plain terraform setup, coming from the 0.6.x days. The domain definition looks like this

# domain (VM definition)
resource "libvirt_domain" "microos_domain" {
  for_each  = { for vm_def in var.vm_defs : vm_def.hostname => vm_def }
  name      = "homelab002-${each.key}"
  autostart = true

  cpu {
    mode = "host-passthrough"
  }

  memory = each.value.memory
  vcpu   = each.value.cpu_count

  disk {
    volume_id = libvirt_volume.os_volume[each.key].id
  }

  network_interface {
    network_name   = libvirt_network.homelab002_net.name
    hostname       = data.template_file.ignition_data[each.key].vars.hostname
    wait_for_lease = true
  }

I upgraded from 0.6.14 to 0.7.6 and terraform plan now wants to recreate my domains, e.g.

  # libvirt_domain.microos_domain["worker-1"] must be replaced
-/+ resource "libvirt_domain" "microos_domain" {
      ~ arch            = "x86_64" -> (known after apply)
      - cmdline         = [] -> null
      ~ emulator        = "/usr/bin/qemu-system-x86_64" -> (known after apply)
      ~ id              = "5c6ce085-2f92-4af9-966e-a04a3aaa1567" -> (known after apply)
      ~ machine         = "pc-i440fx-6.2" -> (known after apply)
        name            = "homelab002-worker-1"
      + type            = "kvm" # forces replacement
        # (7 unchanged attributes hidden)

      ~ network_interface {
          ~ addresses      = [
              - "10.25.1.107",
            ] -> (known after apply)
          ~ mac            = "52:54:00:E6:3A:DC" -> (known after apply)
          ~ network_id     = "a7677a48-27ad-4769-8af2-504d9cb396ef" -> (known after apply)
            # (3 unchanged attributes hidden)
        }

        # (2 unchanged blocks hidden)
    }

I checked the VM definitions via virt-manager and they all start with

<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" type="kvm">
  <name>homelab002-worker-1</name>

Steps to Reproduce Issue

I guess start with 0.6.x and upgrade to 0.7.x?

Additional information:

Do you have SELinux or Apparmor/Firewall enabled? Some special configuration? No. Have you tried to reproduce the issue without them enabled? N/A

This behaviour seems to have been brought in via #1045

michaelbeaumont commented 9 months ago

Yeah, it was probably a mistake to set the default to be kvm. Can you set it to the empty string to avoid the recreation?

rombert commented 9 months ago

Yeah, it was probably a mistake to set the default to be kvm. Can you set it to the empty string to avoid the recreation?

Thanks for the hint. With that property set the domains are no longer set to be re-created.

My immediate problem is solved, I'll leave this up to you whether you want to follow-up with a fix or just close.

scabala commented 3 weeks ago

Since the PR for providing a property is merged, I think this can be closed?

rombert commented 3 weeks ago

@scabala - do you mean https://github.com/dmacvicar/terraform-provider-libvirt/pull/1045 ? That was the one that caused the problem for me. I have a workaround but the root cause is not fixed.

scabala commented 3 weeks ago

Oh, I misunderstood the issue.

I think this is part of bigger idea of having non-recreating updates to domains like #1069

rombert commented 3 weeks ago

Yes, #1069 definitely sounds like a good idea, I sometimes change the terraform definitions then update the domains in-place to prevent disruptive operations.