Im able to make a number of kubernetes constructs in terraform and even have done so for my ephemeral stateful data that requires low latency disk performance. I can terraform up a kubernetes_persistent_volume but trying to do so with node_affinity has got me a bit perplexed as it requires a lists of lists (nodeSelectorTerms and matchExpressions), where the inside list has key/value pairs, and just not sure how to terraform all that. Heres the working yaml version:

apiVersion: v1
kind: PersistentVolume
metadata: # PVs do not have namespaces
  name: nodeconfig
    storage: 25Gi
  volumeMode: Filesystem
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nodeconfig
    path: /mnt/nodeconfig
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          - myhost

Has anone dealt with this?

1 Answer 1


Well of course its always the same - as soon as you actually do ask for help you figure it out...

resource "kubernetes_persistent_volume" "nodeconfig" {
  metadata {
    name = "nodeconfig"
    labels = {
      a = "a"
  spec {
    capacity = {
      storage = "25Gi"
    storage_class_name               = "nodeconfig"
    persistent_volume_reclaim_policy = "Retain"
    access_modes                     = ["ReadOnlyMany"]
    persistent_volume_source {
      local {
        path = "/mnt/nodeconfig"
    node_affinity {
      required {
        node_selector_term {
          match_expressions {
            key = "kubernetes.io/hostname"
            operator = "In"
            values = [var.config.RGs.Primary.clustervmhost]  # must mnanually put this one in

So the docs were a bit off of the lists of lists, it was a map of maps of lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.