Specify "dotted" k8s labels for node pools?

6/26/2019

Kubernetes supports dots in metadata label keys (for example app.role), and indeed this seems to be a common convention.

The terraform configuration language (0.12) doesn't support dots in argument names, so labels of this form cannot be specified. For example in a google_container_node_pool configuration, I want to specify this:

resource "google_container_node_pool" "my-node-pool" {
  ...
  labels = {
    app.role = web
  }
}

Is there a workaround?

note: slashes (/) are quite common in k8s labels as well..

UPDATE: in case anyone stumbles on this same issue down the road, I figured out the root of my issue. I had incorrectly specified the labels argument as a block by omitting the =. So it looked like this:

labels {
  "app.role" = "web"
}

This yielded the following error, which pointed me in the wrong direction:

Error: Invalid argument name

  on main.tf line 45, in resource "google_container_node_pool" "primary_preemptible_nodes":
  45:       "app.role" = "web"

Argument names must not be quoted.

I noticed and fixed the missing = but I didn't put it together that map keys have different syntax from argument names.

-- Kris Pruden
google-kubernetes-engine
terraform

1 Answer

6/26/2019

I verified the suggestion from @ydaetskcoR that wrapping the label in quotes works. Here is the snippet defining the node pool that I created (using Terraform v0.11.13):

resource "google_container_node_pool" "node_pool" {
  cluster = "${google_container_cluster.cluster.name}"
  zone = "${var.cluster_location}"

  initial_node_count = "${var.node_count}"
  autoscaling {
    min_node_count = 1
    max_node_count = 5
  }
  management {
    auto_repair = true
    auto_upgrade = true
  }
  node_config {
    machine_type = "${var.machine_type}"

    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
      "https://www.googleapis.com/auth/devstorage.read_only",
    ]

    metadata {
      disable-legacy-endpoints = "true"
    }
    labels = {
      "app.role" = "web"
    }
  }
}

edit: I also verified that the same works with terraform 0.12.3.

-- Robert Bailey
Source: StackOverflow