diff --git a/README.md b/README.md index 1db1466..2cc7dc6 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ This module is released under the MIT License. See the [LICENSE](./LICENSE) file | Name | Version | |------|---------| +| [aws](#provider\_aws) | n/a | | [null](#provider\_null) | n/a | ## Modules @@ -76,6 +77,8 @@ 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 @@ -83,15 +86,14 @@ No modules. |------|-------------|------|---------|:--------:| | [chart\_name](#input\_chart\_name) | Name of the chart | `string` | n/a | yes | | [chart\_version](#input\_chart\_version) | Version of the chart | `string` | n/a | yes | -| [cluster\_ca\_certificate](#input\_cluster\_ca\_certificate) | CA certificate of the cluster | `string` | n/a | yes | -| [cluster\_endpoint](#input\_cluster\_endpoint) | Endpoint of the cluster | `string` | n/a | yes | +| [cluster\_name](#input\_cluster\_name) | Name of the cluster | `string` | n/a | yes | | [create\_namespace](#input\_create\_namespace) | Create the namespace if it does not exist. Defaults to false | `bool` | `false` | no | | [namespace](#input\_namespace) | Namespace to install the chart | `string` | n/a | yes | | [release\_name](#input\_release\_name) | Release name of the chart | `string` | n/a | yes | | [repo\_name](#input\_repo\_name) | Name of the Helm repository | `string` | n/a | yes | | [repo\_url](#input\_repo\_url) | URL of the Helm repository | `string` | n/a | yes | | [set\_values](#input\_set\_values) | A map of values to pass to the Helm chart | `any` | `{}` | no | -| [token](#input\_token) | Token to authenticate with the cluster | `string` | n/a | yes | +| [trigger\_helm\_update](#input\_trigger\_helm\_update) | Set this to true value trigger a Helm chart update | `bool` | `false` | no | ## Outputs diff --git a/main.tf b/main.tf index c2fa636..0e1fac7 100644 --- a/main.tf +++ b/main.tf @@ -1,9 +1,19 @@ +data "aws_eks_cluster_auth" "cluster" { + name = var.cluster_name +} + +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" { @@ -11,17 +21,17 @@ resource "null_resource" "helm_install" { 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 < $KUBECONFIG_FILE + cat < $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: @@ -32,9 +42,9 @@ resource "null_resource" "helm_install" { users: - name: aws user: - token: ${var.token} + token: ${data.aws_eks_cluster_auth.cluster.token} EOF - echo "Wrote kubeconfig content to $KUBECONFIG_FILE" + echo "Wrote kubeconfig content to $KUBECONFIG" # Create a temporary values file VALUES_FILE=$(mktemp) @@ -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" : ""} \ @@ -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" diff --git a/variables.tf b/variables.tf index ab665d6..8913bfe 100644 --- a/variables.tf +++ b/variables.tf @@ -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 = {} -} \ No newline at end of file +} +variable "trigger_helm_update" { + description = "Set this to true value trigger a Helm chart update" + type = bool + default = false +}