Skip to content

Commit

Permalink
refactor(terraform): streamline Helm installation and configuration (#5)
Browse files Browse the repository at this point in the history
* 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] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
sachincool and github-actions[bot] authored Nov 6, 2024
1 parent 54ca1a2 commit f23c745
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 61 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ This module is released under the MIT License. See the [LICENSE](./LICENSE) file

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.0.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | >= 3.0.0 |

## Modules

Expand All @@ -77,17 +77,15 @@ 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

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_chart_name"></a> [chart\_name](#input\_chart\_name) | Name of the chart | `string` | n/a | yes |
| <a name="input_chart_version"></a> [chart\_version](#input\_chart\_version) | Version of the chart | `string` | n/a | yes |
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the cluster | `string` | n/a | yes |
| <a name="input_create_namespace"></a> [create\_namespace](#input\_create\_namespace) | Create the namespace if it does not exist. Defaults to false | `bool` | `false` | no |
| <a name="input_kubeconfig_json"></a> [kubeconfig\_json](#input\_kubeconfig\_json) | Kubeconfig JSON | `string` | n/a | yes |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Namespace to install the chart | `string` | n/a | yes |
| <a name="input_release_name"></a> [release\_name](#input\_release\_name) | Release name of the chart | `string` | n/a | yes |
| <a name="input_repo_name"></a> [repo\_name](#input\_repo\_name) | Name of the Helm repository | `string` | n/a | yes |
Expand Down
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 f23c745

Please sign in to comment.