From fd3ebc13f7306f05ea5b89b5f448962bf41083fa Mon Sep 17 00:00:00 2001
From: Cristian Klein <cristian@kleinlabs.eu>
Date: Wed, 23 Dec 2020 14:08:26 +0100
Subject: [PATCH] Fix terraform0.13 errors (#7077)

* [terraform/aws] Fix Terraform >=0.13 warnings

Terraform >=0.13 gives the following warning:

```
Warning: Interpolation-only expressions are deprecated
```

The fix was tested as follows:
```
rm -rf .terraform && terraform0.12.26 init && terraform0.12.26 validate
rm -rf .terraform && terraform0.13.5 init && terraform0.13.5 validate
rm -rf .terraform && terraform0.14.3 init && terraform0.14.3 validate
```
which gave no errors nor warnings.

* [terraform/openstack] Fixes for Terraform >=0.13

Terraform >=0.13 gives the following error:
```
Error: Failed to install providers
Could not find required providers, but found possible alternatives:
  hashicorp/openstack -> terraform-provider-openstack/openstack
```

This patch fixes these errors.

This fix was tested as follows:
```
rm -rf .terraform && terraform0.12.26 init && terraform0.12.26 validate
rm -rf .terraform && terraform0.13.5 init && terraform0.13.5 validate
rm -rf .terraform && terraform0.14.3 init && terraform0.14.3 validate
```
which gave no errors nor warnings for Terraform 0.13.5 and Terraform
0.14.3. Unfortunately, 0.12.x gives a harmless warning, but
with 0.14.3 out the door, I guess we need to move on.

* [terraform/packet] Fixes for Terraform >=0.13

This fix was tested as follows:
```
export PACKET_AUTH_TOKEN=blah-blah
rm -rf .terraform && terraform0.12.26 init && terraform0.12.26 validate
rm -rf .terraform && terraform0.13.5 init && terraform0.13.5 validate
rm -rf .terraform && terraform0.14.3 init && terraform0.14.3 validate
```

Errors are gone, but warnings still remain. It is impossible to please
all three versions of Terraform.

* Add tests for Terraform >=0.13
---
 .gitlab-ci/terraform.yml                      | 42 +++++++++++++++++++
 .../terraform/aws/create-infrastructure.tf    |  2 +-
 .../terraform/aws/modules/elb/variables.tf    |  6 +--
 .../terraform/aws/modules/vpc/variables.tf    |  8 ++--
 contrib/terraform/aws/variables.tf            |  6 +--
 contrib/terraform/openstack/kubespray.tf      |  4 --
 .../openstack/modules/compute/versions.tf     |  8 ++++
 .../openstack/modules/ips/versions.tf         | 11 +++++
 .../openstack/modules/network/versions.tf     |  8 ++++
 contrib/terraform/openstack/variables.tf      |  6 +--
 contrib/terraform/openstack/versions.tf       |  9 ++++
 contrib/terraform/packet/versions.tf          |  5 +++
 12 files changed, 97 insertions(+), 18 deletions(-)
 create mode 100644 contrib/terraform/openstack/modules/compute/versions.tf
 create mode 100644 contrib/terraform/openstack/modules/ips/versions.tf
 create mode 100644 contrib/terraform/openstack/modules/network/versions.tf
 create mode 100644 contrib/terraform/openstack/versions.tf

diff --git a/.gitlab-ci/terraform.yml b/.gitlab-ci/terraform.yml
index 3ab6d6363..d2ec67c69 100644
--- a/.gitlab-ci/terraform.yml
+++ b/.gitlab-ci/terraform.yml
@@ -74,6 +74,48 @@ tf-validate-aws:
     PROVIDER: aws
     CLUSTER: $CI_COMMIT_REF_NAME
 
+tf-0.13.x-validate-openstack:
+  extends: .terraform_validate
+  variables:
+    TF_VERSION: 0.13.5
+    PROVIDER: openstack
+    CLUSTER: $CI_COMMIT_REF_NAME
+
+tf-0.13.x-validate-packet:
+  extends: .terraform_validate
+  variables:
+    TF_VERSION: 0.13.5
+    PROVIDER: packet
+    CLUSTER: $CI_COMMIT_REF_NAME
+
+tf-0.13.x-validate-aws:
+  extends: .terraform_validate
+  variables:
+    TF_VERSION: 0.13.5
+    PROVIDER: aws
+    CLUSTER: $CI_COMMIT_REF_NAME
+
+tf-0.14.x-validate-openstack:
+  extends: .terraform_validate
+  variables:
+    TF_VERSION: 0.14.3
+    PROVIDER: openstack
+    CLUSTER: $CI_COMMIT_REF_NAME
+
+tf-0.14.x-validate-packet:
+  extends: .terraform_validate
+  variables:
+    TF_VERSION: 0.14.3
+    PROVIDER: packet
+    CLUSTER: $CI_COMMIT_REF_NAME
+
+tf-0.14.x-validate-aws:
+  extends: .terraform_validate
+  variables:
+    TF_VERSION: 0.14.3
+    PROVIDER: aws
+    CLUSTER: $CI_COMMIT_REF_NAME
+
 # tf-packet-ubuntu16-default:
 #   extends: .terraform_apply
 #   variables:
diff --git a/contrib/terraform/aws/create-infrastructure.tf b/contrib/terraform/aws/create-infrastructure.tf
index 2d9850d4d..72aa27c79 100644
--- a/contrib/terraform/aws/create-infrastructure.tf
+++ b/contrib/terraform/aws/create-infrastructure.tf
@@ -63,7 +63,7 @@ resource "aws_instance" "bastion-server" {
 
   tags = merge(var.default_tags, map(
     "Name", "kubernetes-${var.aws_cluster_name}-bastion-${count.index}",
-    "Cluster", "${var.aws_cluster_name}",
+    "Cluster", var.aws_cluster_name,
     "Role", "bastion-${var.aws_cluster_name}-${count.index}"
   ))
 }
diff --git a/contrib/terraform/aws/modules/elb/variables.tf b/contrib/terraform/aws/modules/elb/variables.tf
index 4395e7132..ca56b1a92 100644
--- a/contrib/terraform/aws/modules/elb/variables.tf
+++ b/contrib/terraform/aws/modules/elb/variables.tf
@@ -16,15 +16,15 @@ variable "k8s_secure_api_port" {
 
 variable "aws_avail_zones" {
   description = "Availability Zones Used"
-  type        = "list"
+  type        = list(string)
 }
 
 variable "aws_subnet_ids_public" {
   description = "IDs of Public Subnets"
-  type        = "list"
+  type        = list(string)
 }
 
 variable "default_tags" {
   description = "Tags for all resources"
-  type        = "map"
+  type        = map(string)
 }
diff --git a/contrib/terraform/aws/modules/vpc/variables.tf b/contrib/terraform/aws/modules/vpc/variables.tf
index fea460ce5..de058a180 100644
--- a/contrib/terraform/aws/modules/vpc/variables.tf
+++ b/contrib/terraform/aws/modules/vpc/variables.tf
@@ -8,20 +8,20 @@ variable "aws_cluster_name" {
 
 variable "aws_avail_zones" {
   description = "AWS Availability Zones Used"
-  type        = "list"
+  type        = list(string)
 }
 
 variable "aws_cidr_subnets_private" {
   description = "CIDR Blocks for private subnets in Availability zones"
-  type        = "list"
+  type        = list(string)
 }
 
 variable "aws_cidr_subnets_public" {
   description = "CIDR Blocks for public subnets in Availability zones"
-  type        = "list"
+  type        = list(string)
 }
 
 variable "default_tags" {
   description = "Default tags for all resources"
-  type        = "map"
+  type        = map(string)
 }
diff --git a/contrib/terraform/aws/variables.tf b/contrib/terraform/aws/variables.tf
index 84635edae..19d165f04 100644
--- a/contrib/terraform/aws/variables.tf
+++ b/contrib/terraform/aws/variables.tf
@@ -44,12 +44,12 @@ variable "aws_vpc_cidr_block" {
 
 variable "aws_cidr_subnets_private" {
   description = "CIDR Blocks for private subnets in Availability Zones"
-  type        = "list"
+  type        = list(string)
 }
 
 variable "aws_cidr_subnets_public" {
   description = "CIDR Blocks for public subnets in Availability Zones"
-  type        = "list"
+  type        = list(string)
 }
 
 //AWS EC2 Settings
@@ -101,7 +101,7 @@ variable "k8s_secure_api_port" {
 
 variable "default_tags" {
   description = "Default tags for all resources"
-  type        = "map"
+  type        = map(string)
 }
 
 variable "inventory_file" {
diff --git a/contrib/terraform/openstack/kubespray.tf b/contrib/terraform/openstack/kubespray.tf
index 7628996cc..25613f438 100644
--- a/contrib/terraform/openstack/kubespray.tf
+++ b/contrib/terraform/openstack/kubespray.tf
@@ -1,7 +1,3 @@
-provider "openstack" {
-  version = "~> 1.17"
-}
-
 module "network" {
   source = "./modules/network"
 
diff --git a/contrib/terraform/openstack/modules/compute/versions.tf b/contrib/terraform/openstack/modules/compute/versions.tf
new file mode 100644
index 000000000..6c942790d
--- /dev/null
+++ b/contrib/terraform/openstack/modules/compute/versions.tf
@@ -0,0 +1,8 @@
+terraform {
+  required_providers {
+    openstack = {
+      source = "terraform-provider-openstack/openstack"
+    }
+  }
+  required_version = ">= 0.12.26"
+}
diff --git a/contrib/terraform/openstack/modules/ips/versions.tf b/contrib/terraform/openstack/modules/ips/versions.tf
new file mode 100644
index 000000000..b7bf5a9cd
--- /dev/null
+++ b/contrib/terraform/openstack/modules/ips/versions.tf
@@ -0,0 +1,11 @@
+terraform {
+  required_providers {
+    null = {
+      source = "hashicorp/null"
+    }
+    openstack = {
+      source = "terraform-provider-openstack/openstack"
+    }
+  }
+  required_version = ">= 0.12.26"
+}
diff --git a/contrib/terraform/openstack/modules/network/versions.tf b/contrib/terraform/openstack/modules/network/versions.tf
new file mode 100644
index 000000000..6c942790d
--- /dev/null
+++ b/contrib/terraform/openstack/modules/network/versions.tf
@@ -0,0 +1,8 @@
+terraform {
+  required_providers {
+    openstack = {
+      source = "terraform-provider-openstack/openstack"
+    }
+  }
+  required_version = ">= 0.12.26"
+}
diff --git a/contrib/terraform/openstack/variables.tf b/contrib/terraform/openstack/variables.tf
index 48b2ea88d..c608b2523 100644
--- a/contrib/terraform/openstack/variables.tf
+++ b/contrib/terraform/openstack/variables.tf
@@ -152,7 +152,7 @@ variable "subnet_cidr" {
 
 variable "dns_nameservers" {
   description = "An array of DNS name server names used by hosts in this subnet."
-  type        = list
+  type        = list(string)
   default     = []
 }
 
@@ -211,13 +211,13 @@ variable "k8s_allowed_egress_ips" {
 }
 
 variable "master_allowed_ports" {
-  type = list
+  type = list(any)
 
   default = []
 }
 
 variable "worker_allowed_ports" {
-  type = list
+  type = list(any)
 
   default = [
     {
diff --git a/contrib/terraform/openstack/versions.tf b/contrib/terraform/openstack/versions.tf
new file mode 100644
index 000000000..9541063a2
--- /dev/null
+++ b/contrib/terraform/openstack/versions.tf
@@ -0,0 +1,9 @@
+terraform {
+  required_providers {
+    openstack = {
+      source  = "terraform-provider-openstack/openstack"
+      version = "~> 1.17"
+    }
+  }
+  required_version = ">= 0.12.26"
+}
diff --git a/contrib/terraform/packet/versions.tf b/contrib/terraform/packet/versions.tf
index ac97c6ac8..d222f2bdd 100644
--- a/contrib/terraform/packet/versions.tf
+++ b/contrib/terraform/packet/versions.tf
@@ -1,4 +1,9 @@
 
 terraform {
   required_version = ">= 0.12"
+  required_providers {
+    packet = {
+      source = "terraform-providers/packet"
+    }
+  }
 }
-- 
GitLab