Skip to content

Commit

Permalink
Added cluster autoscaling - optimized input variable
Browse files Browse the repository at this point in the history
  • Loading branch information
kopachevsky committed Nov 21, 2019
1 parent 1e21211 commit b51739b
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 67 deletions.
2 changes: 1 addition & 1 deletion autogen/cluster.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resource "google_container_cluster" "primary" {

{% if beta_cluster %}
cluster_autoscaling {
enabled = var.cluster_autoscaling.enabled
enabled = local.autoscalling_enabled
dynamic "resource_limits" {
for_each = local.autoscalling_resource_limits
content {
Expand Down
8 changes: 5 additions & 3 deletions autogen/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,22 @@ locals {
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
{% if beta_cluster %}
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
limits = var.cluster_autoscaling.resource_limits
limits = var.cluster_autoscaling

autoscalling_resource_limits = concat(
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
resource_type = "cpu"
minimum = local.limits["min_cpu_cores"]
maximum = local.limits["max_cpu_cores"]
}] : [],
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
resource_type = "memory"
minimum = local.limits["min_memory_gb"]
maximum = local.limits["max_memory_gb"]
}] : []
)

autoscalling_enabled = length(local.autoscalling_resource_limits) > 0
{% endif %}


Expand Down
10 changes: 2 additions & 8 deletions autogen/variables.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,8 @@ variable "node_pools_metadata" {
{% if beta_cluster %}

variable "cluster_autoscaling" {
type = object({
enabled = bool
resource_limits = map(number)
})
default = {
enabled = false
resource_limits = {}
}
type = map(number)
default = {}
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/node_pool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This example illustrates how to create a cluster with multiple custom node-pool

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
| cluster\_name\_suffix | A suffix to append to the default cluster name | string | `""` | no |
| compute\_engine\_service\_account | Service account to associate to the nodes in the cluster | string | n/a | yes |
| ip\_range\_pods | The secondary ip range to use for pods | string | n/a | yes |
Expand Down
10 changes: 2 additions & 8 deletions examples/node_pool/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ variable "compute_engine_service_account" {
}

variable "cluster_autoscaling" {
type = object({
enabled = bool
resource_limits = map(number)
})
default = {
enabled = false
resource_limits = {}
}
type = map(number)
default = {}
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
}
2 changes: 1 addition & 1 deletion modules/beta-private-cluster-update-variant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
| basic\_auth\_username | The username to be used with Basic Authentication. An empty value will disable Basic Authentication, which is the recommended configuration. | string | `""` | no |
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
| configure\_ip\_masq | Enables the installation of ip masquerading, which is usually no longer required when using aliasied IP addresses. IP masquerading uses a kubectl call, so when you have a private cluster, you will need access to the API server. | string | `"false"` | no |
Expand Down
2 changes: 1 addition & 1 deletion modules/beta-private-cluster-update-variant/cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ resource "google_container_cluster" "primary" {
monitoring_service = var.monitoring_service

cluster_autoscaling {
enabled = var.cluster_autoscaling.enabled
enabled = local.autoscalling_enabled
dynamic "resource_limits" {
for_each = local.autoscalling_resource_limits
content {
Expand Down
8 changes: 5 additions & 3 deletions modules/beta-private-cluster-update-variant/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ locals {
master_version = var.regional ? local.master_version_regional : local.master_version_zonal
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
limits = var.cluster_autoscaling.resource_limits
limits = var.cluster_autoscaling

autoscalling_resource_limits = concat(
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
resource_type = "cpu"
minimum = local.limits["min_cpu_cores"]
maximum = local.limits["max_cpu_cores"]
}] : [],
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
resource_type = "memory"
minimum = local.limits["min_memory_gb"]
maximum = local.limits["max_memory_gb"]
}] : []
)

autoscalling_enabled = length(local.autoscalling_resource_limits) > 0


custom_kube_dns_config = length(keys(var.stub_domains)) > 0
upstream_nameservers_config = length(var.upstream_nameservers) > 0
Expand Down
10 changes: 2 additions & 8 deletions modules/beta-private-cluster-update-variant/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,8 @@ variable "node_pools_metadata" {
}

variable "cluster_autoscaling" {
type = object({
enabled = bool
resource_limits = map(number)
})
default = {
enabled = false
resource_limits = {}
}
type = map(number)
default = {}
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
}

Expand Down
2 changes: 1 addition & 1 deletion modules/beta-private-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
| basic\_auth\_username | The username to be used with Basic Authentication. An empty value will disable Basic Authentication, which is the recommended configuration. | string | `""` | no |
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
| configure\_ip\_masq | Enables the installation of ip masquerading, which is usually no longer required when using aliasied IP addresses. IP masquerading uses a kubectl call, so when you have a private cluster, you will need access to the API server. | string | `"false"` | no |
Expand Down
2 changes: 1 addition & 1 deletion modules/beta-private-cluster/cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ resource "google_container_cluster" "primary" {
monitoring_service = var.monitoring_service

cluster_autoscaling {
enabled = var.cluster_autoscaling.enabled
enabled = local.autoscalling_enabled
dynamic "resource_limits" {
for_each = local.autoscalling_resource_limits
content {
Expand Down
8 changes: 5 additions & 3 deletions modules/beta-private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ locals {
master_version = var.regional ? local.master_version_regional : local.master_version_zonal
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
limits = var.cluster_autoscaling.resource_limits
limits = var.cluster_autoscaling

autoscalling_resource_limits = concat(
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
resource_type = "cpu"
minimum = local.limits["min_cpu_cores"]
maximum = local.limits["max_cpu_cores"]
}] : [],
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
resource_type = "memory"
minimum = local.limits["min_memory_gb"]
maximum = local.limits["max_memory_gb"]
}] : []
)

autoscalling_enabled = length(local.autoscalling_resource_limits) > 0


custom_kube_dns_config = length(keys(var.stub_domains)) > 0
upstream_nameservers_config = length(var.upstream_nameservers) > 0
Expand Down
10 changes: 2 additions & 8 deletions modules/beta-private-cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,8 @@ variable "node_pools_metadata" {
}

variable "cluster_autoscaling" {
type = object({
enabled = bool
resource_limits = map(number)
})
default = {
enabled = false
resource_limits = {}
}
type = map(number)
default = {}
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
}

Expand Down
2 changes: 1 addition & 1 deletion modules/beta-public-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
| basic\_auth\_password | The password to be used with Basic Authentication. | string | `""` | no |
| basic\_auth\_username | The username to be used with Basic Authentication. An empty value will disable Basic Authentication, which is the recommended configuration. | string | `""` | no |
| cloudrun | (Beta) Enable CloudRun addon | string | `"false"` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | object | `<map>` | no |
| cluster\_autoscaling | Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling) | map(number) | `<map>` | no |
| cluster\_ipv4\_cidr | The IP address range of the kubernetes pods in this cluster. Default is an automatically assigned CIDR. | string | `""` | no |
| cluster\_resource\_labels | The GCE resource labels (a map of key/value pairs) to be applied to the cluster | map(string) | `<map>` | no |
| configure\_ip\_masq | Enables the installation of ip masquerading, which is usually no longer required when using aliasied IP addresses. IP masquerading uses a kubectl call, so when you have a private cluster, you will need access to the API server. | string | `"false"` | no |
Expand Down
2 changes: 1 addition & 1 deletion modules/beta-public-cluster/cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ resource "google_container_cluster" "primary" {
monitoring_service = var.monitoring_service

cluster_autoscaling {
enabled = var.cluster_autoscaling.enabled
enabled = local.autoscalling_enabled
dynamic "resource_limits" {
for_each = local.autoscalling_resource_limits
content {
Expand Down
8 changes: 5 additions & 3 deletions modules/beta-public-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ locals {
master_version = var.regional ? local.master_version_regional : local.master_version_zonal
node_version = var.regional ? local.node_version_regional : local.node_version_zonal
release_channel = var.release_channel != null ? [{ channel : var.release_channel }] : []
limits = var.cluster_autoscaling.resource_limits
limits = var.cluster_autoscaling

autoscalling_resource_limits = concat(
var.cluster_autoscaling.enabled && lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
lookup(local.limits, "max_cpu_cores", 0) > lookup(local.limits, "min_cpu_cores", 0) ? [{
resource_type = "cpu"
minimum = local.limits["min_cpu_cores"]
maximum = local.limits["max_cpu_cores"]
}] : [],
var.cluster_autoscaling.enabled && lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
lookup(local.limits, "max_memory_gb", 0) > lookup(local.limits, "min_memory_gb", 0) ? [{
resource_type = "memory"
minimum = local.limits["min_memory_gb"]
maximum = local.limits["max_memory_gb"]
}] : []
)

autoscalling_enabled = length(local.autoscalling_resource_limits) > 0


custom_kube_dns_config = length(keys(var.stub_domains)) > 0
upstream_nameservers_config = length(var.upstream_nameservers) > 0
Expand Down
10 changes: 2 additions & 8 deletions modules/beta-public-cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,8 @@ variable "node_pools_metadata" {
}

variable "cluster_autoscaling" {
type = object({
enabled = bool
resource_limits = map(number)
})
default = {
enabled = false
resource_limits = {}
}
type = map(number)
default = {}
description = "Cluster autoscaling configuration. See [more details](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#clusterautoscaling)"
}

Expand Down
11 changes: 4 additions & 7 deletions test/fixtures/node_pool/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ module "example" {
compute_engine_service_account = var.compute_engine_service_account

cluster_autoscaling = {
enabled = true
resource_limits = {
max_cpu_cores = 20
min_cpu_cores = 5
max_memory_gb = 30
min_memory_gb = 10
}
max_cpu_cores = 20
min_cpu_cores = 5
max_memory_gb = 30
min_memory_gb = 10
}
}

0 comments on commit b51739b

Please sign in to comment.