mirror of
https://github.com/ysoftdevs/terraform-aws-eks.git
synced 2026-04-17 14:29:39 +02:00
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
This commit is contained in:
@@ -134,7 +134,6 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
|
|||||||
| local | >= 1.2 |
|
| local | >= 1.2 |
|
||||||
| null | >= 2.1 |
|
| null | >= 2.1 |
|
||||||
| random | >= 2.1 |
|
| random | >= 2.1 |
|
||||||
| template | >= 2.1 |
|
|
||||||
|
|
||||||
## Providers
|
## Providers
|
||||||
|
|
||||||
@@ -145,7 +144,6 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
|
|||||||
| local | >= 1.2 |
|
| local | >= 1.2 |
|
||||||
| null | >= 2.1 |
|
| null | >= 2.1 |
|
||||||
| random | >= 2.1 |
|
| random | >= 2.1 |
|
||||||
| template | >= 2.1 |
|
|
||||||
|
|
||||||
## Inputs
|
## Inputs
|
||||||
|
|
||||||
|
|||||||
212
data.tf
212
data.tf
@@ -66,138 +66,104 @@ data "aws_iam_policy_document" "cluster_assume_role_policy" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data "template_file" "kubeconfig" {
|
locals {
|
||||||
count = var.create_eks ? 1 : 0
|
kubeconfig = var.create_eks ? templatefile("${path.module}/templates/kubeconfig.tpl", {
|
||||||
template = file("${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 = {
|
userdata = [for worker in var.worker_groups : templatefile(
|
||||||
kubeconfig_name = local.kubeconfig_name
|
lookup(
|
||||||
endpoint = aws_eks_cluster.this[0].endpoint
|
worker,
|
||||||
cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data
|
"userdata_template_file",
|
||||||
aws_authenticator_command = var.kubeconfig_aws_authenticator_command
|
lookup(worker, "platform", local.workers_group_defaults["platform"]) == "windows"
|
||||||
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 = <<EOF
|
|
||||||
- name: $${key}
|
|
||||||
value: $${value}
|
|
||||||
EOF
|
|
||||||
|
|
||||||
|
|
||||||
vars = {
|
|
||||||
value = values(var.kubeconfig_aws_authenticator_env_variables)[count.index]
|
|
||||||
key = keys(var.kubeconfig_aws_authenticator_env_variables)[count.index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data "template_file" "userdata" {
|
|
||||||
count = var.create_eks ? local.worker_group_count : 0
|
|
||||||
template = lookup(
|
|
||||||
var.worker_groups[count.index],
|
|
||||||
"userdata_template_file",
|
|
||||||
file(
|
|
||||||
lookup(var.worker_groups[count.index], "platform", local.workers_group_defaults["platform"]) == "windows"
|
|
||||||
? "${path.module}/templates/userdata_windows.tpl"
|
? "${path.module}/templates/userdata_windows.tpl"
|
||||||
: "${path.module}/templates/userdata.sh.tpl"
|
: "${path.module}/templates/userdata.sh.tpl"
|
||||||
|
),
|
||||||
|
merge(
|
||||||
|
{
|
||||||
|
platform = lookup(worker, "platform", local.workers_group_defaults["platform"])
|
||||||
|
cluster_name = aws_eks_cluster.this[0].name
|
||||||
|
endpoint = aws_eks_cluster.this[0].endpoint
|
||||||
|
cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data
|
||||||
|
pre_userdata = lookup(
|
||||||
|
worker,
|
||||||
|
"pre_userdata",
|
||||||
|
local.workers_group_defaults["pre_userdata"],
|
||||||
|
)
|
||||||
|
additional_userdata = lookup(
|
||||||
|
worker,
|
||||||
|
"additional_userdata",
|
||||||
|
local.workers_group_defaults["additional_userdata"],
|
||||||
|
)
|
||||||
|
bootstrap_extra_args = lookup(
|
||||||
|
worker,
|
||||||
|
"bootstrap_extra_args",
|
||||||
|
local.workers_group_defaults["bootstrap_extra_args"],
|
||||||
|
)
|
||||||
|
kubelet_extra_args = lookup(
|
||||||
|
worker,
|
||||||
|
"kubelet_extra_args",
|
||||||
|
local.workers_group_defaults["kubelet_extra_args"],
|
||||||
|
)
|
||||||
|
},
|
||||||
|
lookup(
|
||||||
|
worker,
|
||||||
|
"userdata_template_extra_args",
|
||||||
|
local.workers_group_defaults["userdata_template_extra_args"]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
) if var.create_eks
|
||||||
|
]
|
||||||
|
|
||||||
vars = merge({
|
launch_template_userdata = [for worker in var.worker_groups_launch_template : templatefile(
|
||||||
platform = lookup(var.worker_groups[count.index], "platform", local.workers_group_defaults["platform"])
|
|
||||||
cluster_name = aws_eks_cluster.this[0].name
|
|
||||||
endpoint = aws_eks_cluster.this[0].endpoint
|
|
||||||
cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data
|
|
||||||
pre_userdata = lookup(
|
|
||||||
var.worker_groups[count.index],
|
|
||||||
"pre_userdata",
|
|
||||||
local.workers_group_defaults["pre_userdata"],
|
|
||||||
)
|
|
||||||
additional_userdata = lookup(
|
|
||||||
var.worker_groups[count.index],
|
|
||||||
"additional_userdata",
|
|
||||||
local.workers_group_defaults["additional_userdata"],
|
|
||||||
)
|
|
||||||
bootstrap_extra_args = lookup(
|
|
||||||
var.worker_groups[count.index],
|
|
||||||
"bootstrap_extra_args",
|
|
||||||
local.workers_group_defaults["bootstrap_extra_args"],
|
|
||||||
)
|
|
||||||
kubelet_extra_args = lookup(
|
|
||||||
var.worker_groups[count.index],
|
|
||||||
"kubelet_extra_args",
|
|
||||||
local.workers_group_defaults["kubelet_extra_args"],
|
|
||||||
)
|
|
||||||
},
|
|
||||||
lookup(
|
lookup(
|
||||||
var.worker_groups[count.index],
|
worker,
|
||||||
"userdata_template_extra_args",
|
"userdata_template_file",
|
||||||
local.workers_group_defaults["userdata_template_extra_args"]
|
lookup(worker, "platform", local.workers_group_defaults["platform"]) == "windows"
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
data "template_file" "launch_template_userdata" {
|
|
||||||
count = var.create_eks ? local.worker_group_launch_template_count : 0
|
|
||||||
template = lookup(
|
|
||||||
var.worker_groups_launch_template[count.index],
|
|
||||||
"userdata_template_file",
|
|
||||||
file(
|
|
||||||
lookup(var.worker_groups_launch_template[count.index], "platform", local.workers_group_defaults["platform"]) == "windows"
|
|
||||||
? "${path.module}/templates/userdata_windows.tpl"
|
? "${path.module}/templates/userdata_windows.tpl"
|
||||||
: "${path.module}/templates/userdata.sh.tpl"
|
: "${path.module}/templates/userdata.sh.tpl"
|
||||||
|
),
|
||||||
|
merge(
|
||||||
|
{
|
||||||
|
platform = lookup(worker, "platform", local.workers_group_defaults["platform"])
|
||||||
|
cluster_name = aws_eks_cluster.this[0].name
|
||||||
|
endpoint = aws_eks_cluster.this[0].endpoint
|
||||||
|
cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data
|
||||||
|
pre_userdata = lookup(
|
||||||
|
worker,
|
||||||
|
"pre_userdata",
|
||||||
|
local.workers_group_defaults["pre_userdata"],
|
||||||
|
)
|
||||||
|
additional_userdata = lookup(
|
||||||
|
worker,
|
||||||
|
"additional_userdata",
|
||||||
|
local.workers_group_defaults["additional_userdata"],
|
||||||
|
)
|
||||||
|
bootstrap_extra_args = lookup(
|
||||||
|
worker,
|
||||||
|
"bootstrap_extra_args",
|
||||||
|
local.workers_group_defaults["bootstrap_extra_args"],
|
||||||
|
)
|
||||||
|
kubelet_extra_args = lookup(
|
||||||
|
worker,
|
||||||
|
"kubelet_extra_args",
|
||||||
|
local.workers_group_defaults["kubelet_extra_args"],
|
||||||
|
)
|
||||||
|
},
|
||||||
|
lookup(
|
||||||
|
worker,
|
||||||
|
"userdata_template_extra_args",
|
||||||
|
local.workers_group_defaults["userdata_template_extra_args"]
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
) if var.create_eks
|
||||||
|
]
|
||||||
vars = merge({
|
|
||||||
platform = lookup(var.worker_groups_launch_template[count.index], "platform", local.workers_group_defaults["platform"])
|
|
||||||
cluster_name = aws_eks_cluster.this[0].name
|
|
||||||
endpoint = aws_eks_cluster.this[0].endpoint
|
|
||||||
cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data
|
|
||||||
pre_userdata = lookup(
|
|
||||||
var.worker_groups_launch_template[count.index],
|
|
||||||
"pre_userdata",
|
|
||||||
local.workers_group_defaults["pre_userdata"],
|
|
||||||
)
|
|
||||||
additional_userdata = lookup(
|
|
||||||
var.worker_groups_launch_template[count.index],
|
|
||||||
"additional_userdata",
|
|
||||||
local.workers_group_defaults["additional_userdata"],
|
|
||||||
)
|
|
||||||
bootstrap_extra_args = lookup(
|
|
||||||
var.worker_groups_launch_template[count.index],
|
|
||||||
"bootstrap_extra_args",
|
|
||||||
local.workers_group_defaults["bootstrap_extra_args"],
|
|
||||||
)
|
|
||||||
kubelet_extra_args = lookup(
|
|
||||||
var.worker_groups_launch_template[count.index],
|
|
||||||
"kubelet_extra_args",
|
|
||||||
local.workers_group_defaults["kubelet_extra_args"],
|
|
||||||
)
|
|
||||||
},
|
|
||||||
lookup(
|
|
||||||
var.worker_groups_launch_template[count.index],
|
|
||||||
"userdata_template_extra_args",
|
|
||||||
local.workers_group_defaults["userdata_template_extra_args"]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data "aws_iam_role" "custom_cluster_iam_role" {
|
data "aws_iam_role" "custom_cluster_iam_role" {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
resource "local_file" "kubeconfig" {
|
resource "local_file" "kubeconfig" {
|
||||||
count = var.write_kubeconfig && var.create_eks ? 1 : 0
|
count = var.write_kubeconfig && var.create_eks ? 1 : 0
|
||||||
content = data.template_file.kubeconfig[0].rendered
|
content = local.kubeconfig
|
||||||
filename = substr(var.config_output_path, -1, 1) == "/" ? "${var.config_output_path}kubeconfig_${var.cluster_name}" : var.config_output_path
|
filename = substr(var.config_output_path, -1, 1) == "/" ? "${var.config_output_path}kubeconfig_${var.cluster_name}" : var.config_output_path
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ output "cloudwatch_log_group_name" {
|
|||||||
|
|
||||||
output "kubeconfig" {
|
output "kubeconfig" {
|
||||||
description = "kubectl config file contents for this EKS cluster."
|
description = "kubectl config file contents for this EKS cluster."
|
||||||
value = concat(data.template_file.kubeconfig[*].rendered, [""])[0]
|
value = local.kubeconfig
|
||||||
}
|
}
|
||||||
|
|
||||||
output "kubeconfig_filename" {
|
output "kubeconfig_filename" {
|
||||||
@@ -92,8 +92,8 @@ output "workers_asg_names" {
|
|||||||
output "workers_user_data" {
|
output "workers_user_data" {
|
||||||
description = "User data of worker groups"
|
description = "User data of worker groups"
|
||||||
value = concat(
|
value = concat(
|
||||||
data.template_file.userdata.*.rendered,
|
local.userdata,
|
||||||
data.template_file.launch_template_userdata.*.rendered,
|
local.launch_template_userdata,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,16 @@ users:
|
|||||||
apiVersion: client.authentication.k8s.io/v1alpha1
|
apiVersion: client.authentication.k8s.io/v1alpha1
|
||||||
command: ${aws_authenticator_command}
|
command: ${aws_authenticator_command}
|
||||||
args:
|
args:
|
||||||
${aws_authenticator_command_args}
|
%{~ for i in aws_authenticator_command_args }
|
||||||
${aws_authenticator_additional_args}
|
- "${i}"
|
||||||
${aws_authenticator_env_variables}
|
%{~ endfor ~}
|
||||||
|
%{ for i in aws_authenticator_additional_args }
|
||||||
|
- ${i}
|
||||||
|
%{~ endfor ~}
|
||||||
|
%{ if length(aws_authenticator_env_variables) > 0 }
|
||||||
|
env:
|
||||||
|
%{~ for k, v in aws_authenticator_env_variables ~}
|
||||||
|
- name: ${k}
|
||||||
|
value: ${v}
|
||||||
|
%{~ endfor ~}
|
||||||
|
%{ endif ~}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ terraform {
|
|||||||
aws = ">= 2.52.0"
|
aws = ">= 2.52.0"
|
||||||
local = ">= 1.2"
|
local = ">= 1.2"
|
||||||
null = ">= 2.1"
|
null = ">= 2.1"
|
||||||
template = ">= 2.1"
|
|
||||||
random = ">= 2.1"
|
random = ">= 2.1"
|
||||||
kubernetes = ">= 1.11.1"
|
kubernetes = ">= 1.11.1"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ resource "aws_launch_configuration" "workers" {
|
|||||||
"key_name",
|
"key_name",
|
||||||
local.workers_group_defaults["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(
|
ebs_optimized = lookup(
|
||||||
var.worker_groups[count.index],
|
var.worker_groups[count.index],
|
||||||
"ebs_optimized",
|
"ebs_optimized",
|
||||||
|
|||||||
@@ -262,7 +262,7 @@ resource "aws_launch_template" "workers_launch_template" {
|
|||||||
local.workers_group_defaults["key_name"],
|
local.workers_group_defaults["key_name"],
|
||||||
)
|
)
|
||||||
user_data = base64encode(
|
user_data = base64encode(
|
||||||
data.template_file.launch_template_userdata.*.rendered[count.index],
|
local.launch_template_userdata[count.index],
|
||||||
)
|
)
|
||||||
|
|
||||||
ebs_optimized = lookup(
|
ebs_optimized = lookup(
|
||||||
|
|||||||
Reference in New Issue
Block a user