Skip to content

Commit

Permalink
add main.tf and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sachincool committed Oct 22, 2024
1 parent 5ef9572 commit 0391057
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 11 deletions.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
# terraform-kubernetes-helm
# Terraform Helm Chart Installation Module

This Terraform module provides a flexible way to install Helm charts on a Kubernetes cluster. It uses a `null_resource` with a `local-exec` provisioner to run Helm commands, allowing for dynamic chart installation and updates.

## Features

- Installs or upgrades Helm charts
- Supports custom repositories
- Allows for namespace creation
- Configurable chart values
- Uses temporary files for secure kubeconfig and values handling

## Requirements

- Terraform >= 0.13
- Helm (installed on the machine running Terraform)
- Access to a Kubernetes cluster

## Input Variables

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| `chart_name` | Name of the Helm chart to install | `string` | n/a | yes |
| `chart_version` | Version of the Helm chart to install | `string` | n/a | yes |
| `release_name` | Name for the Helm release | `string` | n/a | yes |
| `namespace` | Kubernetes namespace to install the release into | `string` | n/a | yes |
| `create_namespace` | Whether to create the namespace if it doesn't exist | `bool` | `false` | no |
| `repo_name` | Name of the Helm repository | `string` | n/a | yes |
| `repo_url` | URL of the Helm repository | `string` | n/a | yes |
| `cluster_ca_certificate` | Base64 encoded CA certificate of the Kubernetes cluster | `string` | n/a | yes |
| `cluster_endpoint` | Endpoint of the Kubernetes cluster | `string` | n/a | yes |
| `token` | Authentication token for the Kubernetes cluster | `string` | n/a | yes |
| `set_values` | Map of values to pass to the Helm chart | `any` | `{}` | no |

## How it works

1. The module creates temporary files for the kubeconfig and chart values.
2. It then uses these temporary files to run Helm commands via a `local-exec` provisioner.
3. The Helm repository is added and updated.
4. The chart is installed or upgraded using the provided values.
5. Temporary files are cleaned up after the Helm command execution.

## Notes

- Ensure that the machine running Terraform has Helm installed and configured.
- The module uses a `null_resource` with a `local-exec` provisioner, which means the Helm commands are executed on the machine running Terraform, not within Terraform itself.
- Be cautious with sensitive information in `set_values`. While this module uses temporary files, it's generally a good practice to manage secrets separately.

## Contributing

Contributions to improve this module are welcome. Please submit a pull request or open an issue on the repository.

## License

This module is released under the MIT License. See the [LICENSE](./LICENSE) file for more details.
72 changes: 72 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
resource "null_resource" "helm_install" {
triggers = {
chart_name = var.chart_name
chart_version = var.chart_version
release_name = var.release_name
namespace = var.namespace
always_run = "${timestamp()}"
}

provisioner "local-exec" {
command = <<-EOT
echo "Starting Helm install process..."
# Create a temporary kubeconfig file
KUBECONFIG_FILE=$(mktemp)
echo "Created temporary KUBECONFIG file: $KUBECONFIG_FILE"
# Write the kubeconfig content
cat <<EOF > $KUBECONFIG_FILE
apiVersion: v1
kind: Config
clusters:
- cluster:
server: ${var.cluster_endpoint}
certificate-authority-data: ${var.cluster_ca_certificate}
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: aws
name: aws
current-context: aws
users:
- name: aws
user:
token: ${var.token}
EOF
echo "Wrote kubeconfig content to $KUBECONFIG_FILE"
# Create a temporary values file
VALUES_FILE=$(mktemp)
echo "Created temporary values file: $VALUES_FILE"
# Write the values content
cat <<EOF > $VALUES_FILE
${jsonencode(var.set_values)}
EOF
echo "Wrote values content to $VALUES_FILE"
# Run Helm command with the temporary kubeconfig and values file
echo "Running Helm command..."
KUBECONFIG=$KUBECONFIG_FILE helm repo add ${var.repo_name} ${var.repo_url}
KUBECONFIG=$KUBECONFIG_FILE helm repo update
KUBECONFIG=$KUBECONFIG_FILE 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
HELM_EXIT_CODE=$?
echo "Helm command exited with code: $HELM_EXIT_CODE"
# Clean up the temporary files
rm $KUBECONFIG_FILE
rm $VALUES_FILE
echo "Removed temporary KUBECONFIG and values files"
exit $HELM_EXIT_CODE
EOT
}
}
10 changes: 0 additions & 10 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,52 +1,42 @@
variable "chart_name" {
type = string
description = "Name of the chart"
}

variable "chart_version" {
type = string
description = "Version of the chart"
}

variable "release_name" {
type = string
description = "Release name of the chart"
}

variable "namespace" {
type = string
description = "Namespace to install the chart"
}

variable "create_namespace" {
type = bool
default = false
description = "Create the namespace if it does not exist. Defaults to false"
}

variable "repo_name" {
type = string
description = "Name of the Helm repository"
}

variable "repo_url" {
type = string
description = "URL of the Helm repository"
}

variable "cluster_ca_certificate" {
type = string
description = "CA certificate of the cluster"
}

variable "cluster_endpoint" {
type = string
description = "Endpoint of the cluster"
}

variable "token" {
type = string
description = "Token to authenticate with the cluster"
}

variable "set_values" {
Expand Down

0 comments on commit 0391057

Please sign in to comment.