From 8588d503c9cb9551023a216461e8a6144234c6b6 Mon Sep 17 00:00:00 2001 From: Michael Wilkerson Date: Mon, 9 Jan 2023 10:39:44 -0800 Subject: [PATCH 1/2] added CI test to catch bad terraform formatting --- .github/workflows/test.yml | 14 ++++++++++++++ Makefile | 10 ++++++++++ .../build-support/scripts/terraformfmtcheck.sh | 14 ++++++++++++++ 3 files changed, 38 insertions(+) create mode 100755 control-plane/build-support/scripts/terraformfmtcheck.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1e5de4c98e..422e0654f5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,6 +9,20 @@ env: GOTESTSUM_VERSION: 1.8.1 # You cannot use environment variables with workflows. The gotestsum version is hardcoded in the reusable workflows too. jobs: + terraform-fmt-check: + name: "Terraform format check" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Terraform + uses: hashicorp/setup-terraform@v2 + with: + terraform_version: TERRAFORM_VERSION + terraform_wrapper: false + - name: Run Terraform checks + run: | + make terraform-fmt-check TERRAFORM_DIR="${{ github.workspace }}" + get-go-version: name: "Determine Go toolchain version" runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 43a8e16b0d..6ad59a2a91 100644 --- a/Makefile +++ b/Makefile @@ -75,6 +75,16 @@ kind-cni: kind create cluster --config=$(CURDIR)/acceptance/framework/environment/cni-kind/kind.config --name dc2 --image kindest/node:v1.23.6 make kind-cni-calico +# Perform a terraform fmt check but don't change anything +terraform-fmt-check: + @$(CURDIR)/control-plane/build-support/scripts/terraformfmtcheck.sh $(TERRAFORM_DIR) +.PHONY: terraform-fmt-check + +# Format all terraform files according to terraform fmt +terraform-fmt: + @terraform fmt -recursive +.PHONY: terraform-fmt + # ===========> CLI Targets diff --git a/control-plane/build-support/scripts/terraformfmtcheck.sh b/control-plane/build-support/scripts/terraformfmtcheck.sh new file mode 100755 index 0000000000..0f962a1c1b --- /dev/null +++ b/control-plane/build-support/scripts/terraformfmtcheck.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# Check terraform fmt +echo "==> Checking that code complies with terraform fmt requirements..." +tffmt_files=$(terraform fmt -check -recursive "$1") +if [[ -n ${tffmt_files} ]]; then + echo 'terraform fmt needs to be run on the following files:' + echo "${tffmt_files}" + echo "You can use the command: \`make terraform-fmt\` to reformat all terraform code." + exit 1 +fi + +echo "==> Check code compile completed successfully" +exit 0 \ No newline at end of file From a2b6b241d7714e9c9f37d19cb7f38bb12ef2b533 Mon Sep 17 00:00:00 2001 From: Michael Wilkerson Date: Mon, 9 Jan 2023 10:39:58 -0800 Subject: [PATCH 2/2] formatted terraform files --- charts/consul/test/terraform/aks/variables.tf | 2 +- charts/consul/test/terraform/eks/variables.tf | 2 +- charts/consul/test/terraform/gke/variables.tf | 2 +- charts/consul/test/terraform/openshift/variables.tf | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/charts/consul/test/terraform/aks/variables.tf b/charts/consul/test/terraform/aks/variables.tf index 1651ce7b09..bb9dbef537 100644 --- a/charts/consul/test/terraform/aks/variables.tf +++ b/charts/consul/test/terraform/aks/variables.tf @@ -27,7 +27,7 @@ variable "cluster_count" { } variable "tags" { - type = map + type = map(any) default = {} description = "Tags to attach to the created resources." } diff --git a/charts/consul/test/terraform/eks/variables.tf b/charts/consul/test/terraform/eks/variables.tf index 361a5f5c45..05f383168b 100644 --- a/charts/consul/test/terraform/eks/variables.tf +++ b/charts/consul/test/terraform/eks/variables.tf @@ -21,7 +21,7 @@ variable "role_arn" { } variable "tags" { - type = map + type = map(any) default = {} description = "Tags to attach to the created resources." } diff --git a/charts/consul/test/terraform/gke/variables.tf b/charts/consul/test/terraform/gke/variables.tf index 04d214cedb..ef4a429116 100644 --- a/charts/consul/test/terraform/gke/variables.tf +++ b/charts/consul/test/terraform/gke/variables.tf @@ -30,7 +30,7 @@ variable "cluster_count" { } variable "labels" { - type = map + type = map(any) default = {} description = "Labels to attach to the created resources." } diff --git a/charts/consul/test/terraform/openshift/variables.tf b/charts/consul/test/terraform/openshift/variables.tf index f2479e3229..1df518f8ed 100644 --- a/charts/consul/test/terraform/openshift/variables.tf +++ b/charts/consul/test/terraform/openshift/variables.tf @@ -9,7 +9,7 @@ variable "cluster_count" { } variable "tags" { - type = map + type = map(any) default = {} description = "Tags to attach to the created resources." }