Dynamic node pools in digitalocean kubernetes with terraform

huangapple go评论68阅读模式
英文:

Dynamic node pools in digitalocean kubernetes with terraform

问题

我试图添加一个动态节点池,但出现了一些错误。

main.tf:

resource "digitalocean_kubernetes_cluster" "example_cluster" {
  name    = var.name
  region  = var.region
  version = "1.24.12-do.0"

  dynamic "node_pool" {
    for_each = var.node_pools
    content {
      name       = node_pool.value.name
      size       = node_pool.value.size
      node_count = node_pool.value.node_count
      labels     = node_pool.value.labels
      auto_scale = "true"
      
      dynamic "auto_scale" {
        for_each = node_pool.value.auto_scale ? [node_pool.value.auto_scale] : []
        content {
          min_nodes = node_pool.value.min_nodes
          max_nodes = node_pool.value.max_nodes
        }
      }
    }
  }
}

veriables.tf:

variable "name" {
  type        = string
  description = "The name of the Kubernetes cluster to be created."
}

variable "region" {
  type        = string
  description = "The region in which to create the Kubernetes cluster."
}

variable "node_pools" {
  description = "List of node pools"
  type = list(object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
  }))
}

my app.tf:

module "kubernetes" {
  source = "../modules/kubernetes"

  name   = "test-cluster"
  region = "fra1"
  node_pools = [
    {
      name       = "pool-test1-8c-16gb-tests"
      size       = "s-8vcpu-16gb"
      node_count = 2
      auto_scale = "true"
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test1"
      }
    },
    {
      name       = "pool-test2-4c-8gb"
      size       = "s-4vcpu-8gb"
      node_count = 1
      auto_scale = "true"
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test2"
      }
    }
  ]
}

我遇到的错误是:

> │ Error: Unsupported block type
│ 
│   on ../modules/DO/kubernetes/main.tf line 32, in resource "digitalocean_kubernetes_cluster" "example_cluster":
│   32:       dynamic "auto_scale" {
│ 
│ Blocks of type "auto_scale" are not expected here.


如果我硬编码 auto_scale,我得到这个错误:

>  ╷
│ Error: Too many node_pool blocks
│ 
│   on ../modules/DO/kubernetes/main.tf line 22, in resource "digitalocean_kubernetes_cluster" "example_cluster":
│   22:     content {
│ 
│ No more than 1 "node_pool" blocks are allowed
╵

有人能指出我做错了什么吗?
英文:

I'm trying to add a dynamic node pool but I'm getting a few errors.

main.tf

resource "digitalocean_kubernetes_cluster" "example_cluster" {
  name    = var.name
  region  = var.region
  version = "1.24.12-do.0"

  dynamic "node_pool" {
    for_each = var.node_pools
    content {
      name       = node_pool.value.name
      size       = node_pool.value.size
      node_count = node_pool.value.node_count
      labels     = node_pool.value.labels
      auto_scale = "true"
      #min_nodes  = "1"
      #max_nodes  = "15"
      #count = node_pool.value.auto_scale ? 1 : 0
     
      dynamic "auto_scale" {
        for_each = node_pool.value.auto_scale ? [node_pool.value.auto_scale] : []
        content {
          min_nodes = node_pool.value.min_nodes
          max_nodes = node_pool.value.max_nodes

        }
      } 
    }
  }

veriables.tf

variable "name" {
  type        = string
  description = "The name of the Kubernetes cluster to be created."
}

variable "region" {
  type        = string
  description = "The region in which to create the Kubernetes cluster."
}

#variable "version" {
 # description = "Version of the Kubernetes cluster"
#}

variable "node_pools" {
  description = "List of node pools"
  type = list(object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
  }))
}

and my app.tf

module "kubernetes" {
  source = "../modules/kubernetes"

  name   = "test-cluster"
  region = "fra1"
  # version = "1.24.12-do.0"
  node_pools = [
    {
      name       = "pool-test1-8c-16gb-tests"
      size       = "s-8vcpu-16gb"
      node_count = 2
      auto_scale = "true"
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test1"
      }
    },
    {
      name       = "pool-test2-4c-8gb"
      size       = "s-4vcpu-8gb"
      node_count = 1
      auto_scale = "true"
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test2"
      }
    }
  ]
}

the error I'm getting is

> │ Error: Unsupported block type

│ on ../modules/DO/kubernetes/main.tf line 32, in resource "digitalocean_kubernetes_cluster" "example_cluster":
│ 32: dynamic "auto_scale" {

│ Blocks of type "auto_scale" are not expected here.

if I hardcode the auto_scale I'm getting this

> ╷
│ Error: Too many node_pool blocks

│ on ../modules/DO/kubernetes/main.tf line 22, in resource "digitalocean_kubernetes_cluster" "example_cluster":
│ 22: content {

│ No more than 1 "node_pool" blocks are allowed

can someone point out what am I doing wrong?

答案1

得分: 2

The DO provider does not allow for more than one node_pool block:

> (Required) A block representing the cluster's default node pool. Additional node pools may be added to the cluster using the digitalocean_kubernetes_node_pool resource.

Furthermore, the auto_scale option is not a block, rather an argument which should have a boolean value:

resource "digitalocean_kubernetes_cluster" "foo" {
  name    = "foo"
  region  = "nyc1"
  version = "1.22.8-do.1"

  node_pool {
    name       = "autoscale-worker-pool"
    size       = "s-2vcpu-2gb"
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 5
  }
}

This example is taken from the resource documentation.

Here is how I would approach the refactoring:

  1. Define two variables, one for default node group, one for additional node groups
  2. Use the digitalocean_kubernetes_node_pool resource for additional node pools

Variables would then look like the following:

variable "default_node_pool" {
  type = object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
    }
  )
  description = "Default node pool."

  default = {
    name       = "pool-test1-8c-16gb-tests"
    size       = "s-8vcpu-16gb"
    node_count = 2
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 15
    labels = {
      runner = "test1"
    }
  }
}

variable "additional_node_pools" {
  description = "Additional node pools."
  type = map(object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
    }
  ))

  default = {
    "test1" = {
      name       = "pool-test2-4c-8gb"
      size       = "s-4vcpu-8gb"
      node_count = 1
      auto_scale = true
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test2"
      }
    }
  }
}

Then, in the module code, you would change to the following:

resource "digitalocean_kubernetes_cluster" "example_cluster" {
  name    = var.name
  region  = var.region
  version = "1.24.12-do.0"

  node_pool {
    name       = var.default_node_pool.name
    size       = var.default_node_pool.size
    node_count = var.default_node_pool.node_count
    labels     = var.default_node_pool.labels
    auto_scale = var.default_node_pool.auto_scale
    max_nodes  = var.default_node_pool.max_nodes
    min_nodes  = var.default_node_pool.min_nodes
  }
}

resource "digitalocean_kubernetes_node_pool" "bar" {
  for_each   = var.additional_node_pools
  cluster_id = digitalocean_kubernetes_cluster.example_cluster.id

  name       = each.value.name
  size       = each.value.size
  node_count = each.value.node_count
  min_nodes  = each.value.min_nodes
  max_nodes  = each.value.max_nodes
  auto_scale = each.value.auto_scale

  labels = each.value.labels
}

When calling the module you would of course have to adjust how you are passing the variables according to the module definition:

module "kubernetes" {
  source = "../modules/kubernetes"

  name   = "test-cluster"
  region = "fra1"
  # version = "1.24.12-do.0"
  default_node_pool = {
    name       = "pool-test1-8c-16gb-tests"
    size       = "s-8vcpu-16gb"
    node_count = 2
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 15
    labels = {
      runner = "test1"
    }
  }
  additional_node_pools = {
    "test1" = {
      name       = "pool-test2-4c-8gb"
      size       = "s-4vcpu-8gb"
      node_count = 1
      auto_scale = true
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test2"
      }
    }
  }
}
英文:

The DO provider does not allow for more than one node_pool block:

> (Required) A block representing the cluster's default node pool. Additional node pools may be added to the cluster using the digitalocean_kubernetes_node_pool resource.

Furthermore, the auto_scale option is not a block, rather an argument which should have a boolean value:

resource "digitalocean_kubernetes_cluster" "foo" {
  name    = "foo"
  region  = "nyc1"
  version = "1.22.8-do.1"

  node_pool {
    name       = "autoscale-worker-pool"
    size       = "s-2vcpu-2gb"
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 5
  }
}

This example is taken from the resource documentation.

Here is how I would approach the refactoring:

  1. Define two variables, one for default node group, one for additional node groups
  2. Use the digitalocean_kubernetes_node_pool resource for additional node pools

Variables would then look like the following:

variable "default_node_pool" {
  type = object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
    }
  )
  description = "Default node pool."

  default = {
    name       = "pool-test1-8c-16gb-tests"
    size       = "s-8vcpu-16gb"
    node_count = 2
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 15
    labels = {
      runner = "test1"
    }
  }
}

variable "additional_node_pools" {
  description = "Additional node pools."
  type = map(object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
    }
  ))

  default = {
    "test1" = {
      name       = "pool-test2-4c-8gb"
      size       = "s-4vcpu-8gb"
      node_count = 1
      auto_scale = true
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test2"
      }
    }
  }
}

Then, in the module code, you would change to the following:

resource "digitalocean_kubernetes_cluster" "example_cluster" {
  name    = var.name
  region  = var.region
  version = "1.24.12-do.0"

  node_pool {
    name       = var.default_node_pool.name
    size       = var.default_node_pool.size
    node_count = var.default_node_pool.node_count
    labels     = var.default_node_pool.labels
    auto_scale = var.default_node_pool.auto_scale
    max_nodes  = var.default_node_pool.max_nodes
    min_nodes  = var.default_node_pool.min_nodes
  }
}

resource "digitalocean_kubernetes_node_pool" "bar" {
  for_each   = var.additional_node_pools
  cluster_id = digitalocean_kubernetes_cluster.example_cluster.id

  name       = each.value.name
  size       = each.value.size
  node_count = each.value.node_count
  min_nodes  = each.value.min_nodes
  max_nodes  = each.value.max_nodes
  auto_scale = each.value.auto_scale

  labels = each.value.labels
}

When calling the module you would of course have to adjust how you are passing the variables according to the module definition:

module "kubernetes" {
  source = "../modules/kubernetes"


  name   = "test-cluster"
  region = "fra1"
  # version = "1.24.12-do.0"
  default_node_pool = {
    name       = "pool-test1-8c-16gb-tests"
    size       = "s-8vcpu-16gb"
    node_count = 2
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 15
    labels = {
      runner = "test1"
    }
  }
  additional_node_pools = {
    "test1" = {
      name       = "pool-test2-4c-8gb"
      size       = "s-4vcpu-8gb"
      node_count = 1
      auto_scale = true
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = "test2"
      }
    }
  }
}

huangapple
  • 本文由 发表于 2023年5月10日 19:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定