From f23c74574b7cfc03e2b006a172b55c36fba294d0 Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Wed, 6 Nov 2024 14:14:01 +0530 Subject: [PATCH] refactor(terraform): streamline Helm installation and configuration (#5) * refactor(terraform): streamline Helm installation and configuration Consolidate Helm command logic into a local variable for clarity. Remove redundant EKS data sources and use kubeconfig JSON directly. Add providers.tf for managing provider versions. Remove versions.tf to centralize version management. Enhance temporary file handling and cleanup process for better resource management. * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] --- README.md | 10 +++----- main.tf | 72 +++++++++++++++++++--------------------------------- providers.tf | 10 ++++++++ variables.tf | 11 ++++---- versions.tf | 4 --- 5 files changed, 46 insertions(+), 61 deletions(-) create mode 100644 providers.tf delete mode 100644 versions.tf diff --git a/README.md b/README.md index 2cc7dc6..4d39dea 100644 --- a/README.md +++ b/README.md @@ -59,14 +59,14 @@ This module is released under the MIT License. See the [LICENSE](./LICENSE) file | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.13 | +| [terraform](#requirement\_terraform) | >= 0.13.0 | +| [null](#requirement\_null) | >= 3.0.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | n/a | -| [null](#provider\_null) | n/a | +| [null](#provider\_null) | >= 3.0.0 | ## Modules @@ -77,8 +77,6 @@ No modules. | Name | Type | |------|------| | [null_resource.helm_install](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource | -| [aws_eks_cluster.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster) | data source | -| [aws_eks_cluster_auth.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source | ## Inputs @@ -86,8 +84,8 @@ No modules. |------|-------------|------|---------|:--------:| | [chart\_name](#input\_chart\_name) | Name of the chart | `string` | n/a | yes | | [chart\_version](#input\_chart\_version) | Version of the chart | `string` | n/a | yes | -| [cluster\_name](#input\_cluster\_name) | Name of the cluster | `string` | n/a | yes | | [create\_namespace](#input\_create\_namespace) | Create the namespace if it does not exist. Defaults to false | `bool` | `false` | no | +| [kubeconfig\_json](#input\_kubeconfig\_json) | Kubeconfig JSON | `string` | n/a | yes | | [namespace](#input\_namespace) | Namespace to install the chart | `string` | n/a | yes | | [release\_name](#input\_release\_name) | Release name of the chart | `string` | n/a | yes | | [repo\_name](#input\_repo\_name) | Name of the Helm repository | `string` | n/a | yes | diff --git a/main.tf b/main.tf index 7e464be..1c1d7ac 100644 --- a/main.tf +++ b/main.tf @@ -1,12 +1,17 @@ -data "aws_eks_cluster_auth" "cluster" { - name = var.cluster_name -} -data "aws_eks_cluster" "cluster" { - name = var.cluster_name +locals { + # Helm command configuration + helm_command = <<-EOT + helm upgrade --install ${var.release_name} ${var.repo_name}/${var.chart_name} \ + --version ${var.chart_version} \ + --namespace ${var.namespace} \ + ${var.create_namespace ? "--create-namespace" : ""} \ + -f $VALUES_FILE \ + --debug + EOT } - +# Main resource for Helm installation resource "null_resource" "helm_install" { triggers = { chart_name = var.chart_name @@ -20,60 +25,35 @@ resource "null_resource" "helm_install" { command = <<-EOT echo "Starting Helm install process..." - # Create a temporary kubeconfig file + # Create temporary files export KUBECONFIG=$(mktemp) - echo "Created temporary KUBECONFIG file: $KUBECONFIG" + VALUES_FILE=$(mktemp) + echo "Created temporary files: KUBECONFIG=$KUBECONFIG, VALUES_FILE=$VALUES_FILE" - # Write the kubeconfig content + # Generate kubeconfig cat < $KUBECONFIG - apiVersion: v1 - kind: Config - clusters: - - cluster: - server: ${data.aws_eks_cluster.cluster.endpoint} - certificate-authority-data: ${data.aws_eks_cluster.cluster.certificate_authority[0].data} - name: kubernetes - contexts: - - context: - cluster: kubernetes - user: aws - name: aws - current-context: aws - users: - - name: aws - user: - token: ${data.aws_eks_cluster_auth.cluster.token} + ${var.kubeconfig_json} EOF - echo "Wrote kubeconfig content to $KUBECONFIG" - - # Create a temporary values file - VALUES_FILE=$(mktemp) - echo "Created temporary values file: $VALUES_FILE" + echo "Generated kubeconfig file" - # Write the values content + # Generate values file cat < $VALUES_FILE ${jsonencode(var.set_values)} EOF - echo "Wrote values content to $VALUES_FILE" + echo "Generated values file" - # Run Helm command with the temporary kubeconfig and values file - echo "Running Helm command..." + # Execute Helm commands + echo "Executing Helm commands..." helm repo add ${var.repo_name} ${var.repo_url} helm repo update - helm upgrade --install ${var.release_name} ${var.repo_name}/${var.chart_name} \ - --version ${var.chart_version} \ - --namespace ${var.namespace} \ - ${var.create_namespace ? "--create-namespace" : ""} \ - -f $VALUES_FILE \ - --debug + ${local.helm_command} HELM_EXIT_CODE=$? - echo "Helm command exited with code: $HELM_EXIT_CODE" + echo "Helm command completed with exit code: $HELM_EXIT_CODE" - # Clean up the temporary files - rm $KUBECONFIG - rm $VALUES_FILE - echo "Removed temporary KUBECONFIG and values files" + # Cleanup + rm $KUBECONFIG $VALUES_FILE + echo "Cleaned up temporary files" exit $HELM_EXIT_CODE EOT diff --git a/providers.tf b/providers.tf new file mode 100644 index 0000000..f2580b1 --- /dev/null +++ b/providers.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 0.13.0" + + required_providers { + null = { + source = "hashicorp/null" + version = ">= 3.0.0" + } + } +} diff --git a/variables.tf b/variables.tf index 8913bfe..3204142 100644 --- a/variables.tf +++ b/variables.tf @@ -34,18 +34,19 @@ variable "repo_url" { description = "URL of the Helm repository" } -variable "cluster_name" { - type = string - description = "Name of the cluster" -} - variable "set_values" { type = any description = "A map of values to pass to the Helm chart" default = {} } + variable "trigger_helm_update" { description = "Set this to true value trigger a Helm chart update" type = bool default = false } + +variable "kubeconfig_json" { + description = "Kubeconfig JSON" + type = string +} diff --git a/versions.tf b/versions.tf deleted file mode 100644 index ab85202..0000000 --- a/versions.tf +++ /dev/null @@ -1,4 +0,0 @@ -terraform { - required_version = ">= 0.13" - # Remove the required_providers block if you're not using any providers in this module -}