terraform-provider-hcloud icon indicating copy to clipboard operation
terraform-provider-hcloud copied to clipboard

[Bug]: why it always marks servers to be updated?

Open yajo opened this issue 1 year ago • 2 comments

What happened?

I created one network and used it in one server. Then, when I reapply for any change, Terraform always tells me the server will be updated in-place:

  # hcloud_server.docker-server["moduontest01"] will be updated in-place
  ~ resource "hcloud_server" "docker-server" {
        id                         = "XXXXXXXX"
        name                       = "docker-moduontest01"
        # (18 unchanged attributes hidden)

      - network {
          - alias_ips   = [] -> null
          - ip          = "X.X.X.X" -> null
          - mac_address = "XX:XX:XX:XX:XX:XX" -> null
          - network_id  = XXXXXXXX -> null
        }
      + network {
          + alias_ips   = []
          + ip          = (known after apply)
          + mac_address = (known after apply)
          + network_id  = XXXXXXXX
        }
    }

What did you expect to happen?

Nothing changed in the server definition, so I expect it to not be marked to update.

Please provide a minimal working example

resource "hcloud_placement_group" "pg1" {
  name = "pg1"
  type = "spread"
}

resource "hcloud_network" "europe" {
  name     = "europe-network"
  ip_range = "10.0.0.0/16"
}

resource "hcloud_network_subnet" "europe" {
  ip_range     = "10.0.0.0/16"
  network_id   = hcloud_network.europe.id
  network_zone = "eu-central"
  type         = "cloud"
}

resource "hcloud_server" "my-server" {
  name               = "moduontest01"
  image              = "docker-ce"
  location           = "fsn1"
  placement_group_id = hcloud_placement_group.pg1.id
  server_type        = "cpx31"
  delete_protection  = true
  rebuild_protection = true
  ssh_keys           = local.hcloud_ssh_keys

  labels = {
    role = "docker"
  }

  network {
    network_id = hcloud_network.europe.id
  }

  depends_on = [
    hcloud_network_subnet.europe
  ]
}

yajo avatar Aug 04 '22 07:08 yajo

Having same issue. Might it have to do with the labels parameter (or in general empty default values)? I have not specified any labels in the server resource but terraform plan thinks an empty object was added to labels:

  # hcloud_server.rke_master[0] has changed
  ~ resource "hcloud_server" "rke_master" {
        id                         = "XXXXX"
      + labels                     = {}
        name                       = "rke-master-0"
        # (18 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }

olastor avatar Aug 18 '22 19:08 olastor

I'm also seeing a lot of:

  # hcloud_server.server["admin-sql"] will be updated in-place
  ~ resource "hcloud_server" "server" {
        id                         = "24136938"
        name                       = "admin-sql"
        # (18 unchanged attributes hidden)

      - network {
          - alias_ips   = [] -> null
          - ip          = "10.10.8.4" -> null
          - mac_address = "86:00:00:22:92:61" -> null
          - network_id  = 2052788 -> null
        }
      + network {
          + alias_ips   = []
          + ip          = (known after apply)
          + mac_address = (known after apply)
          + network_id  = 2052788
        }

        # (1 unchanged block hidden)
    }

Using hetznercloud/hcloud v1.35.2

Exchizz avatar Sep 24 '22 12:09 Exchizz

@yajo , to avoid this error, use the resource as in the example. It's works correctly:

resource "hcloud_server_network" "srvnetwork" {
  server_id  = hcloud_server.node1.id
  network_id = hcloud_network.mynet.id
}

b8dmin avatar Oct 05 '22 20:10 b8dmin

@cital In my case I don't want the VM's to have public IPs, hence I need the network block according to https://github.com/hetznercloud/terraform-provider-hcloud/issues/555#issuecomment-1206044082

Exchizz avatar Oct 06 '22 10:10 Exchizz

It looks like this happens because the alias_ips column is not marked as computed. I will create a PR for this tomorrow.

While researching this I found another bug: Multiple network blocks with the same network_id are allowed. This will cause both blocks to "fight" for the actual configuration, changing with each apply. Fix in #594.

apricote avatar Nov 21 '22 15:11 apricote