From 91505b9b6d98de56df3469ae894cc1bd876582e1 Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Wed, 6 Nov 2024 02:31:55 +0530 Subject: [PATCH] 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. --- main.tf | 72 +++++++++++++++++++--------------------------------- providers.tf | 10 ++++++++ variables.tf | 11 ++++---- versions.tf | 4 --- 4 files changed, 42 insertions(+), 55 deletions(-) create mode 100644 providers.tf delete mode 100644 versions.tf 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 -}