Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trigger_helm_update var & add data block #3

Merged
merged 10 commits into from
Oct 29, 2024
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This module is released under the MIT License. See the [LICENSE](./LICENSE) file

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

## Modules
Expand All @@ -76,22 +77,23 @@ 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_ca_certificate"></a> [cluster\_ca\_certificate](#input\_cluster\_ca\_certificate) | CA certificate of the cluster | `string` | n/a | yes |
| <a name="input_cluster_endpoint"></a> [cluster\_endpoint](#input\_cluster\_endpoint) | Endpoint of the cluster | `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_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 |
| <a name="input_repo_url"></a> [repo\_url](#input\_repo\_url) | URL of the Helm repository | `string` | n/a | yes |
| <a name="input_set_values"></a> [set\_values](#input\_set\_values) | A map of values to pass to the Helm chart | `any` | `{}` | no |
| <a name="input_token"></a> [token](#input\_token) | Token to authenticate with the cluster | `string` | n/a | yes |
| <a name="input_trigger_helm_update"></a> [trigger\_helm\_update](#input\_trigger\_helm\_update) | Set this to true value trigger a Helm chart update | `bool` | `false` | no |

## Outputs

Expand Down
40 changes: 25 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
data "aws_eks_cluster_auth" "cluster" {
name = var.cluster_name
}
sachincool marked this conversation as resolved.
Show resolved Hide resolved

data "aws_eks_cluster" "cluster" {
name = var.cluster_name
}


resource "null_resource" "helm_install" {
triggers = {
chart_name = var.chart_name
chart_version = var.chart_version
release_name = var.release_name
namespace = var.namespace
chart_name = var.chart_name
chart_version = var.chart_version
release_name = var.release_name
namespace = var.namespace
update_trigger = var.trigger_helm_update != null ? timestamp() : "initial"
}

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"
export KUBECONFIG=$(mktemp)
echo "Created temporary KUBECONFIG file: $KUBECONFIG"

# Write the kubeconfig content
cat <<EOF > $KUBECONFIG_FILE
cat <<EOF > $KUBECONFIG
apiVersion: v1
kind: Config
clusters:
- cluster:
server: ${var.cluster_endpoint}
certificate-authority-data: ${var.cluster_ca_certificate}
server: ${data.aws_eks_cluster.cluster.endpoint}
certificate-authority-data: ${data.aws_eks_cluster.cluster.certificate_authority[0].data}
name: kubernetes
contexts:
- context:
Expand All @@ -32,9 +42,9 @@ resource "null_resource" "helm_install" {
users:
- name: aws
user:
token: ${var.token}
token: ${data.aws_eks_cluster_auth.cluster.token}
dunefro marked this conversation as resolved.
Show resolved Hide resolved
EOF
echo "Wrote kubeconfig content to $KUBECONFIG_FILE"
echo "Wrote kubeconfig content to $KUBECONFIG"

# Create a temporary values file
VALUES_FILE=$(mktemp)
Expand All @@ -48,9 +58,9 @@ resource "null_resource" "helm_install" {

# 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} \
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" : ""} \
Expand All @@ -61,7 +71,7 @@ resource "null_resource" "helm_install" {
echo "Helm command exited with code: $HELM_EXIT_CODE"

# Clean up the temporary files
rm $KUBECONFIG_FILE
rm $KUBECONFIG
rm $VALUES_FILE
echo "Removed temporary KUBECONFIG and values files"

Expand Down
21 changes: 8 additions & 13 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,18 @@ variable "repo_url" {
description = "URL of the Helm repository"
}

variable "cluster_ca_certificate" {
variable "cluster_name" {
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"
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
}