Dynamic node pools in digitalocean kubernetes with terraform

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

Dynamic node pools in digitalocean kubernetes with terraform

问题

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

main.tf:

  1. resource "digitalocean_kubernetes_cluster" "example_cluster" {
  2. name = var.name
  3. region = var.region
  4. version = "1.24.12-do.0"
  5. dynamic "node_pool" {
  6. for_each = var.node_pools
  7. content {
  8. name = node_pool.value.name
  9. size = node_pool.value.size
  10. node_count = node_pool.value.node_count
  11. labels = node_pool.value.labels
  12. auto_scale = "true"
  13. dynamic "auto_scale" {
  14. for_each = node_pool.value.auto_scale ? [node_pool.value.auto_scale] : []
  15. content {
  16. min_nodes = node_pool.value.min_nodes
  17. max_nodes = node_pool.value.max_nodes
  18. }
  19. }
  20. }
  21. }
  22. }

veriables.tf:

  1. variable "name" {
  2. type = string
  3. description = "The name of the Kubernetes cluster to be created."
  4. }
  5. variable "region" {
  6. type = string
  7. description = "The region in which to create the Kubernetes cluster."
  8. }
  9. variable "node_pools" {
  10. description = "List of node pools"
  11. type = list(object({
  12. name = string
  13. size = string
  14. node_count = number
  15. min_nodes = number
  16. max_nodes = number
  17. auto_scale = bool
  18. labels = map(string)
  19. }))
  20. }

my app.tf:

  1. module "kubernetes" {
  2. source = "../modules/kubernetes"
  3. name = "test-cluster"
  4. region = "fra1"
  5. node_pools = [
  6. {
  7. name = "pool-test1-8c-16gb-tests"
  8. size = "s-8vcpu-16gb"
  9. node_count = 2
  10. auto_scale = "true"
  11. min_nodes = 1
  12. max_nodes = 15
  13. labels = {
  14. runner = "test1"
  15. }
  16. },
  17. {
  18. name = "pool-test2-4c-8gb"
  19. size = "s-4vcpu-8gb"
  20. node_count = 1
  21. auto_scale = "true"
  22. min_nodes = 1
  23. max_nodes = 15
  24. labels = {
  25. runner = "test2"
  26. }
  27. }
  28. ]
  29. }

我遇到的错误是:

  1. > Error: Unsupported block type
  2. on ../modules/DO/kubernetes/main.tf line 32, in resource "digitalocean_kubernetes_cluster" "example_cluster":
  3. 32: dynamic "auto_scale" {
  4. Blocks of type "auto_scale" are not expected here.
  5. 如果我硬编码 auto_scale,我得到这个错误:
  6. >
  7. Error: Too many node_pool blocks
  8. on ../modules/DO/kubernetes/main.tf line 22, in resource "digitalocean_kubernetes_cluster" "example_cluster":
  9. 22: content {
  10. No more than 1 "node_pool" blocks are allowed
  11. 有人能指出我做错了什么吗?
英文:

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

main.tf

  1. resource "digitalocean_kubernetes_cluster" "example_cluster" {
  2. name = var.name
  3. region = var.region
  4. version = "1.24.12-do.0"
  5. dynamic "node_pool" {
  6. for_each = var.node_pools
  7. content {
  8. name = node_pool.value.name
  9. size = node_pool.value.size
  10. node_count = node_pool.value.node_count
  11. labels = node_pool.value.labels
  12. auto_scale = "true"
  13. #min_nodes = "1"
  14. #max_nodes = "15"
  15. #count = node_pool.value.auto_scale ? 1 : 0
  16. dynamic "auto_scale" {
  17. for_each = node_pool.value.auto_scale ? [node_pool.value.auto_scale] : []
  18. content {
  19. min_nodes = node_pool.value.min_nodes
  20. max_nodes = node_pool.value.max_nodes
  21. }
  22. }
  23. }
  24. }

veriables.tf

  1. variable "name" {
  2. type = string
  3. description = "The name of the Kubernetes cluster to be created."
  4. }
  5. variable "region" {
  6. type = string
  7. description = "The region in which to create the Kubernetes cluster."
  8. }
  9. #variable "version" {
  10. # description = "Version of the Kubernetes cluster"
  11. #}
  12. variable "node_pools" {
  13. description = "List of node pools"
  14. type = list(object({
  15. name = string
  16. size = string
  17. node_count = number
  18. min_nodes = number
  19. max_nodes = number
  20. auto_scale = bool
  21. labels = map(string)
  22. }))
  23. }

and my app.tf

  1. module "kubernetes" {
  2. source = "../modules/kubernetes"
  3. name = "test-cluster"
  4. region = "fra1"
  5. # version = "1.24.12-do.0"
  6. node_pools = [
  7. {
  8. name = "pool-test1-8c-16gb-tests"
  9. size = "s-8vcpu-16gb"
  10. node_count = 2
  11. auto_scale = "true"
  12. min_nodes = 1
  13. max_nodes = 15
  14. labels = {
  15. runner = "test1"
  16. }
  17. },
  18. {
  19. name = "pool-test2-4c-8gb"
  20. size = "s-4vcpu-8gb"
  21. node_count = 1
  22. auto_scale = "true"
  23. min_nodes = 1
  24. max_nodes = 15
  25. labels = {
  26. runner = "test2"
  27. }
  28. }
  29. ]
  30. }

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:

  1. resource "digitalocean_kubernetes_cluster" "foo" {
  2. name = "foo"
  3. region = "nyc1"
  4. version = "1.22.8-do.1"
  5. node_pool {
  6. name = "autoscale-worker-pool"
  7. size = "s-2vcpu-2gb"
  8. auto_scale = true
  9. min_nodes = 1
  10. max_nodes = 5
  11. }
  12. }

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:

  1. variable "default_node_pool" {
  2. type = object({
  3. name = string
  4. size = string
  5. node_count = number
  6. min_nodes = number
  7. max_nodes = number
  8. auto_scale = bool
  9. labels = map(string)
  10. }
  11. )
  12. description = "Default node pool."
  13. default = {
  14. name = "pool-test1-8c-16gb-tests"
  15. size = "s-8vcpu-16gb"
  16. node_count = 2
  17. auto_scale = true
  18. min_nodes = 1
  19. max_nodes = 15
  20. labels = {
  21. runner = "test1"
  22. }
  23. }
  24. }
  25. variable "additional_node_pools" {
  26. description = "Additional node pools."
  27. type = map(object({
  28. name = string
  29. size = string
  30. node_count = number
  31. min_nodes = number
  32. max_nodes = number
  33. auto_scale = bool
  34. labels = map(string)
  35. }
  36. ))
  37. default = {
  38. "test1" = {
  39. name = "pool-test2-4c-8gb"
  40. size = "s-4vcpu-8gb"
  41. node_count = 1
  42. auto_scale = true
  43. min_nodes = 1
  44. max_nodes = 15
  45. labels = {
  46. runner = "test2"
  47. }
  48. }
  49. }
  50. }

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

  1. resource "digitalocean_kubernetes_cluster" "example_cluster" {
  2. name = var.name
  3. region = var.region
  4. version = "1.24.12-do.0"
  5. node_pool {
  6. name = var.default_node_pool.name
  7. size = var.default_node_pool.size
  8. node_count = var.default_node_pool.node_count
  9. labels = var.default_node_pool.labels
  10. auto_scale = var.default_node_pool.auto_scale
  11. max_nodes = var.default_node_pool.max_nodes
  12. min_nodes = var.default_node_pool.min_nodes
  13. }
  14. }
  15. resource "digitalocean_kubernetes_node_pool" "bar" {
  16. for_each = var.additional_node_pools
  17. cluster_id = digitalocean_kubernetes_cluster.example_cluster.id
  18. name = each.value.name
  19. size = each.value.size
  20. node_count = each.value.node_count
  21. min_nodes = each.value.min_nodes
  22. max_nodes = each.value.max_nodes
  23. auto_scale = each.value.auto_scale
  24. labels = each.value.labels
  25. }

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

  1. module "kubernetes" {
  2. source = "../modules/kubernetes"
  3. name = "test-cluster"
  4. region = "fra1"
  5. # version = "1.24.12-do.0"
  6. default_node_pool = {
  7. name = "pool-test1-8c-16gb-tests"
  8. size = "s-8vcpu-16gb"
  9. node_count = 2
  10. auto_scale = true
  11. min_nodes = 1
  12. max_nodes = 15
  13. labels = {
  14. runner = "test1"
  15. }
  16. }
  17. additional_node_pools = {
  18. "test1" = {
  19. name = "pool-test2-4c-8gb"
  20. size = "s-4vcpu-8gb"
  21. node_count = 1
  22. auto_scale = true
  23. min_nodes = 1
  24. max_nodes = 15
  25. labels = {
  26. runner = "test2"
  27. }
  28. }
  29. }
  30. }
英文:

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:

  1. resource "digitalocean_kubernetes_cluster" "foo" {
  2. name = "foo"
  3. region = "nyc1"
  4. version = "1.22.8-do.1"
  5. node_pool {
  6. name = "autoscale-worker-pool"
  7. size = "s-2vcpu-2gb"
  8. auto_scale = true
  9. min_nodes = 1
  10. max_nodes = 5
  11. }
  12. }

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:

  1. variable "default_node_pool" {
  2. type = object({
  3. name = string
  4. size = string
  5. node_count = number
  6. min_nodes = number
  7. max_nodes = number
  8. auto_scale = bool
  9. labels = map(string)
  10. }
  11. )
  12. description = "Default node pool."
  13. default = {
  14. name = "pool-test1-8c-16gb-tests"
  15. size = "s-8vcpu-16gb"
  16. node_count = 2
  17. auto_scale = true
  18. min_nodes = 1
  19. max_nodes = 15
  20. labels = {
  21. runner = "test1"
  22. }
  23. }
  24. }
  25. variable "additional_node_pools" {
  26. description = "Additional node pools."
  27. type = map(object({
  28. name = string
  29. size = string
  30. node_count = number
  31. min_nodes = number
  32. max_nodes = number
  33. auto_scale = bool
  34. labels = map(string)
  35. }
  36. ))
  37. default = {
  38. "test1" = {
  39. name = "pool-test2-4c-8gb"
  40. size = "s-4vcpu-8gb"
  41. node_count = 1
  42. auto_scale = true
  43. min_nodes = 1
  44. max_nodes = 15
  45. labels = {
  46. runner = "test2"
  47. }
  48. }
  49. }
  50. }

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

  1. resource "digitalocean_kubernetes_cluster" "example_cluster" {
  2. name = var.name
  3. region = var.region
  4. version = "1.24.12-do.0"
  5. node_pool {
  6. name = var.default_node_pool.name
  7. size = var.default_node_pool.size
  8. node_count = var.default_node_pool.node_count
  9. labels = var.default_node_pool.labels
  10. auto_scale = var.default_node_pool.auto_scale
  11. max_nodes = var.default_node_pool.max_nodes
  12. min_nodes = var.default_node_pool.min_nodes
  13. }
  14. }
  15. resource "digitalocean_kubernetes_node_pool" "bar" {
  16. for_each = var.additional_node_pools
  17. cluster_id = digitalocean_kubernetes_cluster.example_cluster.id
  18. name = each.value.name
  19. size = each.value.size
  20. node_count = each.value.node_count
  21. min_nodes = each.value.min_nodes
  22. max_nodes = each.value.max_nodes
  23. auto_scale = each.value.auto_scale
  24. labels = each.value.labels
  25. }

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

  1. module "kubernetes" {
  2. source = "../modules/kubernetes"
  3. name = "test-cluster"
  4. region = "fra1"
  5. # version = "1.24.12-do.0"
  6. default_node_pool = {
  7. name = "pool-test1-8c-16gb-tests"
  8. size = "s-8vcpu-16gb"
  9. node_count = 2
  10. auto_scale = true
  11. min_nodes = 1
  12. max_nodes = 15
  13. labels = {
  14. runner = "test1"
  15. }
  16. }
  17. additional_node_pools = {
  18. "test1" = {
  19. name = "pool-test2-4c-8gb"
  20. size = "s-4vcpu-8gb"
  21. node_count = 1
  22. auto_scale = true
  23. min_nodes = 1
  24. max_nodes = 15
  25. labels = {
  26. runner = "test2"
  27. }
  28. }
  29. }
  30. }

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:

确定