hetznercloud / terraform-provider-hcloud

Terraform Hetzner Cloud provider
https://registry.terraform.io/providers/hetznercloud/hcloud/latest
Mozilla Public License 2.0
495 stars 72 forks source link

hcloud_image datasource selects images which are not available #74

Closed devurandom closed 5 years ago

devurandom commented 5 years ago

The hcloud_image datasource can point to images which are not available (e.g. because the image is a snapshot that is still being created). Trying to create a hcloud_server from such an image will fail:

Error: Error applying plan:

1 error(s) occurred:

* hcloud_server.<...>: 1 error(s) occurred:

* hcloud_server.<...>: image '337xxxx' not found (invalid_input)

To add insult to injury, there appears to be no way to limit the selected images to available ones. Setting selector = "status == available" (or selector = "hetzner.cloud/status == available") will error out:

Error: Error refreshing state: 1 error(s) occurred:

* data.hcloud_image.<...>: 1 error(s) occurred:

* data.hcloud_image.<...>: data.hcloud_image.<...>: no image found for selector "status == available"

(Note how the name of the datasource is output twice on the same row, which appears to be a different bug.)

LKaemmerling commented 5 years ago

Hello,

first of all, the selector is the label selector. You can find information about our label system here: https://docs.hetzner.cloud/#overview-label-selector

The other problem: Could you please provide a terraform file, which shows your problem?

Edit: I have reproduced it myself while looking at #72 . Solution is on the way :)

devurandom commented 5 years ago

Thanks for the quick patch!

However, I am now on provider.hcloud v1.8.1 and still experience the issue.

Prerequisites

packer-service.json:

{
  "builders": [{
    "type": "hcloud",
    "server_type": "cx11",
    "image": "centos-7",
    "location": "nbg1",
    "snapshot_name": "java-1.8.0-openjdk",
    "snapshot_labels": {
      "nodetype": "service"
    },
    "ssh_username": "root",
    "ssh_pty": true
  }],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sudo yum -y install java-1.8.0-openjdk"
    ]
  }]
}

main.tf:

provider "hcloud" {}

data "hcloud_ssh_key" "ssh_key" {
  name = "${var.ssh_key_name}"
}

data "hcloud_image" "service" {
  # Snapshot, select by label
  selector = "nodetype == service"
  most_recent = true
}

resource "hcloud_server" "service" {
  name = "service"
  server_type = "cx11"
  image = "${data.hcloud_image.service.id}"
  ssh_keys = ["${data.hcloud_ssh_key.ssh_key.id}"]
}

How to reproduce

  1. Run packer build packer-service.json and wait until it says ==> hcloud: Creating snapshot ...
  2. While Packer is still running, run terraform plan
  3. Notice how Terraform wants to use the new image already, despite it still having status creating
    • Compare the image attribute of terraform plan's output with the output of hcloud image list -o columns=id,description,status
LKaemmerling commented 5 years ago

You need to wait until the snapshot is finished, not while it is creating.

devurandom commented 5 years ago

You need to wait until the snapshot is finished, not while it is creating.

Exactly that is my problem. The datasource considers images with status creating valid images, and there is no way to restrict it to images with status available.

LKaemmerling commented 5 years ago

Yep, but this is indeed. You can not create a snapshot and then as long as it is creating create a server from it. You can only create servers from complete snapshots (when they are available).

For your use case, you need to wait until packer is finished. This isn't a bug in the terraform provider.

devurandom commented 5 years ago

This is a deployment automation project for a team of developers. One developer cannot and should not have to control the time when another developer runs Packer to create a new version of the image. Otherwise we end up with an image control shingle.

P.S. I honestly believe that having a way to restrict the hcloud_image datasource to pick only available images when using a selector and most_recent would be immensely beneficial. And that mode should be on by default, since there appears to be little one could do with an image that is not available.

LKaemmerling commented 5 years ago

Implemented, and will be released as 1.9.0 :)