Skip to content

Commit

Permalink
Avoid using cloudinit at all unless the config specifies a MIM type
Browse files Browse the repository at this point in the history
Prior to this change, node_groups userdata had to be specified as a
cloudinit config file.  However, Bottlerocket OS doesn't support
cloudinit and relies on bare TOML.

This change makes the use of cloudinit config rely on the specification
of a MIME type for the templated userdata file.  Otherwise, the userdata
provided falls back to the raw templated file content.

*N.B.*: This is backwards compatible, as the default `user_data`
specification includes a MIME type (`text/x-shellscript`).
  • Loading branch information
scalen committed Oct 20, 2021
1 parent b8e1743 commit 259c997
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
27 changes: 11 additions & 16 deletions modules/node_groups/launch_template.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
data "cloudinit_config" "workers_userdata" {
for_each = { for k, v in local.node_groups_expanded : k => v if v["create_launch_template"] }
for_each = {
for k, v in local.node_groups_expanded : k => v
if v["create_launch_template"] && contains(keys(v["user_data"]), "mime_type")
}

gzip = false
base64_encode = true
boundary = "//"

part {
content_type = lookup(each.value["user_data"], "mime_type", "text/x-shellscript")
content = templatefile(each.value["user_data"]["template_file"],
merge({
kubelet_extra_args = each.value["kubelet_extra_args"]
pre_userdata = each.value["pre_userdata"]
ami_id = lookup(each.value, "ami_id", "")
ami_is_eks_optimized = each.value["ami_is_eks_optimized"]
cluster_name = var.cluster_name
cluster_endpoint = var.cluster_endpoint
cluster_auth_base64 = var.cluster_auth_base64
capacity_type = lookup(each.value, "capacity_type", "ON_DEMAND")
append_labels = length(lookup(each.value, "k8s_labels", {})) > 0 ? ",${join(",", [for k, v in lookup(each.value, "k8s_labels", {}) : "${k}=${v}"])}" : ""
}, lookup(each.value["user_data"], "template_extra_args", {}))
)
content_type = each.value["user_data"]["mime_type"]
content = local.node_groups_userdata[each.key]
}
}

Expand Down Expand Up @@ -80,7 +71,11 @@ resource "aws_launch_template" "workers" {
#
# (optionally you can use https://registry.terraform.io/providers/hashicorp/cloudinit/latest/docs/data-sources/cloudinit_config to render the script, example: https://github.com/terraform-aws-modules/terraform-aws-eks/pull/997#issuecomment-705286151)

user_data = data.cloudinit_config.workers_userdata[each.key].rendered
user_data = (
contains(keys(data.cloudinit_config.workers_userdata), each.key)
? data.cloudinit_config.workers_userdata[each.key].rendered
: base64encode(local.node_groups_userdata[each.key])
)

key_name = lookup(each.value, "key_name", null)

Expand Down
19 changes: 19 additions & 0 deletions modules/node_groups/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,23 @@ locals {
join("-", [var.cluster_name, k])
)
) }

node_groups_userdata = { for k, v in local.node_groups_expanded : k => templatefile(
v["user_data"]["template_file"],
merge(
{
kubelet_extra_args = v["kubelet_extra_args"]
pre_userdata = v["pre_userdata"]
ami_id = lookup(v, "ami_id", "")
ami_is_eks_optimized = v["ami_is_eks_optimized"]
cluster_name = var.cluster_name
cluster_endpoint = var.cluster_endpoint
cluster_auth_base64 = var.cluster_auth_base64
capacity_type = lookup(v, "capacity_type", "ON_DEMAND")
append_labels = length(lookup(v, "k8s_labels", {})) > 0 ? ",${join(",", [for key, value in lookup(v, "k8s_labels", {}) : "${key}=${value}"])}" : ""
},
lookup(v["user_data"], "template_extra_args", {})
)
) if v["create_launch_template"]
}
}

0 comments on commit 259c997

Please sign in to comment.