From b183b972539fa9d39dc55c453d68a234fdb683b1 Mon Sep 17 00:00:00 2001 From: Daniel Piddock <33028589+dpiddockcmp@users.noreply.github.com> Date: Wed, 6 May 2020 10:54:14 +0200 Subject: [PATCH] improvement: Remove dependency on external template provider (#854) * Remove template_file for generating kubeconfig Push logic from terraform down to the template. Makes the formatting slightly easier to follow * Remove template_file for generating userdata Updates to the eks_cluster now do not trigger recreation of launch configurations * Remove template_file for LT userdata * Remove template dependency --- README.md | 2 - data.tf | 212 ++++++++++++++++--------------------- kubectl.tf | 2 +- outputs.tf | 6 +- templates/kubeconfig.tpl | 16 ++- versions.tf | 1 - workers.tf | 2 +- workers_launch_template.tf | 2 +- 8 files changed, 108 insertions(+), 135 deletions(-) diff --git a/README.md b/README.md index 3d0743d..c4b751a 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,6 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | local | >= 1.2 | | null | >= 2.1 | | random | >= 2.1 | -| template | >= 2.1 | ## Providers @@ -145,7 +144,6 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | local | >= 1.2 | | null | >= 2.1 | | random | >= 2.1 | -| template | >= 2.1 | ## Inputs diff --git a/data.tf b/data.tf index bb5effd..e0dcc5e 100644 --- a/data.tf +++ b/data.tf @@ -66,138 +66,104 @@ data "aws_iam_policy_document" "cluster_assume_role_policy" { } } -data "template_file" "kubeconfig" { - count = var.create_eks ? 1 : 0 - template = file("${path.module}/templates/kubeconfig.tpl") +locals { + kubeconfig = var.create_eks ? templatefile("${path.module}/templates/kubeconfig.tpl", { + kubeconfig_name = local.kubeconfig_name + endpoint = aws_eks_cluster.this[0].endpoint + cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data + aws_authenticator_command = var.kubeconfig_aws_authenticator_command + aws_authenticator_command_args = length(var.kubeconfig_aws_authenticator_command_args) > 0 ? var.kubeconfig_aws_authenticator_command_args : ["token", "-i", aws_eks_cluster.this[0].name] + aws_authenticator_additional_args = var.kubeconfig_aws_authenticator_additional_args + aws_authenticator_env_variables = var.kubeconfig_aws_authenticator_env_variables + }) : "" - vars = { - kubeconfig_name = local.kubeconfig_name - endpoint = aws_eks_cluster.this[0].endpoint - cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data - aws_authenticator_command = var.kubeconfig_aws_authenticator_command - aws_authenticator_command_args = length(var.kubeconfig_aws_authenticator_command_args) > 0 ? " - ${join( - "\n - ", - var.kubeconfig_aws_authenticator_command_args, - )}" : " - ${join( - "\n - ", - formatlist("\"%s\"", ["token", "-i", aws_eks_cluster.this[0].name]), - )}" - aws_authenticator_additional_args = length(var.kubeconfig_aws_authenticator_additional_args) > 0 ? " - ${join( - "\n - ", - var.kubeconfig_aws_authenticator_additional_args, - )}" : "" - aws_authenticator_env_variables = length(var.kubeconfig_aws_authenticator_env_variables) > 0 ? " env:\n${join( - "\n", - data.template_file.aws_authenticator_env_variables.*.rendered, - )}" : "" - } -} - -data "template_file" "aws_authenticator_env_variables" { - count = length(var.kubeconfig_aws_authenticator_env_variables) - - template = < 0 } + env: + %{~ for k, v in aws_authenticator_env_variables ~} + - name: ${k} + value: ${v} + %{~ endfor ~} +%{ endif ~} diff --git a/versions.tf b/versions.tf index fd052bb..e5c8ce6 100644 --- a/versions.tf +++ b/versions.tf @@ -5,7 +5,6 @@ terraform { aws = ">= 2.52.0" local = ">= 1.2" null = ">= 2.1" - template = ">= 2.1" random = ">= 2.1" kubernetes = ">= 1.11.1" } diff --git a/workers.tf b/workers.tf index 2eed088..a1908b7 100644 --- a/workers.tf +++ b/workers.tf @@ -170,7 +170,7 @@ resource "aws_launch_configuration" "workers" { "key_name", local.workers_group_defaults["key_name"], ) - user_data_base64 = base64encode(data.template_file.userdata.*.rendered[count.index]) + user_data_base64 = base64encode(local.userdata[count.index]) ebs_optimized = lookup( var.worker_groups[count.index], "ebs_optimized", diff --git a/workers_launch_template.tf b/workers_launch_template.tf index 947bd19..ebc997e 100644 --- a/workers_launch_template.tf +++ b/workers_launch_template.tf @@ -262,7 +262,7 @@ resource "aws_launch_template" "workers_launch_template" { local.workers_group_defaults["key_name"], ) user_data = base64encode( - data.template_file.launch_template_userdata.*.rendered[count.index], + local.launch_template_userdata[count.index], ) ebs_optimized = lookup(