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

<pre><code>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
          }
        }
      }
    }
  }
}
</code></pre>

<p>So the docs were a bit off of the lists of lists, it was a map of maps of lists.</p>

CC BY-SA 4.0
2020-04-03T22:09:57.057Z
2020-04-03T22:09:57.057Z
Optical Carrier
<p>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 <a href="https://www.terraform.io/docs/providers/kubernetes/r/persistent_volume.html" rel="nofollow noreferrer">kubernetes_persistent_volume</a> 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:</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata: # PVs do not have namespaces
  name: nodeconfig
spec:
  capacity:
    storage: 25Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nodeconfig
  local:
    path: /mnt/nodeconfig
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - myhost
</code></pre>

<p>Has anone dealt with this?</p>

<kubernetes><terraform><kubernetes-volume>
Terraform and Kubernetes' StorageClass with node affinity
Optical Carrier
# Terraform and Kubernetes' StorageClass with node affinity

(Asked by: username_0 on 2020-04-03)

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
spec:
  capacity:
    storage: 25Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nodeconfig
  local:
    path: /mnt/nodeconfig
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - myhost

Has anone dealt with this?

**Tags:** kubernetes, terraform, kubernetes-volume


---

## 1 answers

### Answer by username_0 on 2020-04-03. Score: 2

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.


---

