From e44b81dfb7ef96f513624fa74d21e253f9456463 Mon Sep 17 00:00:00 2001 From: Hays Clark Date: Fri, 28 Jun 2019 00:59:23 -0700 Subject: [PATCH 1/9] Porting to support Terraform v0.12 - performed 0.12upgrade and resolved various issues - 1 TF-UPGRADE-TODO remains to be resolved Signed-off-by: Hays Clark --- README.md | 4 +++ example/main.tf | 67 +++++++++++++++++----------------- example/variables.tf | 55 ++++++++++++++-------------- iam.tf | 2 +- main.tf | 86 +++++++++++++++++++++++++++++--------------- variables.tf | 49 ++++++++++++------------- versions.tf | 4 +++ 7 files changed, 152 insertions(+), 115 deletions(-) create mode 100644 versions.tf diff --git a/README.md b/README.md index 9460273..58e3314 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,11 @@ It can be used directly from the Terraform Registry like so: ``` module "gke-cluster" { source = "jetstack/gke-cluster/google" +<<<<<<< HEAD version = "0.1.0" +======= + version = "0.2.0-alpha1" +>>>>>>> Porting to support Terraform v0.12 # insert the 9 required variables here } diff --git a/example/main.tf b/example/main.tf index 755c4da..ff9b809 100644 --- a/example/main.tf +++ b/example/main.tf @@ -19,26 +19,27 @@ terraform { required_version = "~> 0.11" # Use a GCS Bucket as a backend - backend "gcs" {} + backend "gcs" { + } } # Local values assign a name to an expression, that can then be used multiple # times within a module. They are used here to determine the GCP region from # the given location, which can be either a region or zone. locals { - gcp_location_parts = ["${split("-", var.gcp_location)}"] - gcp_region = "${local.gcp_location_parts[0]}-${local.gcp_location_parts[1]}" + gcp_location_parts = split("-", var.gcp_location) + gcp_region = format("%s-%s", local.gcp_location_parts[0], local.gcp_location_parts[1]) } # https://www.terraform.io/docs/providers/google/index.html provider "google" { version = "2.5.1" - project = "${var.gcp_project_id}" - region = "${local.gcp_region}" + project = var.gcp_project_id + region = local.gcp_region } resource "google_compute_network" "vpc_network" { - name = "${var.vpc_network_name}" + name = var.vpc_network_name auto_create_subnetworks = "false" } @@ -52,28 +53,26 @@ resource "google_compute_subnetwork" "vpc_subnetwork" { # a dash, lowercase letter, or digit, except the last character, which # cannot be a dash. #name = "default-${var.gcp_cluster_region}" - name = "${var.vpc_subnetwork_name}" + name = var.vpc_subnetwork_name - ip_cidr_range = "${var.vpc_subnetwork_cidr_range}" + ip_cidr_range = var.vpc_subnetwork_cidr_range # The network this subnet belongs to. Only networks that are in the # distributed mode can have subnetworks. - network = "${var.vpc_network_name}" + network = var.vpc_network_name # An array of configurations for secondary IP ranges for VM instances # contained in this subnetwork. The primary IP of such VM must belong to the # primary ipCidrRange of the subnetwork. The alias IPs may belong to either # primary or secondary ranges. - secondary_ip_range = [ - { - range_name = "${var.cluster_secondary_range_name}" - ip_cidr_range = "${var.cluster_secondary_range_cidr}" - }, - { - range_name = "${var.services_secondary_range_name}" - ip_cidr_range = "${var.services_secondary_range_cidr}" - }, - ] + secondary_ip_range { + range_name = var.cluster_secondary_range_name + ip_cidr_range = var.cluster_secondary_range_cidr + } + secondary_ip_range { + range_name = var.services_secondary_range_name + ip_cidr_range = var.services_secondary_range_cidr + } # When enabled, VMs in this subnetwork without external IP addresses can # access Google APIs and services by using Private Google Access. This is @@ -81,31 +80,31 @@ resource "google_compute_subnetwork" "vpc_subnetwork" { private_ip_google_access = true depends_on = [ - "google_compute_network.vpc_network", + google_compute_network.vpc_network, ] } module "cluster" { source = "jetstack/gke-cluster/google" - version = "0.1.0" + version = "0.2.0-alpha1" # These values are set from the terrafrom.tfvas file - gcp_project_id = "${var.gcp_project_id}" - cluster_name = "${var.cluster_name}" - gcp_location = "${var.gcp_location}" - daily_maintenance_window_start_time = "${var.daily_maintenance_window_start_time}" - node_pools = "${var.node_pools}" - cluster_secondary_range_name = "${var.cluster_secondary_range_name}" - services_secondary_range_name = "${var.services_secondary_range_name}" - master_ipv4_cidr_block = "${var.master_ipv4_cidr_block}" - access_private_images = "${var.access_private_images}" - http_load_balancing_disabled = "${var.http_load_balancing_disabled}" - master_authorized_networks_cidr_blocks = "${var.master_authorized_networks_cidr_blocks}" + gcp_project_id = var.gcp_project_id + cluster_name = var.cluster_name + gcp_location = var.gcp_location + daily_maintenance_window_start_time = var.daily_maintenance_window_start_time + node_pools = var.node_pools + cluster_secondary_range_name = var.cluster_secondary_range_name + services_secondary_range_name = var.services_secondary_range_name + master_ipv4_cidr_block = var.master_ipv4_cidr_block + access_private_images = var.access_private_images + http_load_balancing_disabled = var.http_load_balancing_disabled + master_authorized_networks_cidr_blocks = var.master_authorized_networks_cidr_blocks # Refer to the vpc-network and vpc-subnetwork by the name value on the # resource, rather than the variable used to assign the name, so that # Terraform knows they must be created before creating the cluster - vpc_network_name = "${google_compute_network.vpc_network.name}" - vpc_subnetwork_name = "${google_compute_subnetwork.vpc_subnetwork.name}" + vpc_network_name = google_compute_network.vpc_network.name + vpc_subnetwork_name = google_compute_subnetwork.vpc_subnetwork.name } diff --git a/example/variables.tf b/example/variables.tf index ceef3bb..a428323 100644 --- a/example/variables.tf +++ b/example/variables.tf @@ -13,7 +13,7 @@ # limitations under the License. variable "gcp_project_id" { - type = "string" + type = string description = <=0 and <= max_node_count. - min_node_count = "${lookup(var.node_pools[count.index], "autoscaling_min_node_count", 2)}" + min_node_count = lookup(var.node_pools[count.index], "autoscaling_min_node_count", 2) # Maximum number of nodes in the NodePool. Must be >= min_node_count. - max_node_count = "${lookup(var.node_pools[count.index], "autoscaling_max_node_count", 3)}" + max_node_count = lookup(var.node_pools[count.index], "autoscaling_max_node_count", 3) } # Node management configuration, wherein auto-repair and auto-upgrade is configured. - management = { + management { # Whether the nodes will be automatically repaired. - auto_repair = "${lookup(var.node_pools[count.index], "auto_repair", true)}" + auto_repair = lookup(var.node_pools[count.index], "auto_repair", true) # Whether the nodes will be automatically upgraded. - auto_upgrade = "${lookup(var.node_pools[count.index], "auto_upgrade", true)}" + auto_upgrade = lookup(var.node_pools[count.index], "auto_upgrade", true) } # Parameters used in creating the cluster's nodes. node_config { # The name of a Google Compute Engine machine type. Defaults to # n1-standard-1. - machine_type = "${lookup(var.node_pools[count.index], "node_config_machine_type", "n1-standard-1")}" + machine_type = lookup( + var.node_pools[count.index], + "node_config_machine_type", + "n1-standard-1", + ) - service_account = "${google_service_account.default.email}" + service_account = google_service_account.default.email # Size of the disk attached to each node, specified in GB. The smallest # allowed disk size is 10GB. Defaults to 100GB. - disk_size_gb = "${lookup(var.node_pools[count.index], "node_config_disk_size_gb", 100)}" + disk_size_gb = lookup( + var.node_pools[count.index], + "node_config_disk_size_gb", + 100 + ) # Type of the disk attached to each node (e.g. 'pd-standard' or 'pd-ssd'). # If unspecified, the default disk type is 'pd-standard' - disk_type = "${lookup(var.node_pools[count.index], "node_config_disk_type", "pd-standard")}" + disk_type = lookup( + var.node_pools[count.index], + "node_config_disk_type", + "pd-standard", + ) # A boolean that represents whether or not the underlying node VMs are # preemptible. See the official documentation for more information. # Defaults to false. - preemptible = "${lookup(var.node_pools[count.index], "node_config_preemptible", false)}" + preemptible = lookup( + var.node_pools[count.index], + "node_config_preemptible", + false, + ) # The set of Google API scopes to be made available on all of the node VMs # under the "default" service account. These can be either FQDNs, or scope @@ -236,3 +263,4 @@ resource "google_container_node_pool" "node_pool" { update = "20m" } } + diff --git a/variables.tf b/variables.tf index 828b6e3..184dab7 100644 --- a/variables.tf +++ b/variables.tf @@ -13,7 +13,7 @@ # limitations under the License. variable "gcp_project_id" { - type = "string" + type = string description = < Date: Fri, 5 Jul 2019 14:34:43 -0700 Subject: [PATCH 2/9] Simplifying logic after v0.12 conversion - removing TF-UPGRADE-TODO - functionality verified locally Signed-off-by: Hays Clark --- main.tf | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/main.tf b/main.tf index 50c0c80..c33c813 100644 --- a/main.tf +++ b/main.tf @@ -150,13 +150,8 @@ resource "google_container_cluster" "cluster" { dynamic "cidr_blocks" { for_each = var.master_authorized_networks_cidr_blocks content { - # TF-UPGRADE-TODO: The automatic upgrade tool can't predict - # which keys might be set in maps assigned here, so it has - # produced a comprehensive set here. Consider simplifying - # this after confirming which keys can be set in practice. - cidr_block = cidr_blocks.value.cidr_block - display_name = lookup(cidr_blocks.value, "display_name", null) + display_name = cidr_blocks.value.display_name } } } From ed3e3d4eea805d355d28167353a24a7672e09d1b Mon Sep 17 00:00:00 2001 From: Luke Addison Date: Tue, 15 Oct 2019 15:23:58 +0100 Subject: [PATCH 3/9] Address comments Signed-off-by: Luke Addison --- README.md | 4 ---- example/main.tf | 11 +++++------ main.tf | 4 ++-- variables.tf | 4 ++-- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 58e3314..7e5d12a 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,7 @@ It can be used directly from the Terraform Registry like so: ``` module "gke-cluster" { source = "jetstack/gke-cluster/google" -<<<<<<< HEAD - version = "0.1.0" -======= version = "0.2.0-alpha1" ->>>>>>> Porting to support Terraform v0.12 # insert the 9 required variables here } diff --git a/example/main.tf b/example/main.tf index ff9b809..1eb63c2 100644 --- a/example/main.tf +++ b/example/main.tf @@ -19,8 +19,7 @@ terraform { required_version = "~> 0.11" # Use a GCS Bucket as a backend - backend "gcs" { - } + backend "gcs" {} } # Local values assign a name to an expression, that can then be used multiple @@ -61,10 +60,10 @@ resource "google_compute_subnetwork" "vpc_subnetwork" { # distributed mode can have subnetworks. network = var.vpc_network_name - # An array of configurations for secondary IP ranges for VM instances - # contained in this subnetwork. The primary IP of such VM must belong to the - # primary ipCidrRange of the subnetwork. The alias IPs may belong to either - # primary or secondary ranges. + # Configurations for secondary IP ranges for VM instances contained in this + # subnetwork. The primary IP of such VM must belong to the primary ipCidrRange + # of the subnetwork. The alias IPs may belong to either primary or secondary + # ranges. secondary_ip_range { range_name = var.cluster_secondary_range_name ip_cidr_range = var.cluster_secondary_range_cidr diff --git a/main.tf b/main.tf index c33c813..d1d91fa 100644 --- a/main.tf +++ b/main.tf @@ -22,7 +22,7 @@ terraform { # This module requires a terraform version >= 0.11 but < 0.12. This is # because the module is only tested with 0.11 ,and has not yet been upgraded # to use the new 0.12 syntax. - required_version = "~> 0.11" + required_version = "~> 0.12" } # Local values assign a name to an expression, that can then be used multiple @@ -30,7 +30,7 @@ terraform { # the given location, which can be either a region or zone. locals { gcp_location_parts = split("-", var.gcp_location) - gcp_region = format("%s-%s", local.gcp_location_parts[0], local.gcp_location_parts[1]) + gcp_region = format("%s-%s", local.gcp_location_parts[0], local.gcp_location_parts[1]) } # https://www.terraform.io/docs/providers/google/index.html diff --git a/variables.tf b/variables.tf index 184dab7..1f9ec23 100644 --- a/variables.tf +++ b/variables.tf @@ -134,7 +134,7 @@ EOF } variable "master_ipv4_cidr_block" { - type = string + type = string default = "172.16.0.0/28" description = < Date: Tue, 15 Oct 2019 15:27:16 +0100 Subject: [PATCH 4/9] Require Terraform 0.12 Signed-off-by: Luke Addison --- example/main.tf | 5 +---- main.tf | 3 --- versions.tf | 4 ---- 3 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 versions.tf diff --git a/example/main.tf b/example/main.tf index 1eb63c2..d229bfe 100644 --- a/example/main.tf +++ b/example/main.tf @@ -13,10 +13,7 @@ # limitations under the License. terraform { - # This project requires a terraform version >= 0.11 but < 0.12. This is - # because the module is only tested with 0.11 ,and has not yet been upgraded - # to use the new 0.12 syntax. - required_version = "~> 0.11" + required_version = "~> 0.12" # Use a GCS Bucket as a backend backend "gcs" {} diff --git a/main.tf b/main.tf index d1d91fa..ce195ec 100644 --- a/main.tf +++ b/main.tf @@ -19,9 +19,6 @@ # variables.tf file. terraform { - # This module requires a terraform version >= 0.11 but < 0.12. This is - # because the module is only tested with 0.11 ,and has not yet been upgraded - # to use the new 0.12 syntax. required_version = "~> 0.12" } diff --git a/versions.tf b/versions.tf deleted file mode 100644 index ac97c6a..0000000 --- a/versions.tf +++ /dev/null @@ -1,4 +0,0 @@ - -terraform { - required_version = ">= 0.12" -} From b4239e30c4af3373cdddc443531f9b7ae176de3b Mon Sep 17 00:00:00 2001 From: Luke Addison Date: Tue, 15 Oct 2019 15:32:48 +0100 Subject: [PATCH 5/9] Remove support for Terraform 0.11 Signed-off-by: Luke Addison --- hack/verify.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hack/verify.sh b/hack/verify.sh index e3f5001..8fe8415 100755 --- a/hack/verify.sh +++ b/hack/verify.sh @@ -49,9 +49,7 @@ fi # Checks the Terraform version used by the module, download the Terraform binary # for that version -if grep "required_version.*0.11.*" "${REPO_ROOT}/main.tf"; then - TERRAFORM_VERSION="0.11.14" -elif grep "required_version.*0.12.*" "${REPO_ROOT}/main.tf"; then +if grep "required_version.*0.12.*" "${REPO_ROOT}/main.tf"; then TERRAFORM_VERSION="0.12.4" else echo "Terraform version is not supported or could not be found." From a968935e39784aa3ae191e5b777a86e82c2ee3eb Mon Sep 17 00:00:00 2001 From: Luke Addison Date: Tue, 15 Oct 2019 15:35:53 +0100 Subject: [PATCH 6/9] Do not fail on existing directory Signed-off-by: Luke Addison --- hack/verify.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/verify.sh b/hack/verify.sh index 8fe8415..a7a07c3 100755 --- a/hack/verify.sh +++ b/hack/verify.sh @@ -23,7 +23,7 @@ REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd )" # Make temporary directory to use for testing and enter it VERIFY_DIR="${REPO_ROOT}/verify" -mkdir "$VERIFY_DIR" +mkdir -p "$VERIFY_DIR" pushd "$VERIFY_DIR" # Determine OS type and architecture to get the correct Terraform binary. From 693d39df47f4a7baf7d5cbd047136bacbadd09d8 Mon Sep 17 00:00:00 2001 From: Luke Addison Date: Tue, 15 Oct 2019 15:36:31 +0100 Subject: [PATCH 7/9] Run terraform fmt Signed-off-by: Luke Addison --- example/variables.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/variables.tf b/example/variables.tf index a428323..3bbfd38 100644 --- a/example/variables.tf +++ b/example/variables.tf @@ -146,7 +146,7 @@ variable "services_secondary_range_cidr" { } variable "master_ipv4_cidr_block" { - type = string + type = string default = "172.16.0.0/28" description = < Date: Tue, 15 Oct 2019 15:43:41 +0100 Subject: [PATCH 8/9] Run terraform fmt Signed-off-by: Luke Addison --- variables.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/variables.tf b/variables.tf index 1f9ec23..184dab7 100644 --- a/variables.tf +++ b/variables.tf @@ -134,7 +134,7 @@ EOF } variable "master_ipv4_cidr_block" { - type = string + type = string default = "172.16.0.0/28" description = < Date: Tue, 15 Oct 2019 16:01:24 +0100 Subject: [PATCH 9/9] Change module version when targetting local path Signed-off-by: Luke Addison --- hack/verify.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/verify.sh b/hack/verify.sh index a7a07c3..e8fe159 100755 --- a/hack/verify.sh +++ b/hack/verify.sh @@ -79,7 +79,7 @@ cp "${REPO_ROOT}/example/terraform.tfvars.example" terraform.tfvars # Remove the requirement for a GCS backend so we can init and validate locally perl -i -0pe 's/(\s*)backend "gcs" \{\n?\s*\n?\s*\}/\1# GCS bucket not used for testing/gms' main.tf # Use the local version of the module, not the Terraform Registry version, and remove the version specification -perl -i -0pe 's/(\s*)source*\s*= "jetstack\/gke-cluster\/google"\n\s*version = "0.1.0-beta2"/\1source = "..\/"/gms' main.tf +perl -i -0pe 's/(\s*)source*\s*= "jetstack\/gke-cluster\/google"\n\s*version = "0.2.0-alpha1"/\1source = "..\/"/gms' main.tf # Initialise and validate the generated test project $TERRAFORM init