dmacvicar / terraform-provider-libvirt

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

cloud-init.iso - Error: while retrieving remote ISO #973

Open ebal opened 1 year ago

ebal commented 1 year ago

System Information

Linux distribution

Archlinux

Terraform version

Terraform v1.2.8

Provider and libvirt versions

dmacvicar/libvirt/0.6.14
git describe --always --abbrev=40 --dirty

8e162ae9b6f09a5e7c3b88413c498f317e6cc214

Checklist

Description of Issue/Question

Setup

resource "libvirt_cloudinit_disk" "cloud-init" {
  for_each = local.VMs
    depends_on = [data.template_file.user_data]

    name      = "${random_id.id[each.key].id}_cloud-init.iso"
    user_data = data.template_file.user_data[each.key].rendered
}

resource "libvirt_domain" "domain-ubuntu" {
  for_each = local.VMs
    depends_on = [libvirt_cloudinit_disk.cloud-init]

    name = each.value.hostname

    memory = each.value.vmem
    vcpu   = each.value.vcpu

    cloudinit = libvirt_cloudinit_disk.cloud-init[each.key].id

    network_interface {
      network_name   = "default"
      wait_for_lease = true
    }

    console {
      target_type = "serial"
      type        = "pty"
      target_port = "0"
    }
    console {
      target_type = "virtio"
      type        = "pty"
      target_port = "1"
    }

    disk {
      volume_id = libvirt_volume.ubuntu-base[each.key].id
    }

}

So here is the issue,

from what I see (tf debug) is that cloud-init.iso is being created under a temp directory before "uploaded" to the libvirt image directory.

/usr/bin/mkisofs -output /tmp/cloudinit2562860977/XOxgrQ_cloud-init.iso -volid cidata -joliet -rock /tmp/cloudinit2562860977/user-data /tmp/cloudinit2562860977/meta-data /tmp/cloudinit2562860977/network-config

that's perfectly okay.

The issue I am facing is when libvirt_domain needs the cloudinit id. We are getting the cloud-init id in the creation time, before the ISO being uploaded into the final location.

and so I am getting messages like:

│ Error: error while retrieving remote ISO: error retrieving info for volume: Storage volume not found: no storage vol with matching name '_jX2xw_cloud-init.iso'
│ 
│   with libvirt_cloudinit_disk.cloud-init["k8scpnode"],
│   on Cloudinit.tf line 23, in resource "libvirt_cloudinit_disk" "cloud-init":
│   23: resource "libvirt_cloudinit_disk" "cloud-init" {
│ 

As when the libvirt_domain starts to create the VM, the ISO is not yet in the uploaded directory!

Steps to Reproduce Issue

terraform apply

The weird thing is that, this bug is truly random that depends completely on many things, like cpu/mem/disk usage at the moment of terraform apply. Most of the times will create a single VM without an issue. When you are working building a a lab with many VMs then this issue becomes a really annoying bug.


Additional information:

A workaround, I was thinking, would be to return the cloud-init.iso id after a successful upload to libvirt and not in CreateIso but when UploadIso

https://github.com/dmacvicar/terraform-provider-libvirt/blob/main/libvirt/cloudinit_def.go#L36

https://github.com/dmacvicar/terraform-provider-libvirt/blob/68c9bb05fd229efd70db0fe8cd0c1faef9c6e64f/libvirt/cloudinit_def.go#L110

PS 1. I am really sorry - have no idea about golang. PS 2. I hope what I wrote above makes some sense

fantix commented 1 year ago

fwiw it might be related to recent changes in libvirt that this tf provider is not catching up with - I reverted to libvirt 8.0.0 and this error is gone somehow.