From c765ea9aad0098036bbddcfc86b59288257c1b14 Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Sat, 26 Oct 2024 15:23:37 +0530 Subject: [PATCH 01/10] refactor(terraform): replace token variable with aws_eks_cluster_auth data source for authentication and update cluster_name variable for clarity --- main.tf | 6 +++++- variables.tf | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/main.tf b/main.tf index c2fa636..4363381 100644 --- a/main.tf +++ b/main.tf @@ -1,3 +1,6 @@ +data "aws_eks_cluster_auth" "cluster" { + name = var.cluster_name +} resource "null_resource" "helm_install" { triggers = { chart_name = var.chart_name @@ -5,6 +8,7 @@ resource "null_resource" "helm_install" { release_name = var.release_name namespace = var.namespace } + # Add this data source after the existing aws_eks_cluster data source provisioner "local-exec" { command = <<-EOT @@ -32,7 +36,7 @@ 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" diff --git a/variables.tf b/variables.tf index ab665d6..d0caaf1 100644 --- a/variables.tf +++ b/variables.tf @@ -44,13 +44,13 @@ variable "cluster_endpoint" { description = "Endpoint of the cluster" } -variable "token" { +variable "cluster_name" { 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 +} From 889b75a4467979029b5c77d95e67d74356e46777 Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Sat, 26 Oct 2024 15:34:16 +0530 Subject: [PATCH 02/10] feat(terraform): add trigger_helm_update variable to control Helm chart updates Introduce a new variable `trigger_helm_update` to allow users to trigger Helm chart updates. This enhances flexibility by enabling conditional updates based on the variable's value. --- main.tf | 10 +++++----- variables.tf | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/main.tf b/main.tf index 4363381..dd79b2f 100644 --- a/main.tf +++ b/main.tf @@ -3,12 +3,12 @@ data "aws_eks_cluster_auth" "cluster" { } 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 ? var.trigger_helm_update : "initial" } - # Add this data source after the existing aws_eks_cluster data source provisioner "local-exec" { command = <<-EOT diff --git a/variables.tf b/variables.tf index d0caaf1..022c7bc 100644 --- a/variables.tf +++ b/variables.tf @@ -54,3 +54,8 @@ variable "set_values" { description = "A map of values to pass to the Helm chart" default = {} } +variable "trigger_helm_update" { + description = "Set this to any value to trigger a Helm chart update" + type = string + default = null +} From 1cb4c481a87a84308366aeccb32411c8b7422103 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 26 Oct 2024 10:05:22 +0000 Subject: [PATCH 03/10] terraform-docs: automated action --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1db1466..64c423e 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,7 @@ No modules. | Name | Type | |------|------| | [null_resource.helm_install](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource | +| [aws_eks_cluster_auth.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source | ## Inputs @@ -85,13 +87,14 @@ No modules. | [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 any value to trigger a Helm chart update | `string` | `null` | no | ## Outputs From 9b7fefdf71284c3475f320b0267a7689bf9b22de Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Mon, 28 Oct 2024 13:58:19 +0530 Subject: [PATCH 04/10] changed update_trigger to bool --- main.tf | 3 ++- variables.tf | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index dd79b2f..9ee6994 100644 --- a/main.tf +++ b/main.tf @@ -1,13 +1,14 @@ data "aws_eks_cluster_auth" "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 - update_trigger = var.trigger_helm_update != null ? var.trigger_helm_update : "initial" + update_trigger = var.trigger_helm_update != null ? timestamp() : "initial" } provisioner "local-exec" { diff --git a/variables.tf b/variables.tf index 022c7bc..79744a7 100644 --- a/variables.tf +++ b/variables.tf @@ -56,6 +56,6 @@ variable "set_values" { } variable "trigger_helm_update" { description = "Set this to any value to trigger a Helm chart update" - type = string - default = null + type = bool + default = false } From a90c72bfb66117166b29188653503b533e98d24a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 28 Oct 2024 08:28:45 +0000 Subject: [PATCH 05/10] terraform-docs: automated action --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 64c423e..0bd8b25 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ No modules. | [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 | -| [trigger\_helm\_update](#input\_trigger\_helm\_update) | Set this to any value to trigger a Helm chart update | `string` | `null` | no | +| [trigger\_helm\_update](#input\_trigger\_helm\_update) | Set this to any value to trigger a Helm chart update | `bool` | `false` | no | ## Outputs From 42474f3aa8a7f9cdf828ad36ee30624f64c1bdd8 Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Tue, 29 Oct 2024 02:10:10 +0530 Subject: [PATCH 06/10] refactor(terraform): use aws_eks_cluster data source for dynamic cluster info Replace hardcoded cluster endpoint and CA certificate variables with dynamic retrieval using aws_eks_cluster data source. This change removes the need for cluster_ca_certificate and cluster_endpoint variables, simplifying configuration and reducing manual input. --- main.tf | 9 +++++++-- variables.tf | 10 ---------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/main.tf b/main.tf index 9ee6994..af85887 100644 --- a/main.tf +++ b/main.tf @@ -2,6 +2,11 @@ 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 @@ -25,8 +30,8 @@ resource "null_resource" "helm_install" { 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: diff --git a/variables.tf b/variables.tf index 79744a7..fbe521e 100644 --- a/variables.tf +++ b/variables.tf @@ -34,16 +34,6 @@ variable "repo_url" { 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 "cluster_name" { type = string description = "Name of the cluster" From 2a9fdfbfb2303fd2c6dc86732321b621db8dddce Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 28 Oct 2024 20:40:32 +0000 Subject: [PATCH 07/10] terraform-docs: automated action --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 0bd8b25..390dd48 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ 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 @@ -85,8 +86,6 @@ 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 | From 4de6ff2c32c23460892c4de4602b645b800445fe Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Tue, 29 Oct 2024 10:51:08 +0530 Subject: [PATCH 08/10] refactor(main.tf): streamline KUBECONFIG handling by using export to simplify Helm command execution and cleanup --- main.tf | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/main.tf b/main.tf index af85887..0e1fac7 100644 --- a/main.tf +++ b/main.tf @@ -21,11 +21,11 @@ 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: @@ -44,7 +44,7 @@ resource "null_resource" "helm_install" { user: 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) @@ -58,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" : ""} \ @@ -71,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" From a60f3a1078877ff8950eb2a702f41a3bd0fcc755 Mon Sep 17 00:00:00 2001 From: Harshit Luthra Date: Tue, 29 Oct 2024 10:52:25 +0530 Subject: [PATCH 09/10] docs(variables.tf): clarify description for trigger_helm_update variable to specify it should be set to true to trigger an update --- variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variables.tf b/variables.tf index fbe521e..8913bfe 100644 --- a/variables.tf +++ b/variables.tf @@ -45,7 +45,7 @@ variable "set_values" { default = {} } variable "trigger_helm_update" { - description = "Set this to any value to trigger a Helm chart update" + description = "Set this to true value trigger a Helm chart update" type = bool default = false } From d1d9808532869533249b69817ccc0e4c0dfed94a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 29 Oct 2024 05:22:43 +0000 Subject: [PATCH 10/10] terraform-docs: automated action --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 390dd48..2cc7dc6 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ No modules. | [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 | -| [trigger\_helm\_update](#input\_trigger\_helm\_update) | Set this to any value to trigger a Helm chart update | `bool` | `false` | no | +| [trigger\_helm\_update](#input\_trigger\_helm\_update) | Set this to true value trigger a Helm chart update | `bool` | `false` | no | ## Outputs