-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ef9572
commit 0391057
Showing
3 changed files
with
127 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters