Skip to content

Commit

Permalink
refactor(terraform): streamline Helm installation and configuration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sachincool committed Nov 5, 2024
1 parent 54ca1a2 commit 91505b9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 55 deletions.
72 changes: 26 additions & 46 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <<EOF > $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 <<EOF > $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
Expand Down
10 changes: 10 additions & 0 deletions providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 0.13.0"

required_providers {
null = {
source = "hashicorp/null"
version = ">= 3.0.0"
}
}
}
11 changes: 6 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 0 additions & 4 deletions versions.tf

This file was deleted.

0 comments on commit 91505b9

Please sign in to comment.