Skip to content

Commit

Permalink
Specify node_group user data via flat variables
Browse files Browse the repository at this point in the history
Prior to this change, the user data templating variables were nested
under a top level `user_data` module variable, so that when specifying
one piece of it, the default behaviour (specifically the default MIME
type) changed.  There was no precedent in the module for this kind of
behaviour.

This change flattens the user data module variables, making them
independent of each other.  This requires a slightly odd behaviour
around the MIME type, which we have to specify as explicitly `""` (or
alternatively provide a slightly verbose additional module flag) in
order to prevent wrapping the user data in a CloudInit packet.
  • Loading branch information
scalen committed Nov 3, 2021
1 parent e8d5df5 commit 7135ba9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
17 changes: 9 additions & 8 deletions examples/managed_bottlerocket_node_group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ module "eks" {
public_ip = true

# This section overrides default userdata template to pass bottlerocket
# specific user data and pass additional arguments for userdata template rendering
user_data = {
template_file = "${path.module}/userdata.toml"
template_extra_args = {
enable_admin_container = false
enable_control_container = true
aws_region = data.aws_region.current.name
}
# specific user data and pass additional arguments for userdata template rendering.
# It also instructs the module to not wrap the user data as cloudinit config, by
# indicating that the data has no MIME type.
user_data_mime_type = ""
user_data_template_file = "${path.module}/userdata.toml"
user_data_template_extra_args = {
enable_admin_container = false
enable_control_container = true
aws_region = data.aws_region.current.name
}
# example of k8s/kubelet configuration via additional_userdata
pre_userdata = <<EOT
Expand Down
7 changes: 3 additions & 4 deletions modules/node_groups/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ The role ARN specified in `var.default_iam_role_arn` will be used by default. In
| taints | Kubernetes node taints | list(map) | empty |
| timeouts | A map of timeouts for create/update/delete operations. | `map(string)` | Provider default behavior |
| update_default_version | Whether or not to set the new launch template version the Default | bool | `true` |
| user\_data | Alternative `user_data` templating information. If `user_data` is specified, `user_data.template_file` is required. | `map` (details below) | (see below) |
| user\_data.mime\_type | Alternative MIME type for the user data. | `string` | `"text/x-shellscript"` |
| user\_data.template\_extra\_args | Additional variables to make available to the user data template. | `map(string)` | `{}` |
| user\_data.template\_file | (Required) Alternative template file from which to generate the user data. | `string` | `"${path.module}/templates/userdata.sh.tpl"` |
| user\_data\_mime\_type | Alternative MIME type for the user data when specifying a cloudinit user data. Explicitly set to the empty string `""` to set the user data as a plain base64 encoded file (such as for use with the Bottlerocket AMI). | `string` | `"text/x-shellscript"` |
| user\_data\_template\_extra\_args | Additional variables to make available to the user data template. | `map(string)` | `{}` |
| user\_data\_template\_file | (Required) Alternative template file from which to generate the user data. | `string` | `"${path.module}/templates/userdata.sh.tpl"` |
| metadata_http_endpoint | The state of the instance metadata service. Requires `create_launch_template` to be `true` | string | `var.workers_group_defaults[metadata_http_endpoint]` |
| metadata_http_tokens | If session tokens are required. Requires `create_launch_template` to be `true` | string | `var.workers_group_defaults[metadata_http_tokens]` |
| metadata_http_put_response_hop_limit | The desired HTTP PUT response hop limit for instance metadata requests. Requires `create_launch_template` to be `true` | number | `var.workers_group_defaults[metadata_http_put_response_hop_limit]` |
Expand Down
4 changes: 2 additions & 2 deletions modules/node_groups/launch_template.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
data "cloudinit_config" "workers_userdata" {
for_each = {
for k, v in local.node_groups_expanded : k => v
if v["create_launch_template"] && contains(keys(v["user_data"]), "mime_type")
if v["create_launch_template"] && len(v["user_data_mime_type"]) > 0
}

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

part {
content_type = each.value["user_data"]["mime_type"]
content_type = each.value["user_data_mime_type"]
content = local.node_groups_userdata[each.key]
}
}
Expand Down
11 changes: 5 additions & 6 deletions modules/node_groups/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ locals {
metadata_http_tokens = var.workers_group_defaults["metadata_http_tokens"]
metadata_http_put_response_hop_limit = var.workers_group_defaults["metadata_http_put_response_hop_limit"]
ami_is_eks_optimized = true
user_data = {
mime_type = "text/x-shellscript"
template_extra_args = lookup(var.workers_group_defaults, "userdata_template_file", {})
template_file = lookup(var.workers_group_defaults, "userdata_template_file", "${path.module}/templates/userdata.sh.tpl")
user_data_mime_type = "text/x-shellscript"
user_data_template_extra_args = lookup(var.workers_group_defaults, "userdata_template_file", {})
user_data_template_file = lookup(var.workers_group_defaults, "userdata_template_file", "${path.module}/templates/userdata.sh.tpl")
}
},
var.node_groups_defaults,
Expand All @@ -55,7 +54,7 @@ locals {
) }

node_groups_userdata = { for k, v in local.node_groups_expanded : k => templatefile(
v["user_data"]["template_file"],
v["user_data_template_file"],
merge(
{
cluster_name = var.cluster_name
Expand All @@ -69,7 +68,7 @@ locals {
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", {})
v["user_data_template_extra_args"]
)
) if v["create_launch_template"]
}
Expand Down

0 comments on commit 7135ba9

Please sign in to comment.