Skip to content

Commit 2364795

Browse files
dpiddockcmpmax-rocket-internet
authored andcommitted
Easier overriding of workers_group_defaults (#107)
1 parent 9f8cab1 commit 2364795

File tree

6 files changed

+44
-41
lines changed

6 files changed

+44
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
### Changed
1818

19-
- A subtle but thoughtful change. (Boomshakalaka, @self 🏀)
2019
- **Breaking change** Removed `workstation_cidr` variable, http callout and unnecessary security rule. (by @dpiddockcmp)
20+
- Can now selectively override keys in `workers_group_defaults` variable rather than callers maintaining a duplicate of the whole map. (by @dpiddockcmp)
2121

2222
## [[v1.4.0](https://github.com/terraform-aws-modules/terraform-aws-eks/compare/v1.3.0...v1.4.0)] - 2018-08-02]
2323

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
117117
| worker_groups | A list of maps defining worker group configurations. See workers_group_defaults for valid keys. | list | `<list>` | no |
118118
| worker_security_group_id | If provided, all workers will be attached to this security group. If not given, a security group will be created with necessary ingres/egress to work with the EKS cluster. | string | `` | no |
119119
| worker_sg_ingress_from_port | Minimum port number from which pods will accept communication. Must be changed to a lower value if some pods in your cluster will expose a port lower than 1025 (e.g. 22, 80, or 443). | string | `1025` | no |
120-
| workers_group_defaults | Default values for target groups as defined by the list of maps. | map | `<map>` | no |
120+
| workers_group_defaults | Override default values for target groups. See workers_group_defaults_defaults in locals.tf for valid keys. | map | `<map>` | no |
121121
| write_kubeconfig | Whether to write a kubeconfig file containing the cluster configuration. | string | `true` | no |
122122
123123
## Outputs

data.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ data "template_file" "userdata" {
7878
cluster_name = "${aws_eks_cluster.this.name}"
7979
endpoint = "${aws_eks_cluster.this.endpoint}"
8080
cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}"
81-
max_pod_count = "${lookup(local.max_pod_per_node, lookup(var.worker_groups[count.index], "instance_type", lookup(var.workers_group_defaults, "instance_type")))}"
82-
pre_userdata = "${lookup(var.worker_groups[count.index], "pre_userdata",lookup(var.workers_group_defaults, "pre_userdata"))}"
83-
additional_userdata = "${lookup(var.worker_groups[count.index], "additional_userdata",lookup(var.workers_group_defaults, "additional_userdata"))}"
84-
kubelet_node_labels = "${lookup(var.worker_groups[count.index], "kubelet_node_labels",lookup(var.workers_group_defaults, "kubelet_node_labels"))}"
81+
max_pod_count = "${lookup(local.max_pod_per_node, lookup(var.worker_groups[count.index], "instance_type", lookup(local.workers_group_defaults, "instance_type")))}"
82+
pre_userdata = "${lookup(var.worker_groups[count.index], "pre_userdata",lookup(local.workers_group_defaults, "pre_userdata"))}"
83+
additional_userdata = "${lookup(var.worker_groups[count.index], "additional_userdata",lookup(local.workers_group_defaults, "additional_userdata"))}"
84+
kubelet_node_labels = "${lookup(var.worker_groups[count.index], "kubelet_node_labels",lookup(local.workers_group_defaults, "kubelet_node_labels"))}"
8585
}
8686
}

local.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ locals {
88
worker_security_group_id = "${coalesce(join("", aws_security_group.workers.*.id), var.worker_security_group_id)}"
99
kubeconfig_name = "${var.kubeconfig_name == "" ? "eks_${var.cluster_name}" : var.kubeconfig_name}"
1010

11+
workers_group_defaults_defaults = {
12+
name = "count.index" # Name of the worker group. Literal count.index will never be used but if name is not set, the count.index interpolation will be used.
13+
ami_id = "" # AMI ID for the eks workers. If none is provided, Terraform will search for the latest version of their EKS optimized worker AMI.
14+
asg_desired_capacity = "1" # Desired worker capacity in the autoscaling group.
15+
asg_max_size = "3" # Maximum worker capacity in the autoscaling group.
16+
asg_min_size = "1" # Minimum worker capacity in the autoscaling group.
17+
instance_type = "m4.large" # Size of the workers instances.
18+
spot_price = "" # Cost of spot instance.
19+
root_volume_size = "100" # root volume size of workers instances.
20+
root_volume_type = "gp2" # root volume type of workers instances, can be 'standard', 'gp2', or 'io1'
21+
root_iops = "0" # The amount of provisioned IOPS. This must be set with a volume_type of "io1".
22+
key_name = "" # The key name that should be used for the instances in the autoscaling group
23+
pre_userdata = "" # userdata to pre-append to the default userdata.
24+
additional_userdata = "" # userdata to append to the default userdata.
25+
ebs_optimized = true # sets whether to use ebs optimization on supported types.
26+
enable_monitoring = true # Enables/disables detailed monitoring.
27+
public_ip = false # Associate a public ip address with a worker
28+
kubelet_node_labels = "" # This string is passed directly to kubelet via --node-labels= if set. It should be comma delimited with no spaces. If left empty no --node-labels switch is added.
29+
subnets = "" # A comma delimited string of subnets to place the worker nodes in. i.e. subnet-123,subnet-456,subnet-789
30+
autoscaling_enabled = false # Sets whether policy and matching tags will be added to allow autoscaling.
31+
}
32+
33+
workers_group_defaults = "${merge(local.workers_group_defaults_defaults, var.workers_group_defaults)}"
34+
1135
# Mapping from the node type that we selected and the max number of pods that it can run
1236
# Taken from https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-06-05/amazon-eks-nodegroup.yaml
1337
max_pod_per_node = {

variables.tf

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,9 @@ variable "worker_group_count" {
7676
}
7777

7878
variable "workers_group_defaults" {
79-
description = "Default values for target groups as defined by the list of maps."
79+
description = "Override default values for target groups. See workers_group_defaults_defaults in locals.tf for valid keys."
8080
type = "map"
81-
82-
default = {
83-
name = "count.index" # Name of the worker group. Literal count.index will never be used but if name is not set, the count.index interpolation will be used.
84-
ami_id = "" # AMI ID for the eks workers. If none is provided, Terraform will search for the latest version of their EKS optimized worker AMI.
85-
asg_desired_capacity = "1" # Desired worker capacity in the autoscaling group.
86-
asg_max_size = "3" # Maximum worker capacity in the autoscaling group.
87-
asg_min_size = "1" # Minimum worker capacity in the autoscaling group.
88-
instance_type = "m4.large" # Size of the workers instances.
89-
spot_price = "" # Cost of spot instance.
90-
root_volume_size = "100" # root volume size of workers instances.
91-
root_volume_type = "gp2" # root volume type of workers instances, can be 'standard', 'gp2', or 'io1'
92-
root_iops = "0" # The amount of provisioned IOPS. This must be set with a volume_type of "io1".
93-
key_name = "" # The key name that should be used for the instances in the autoscaling group
94-
pre_userdata = "" # userdata to pre-append to the default userdata.
95-
additional_userdata = "" # userdata to append to the default userdata.
96-
ebs_optimized = true # sets whether to use ebs optimization on supported types.
97-
enable_monitoring = true # Enables/disables detailed monitoring.
98-
public_ip = false # Associate a public ip address with a worker
99-
kubelet_node_labels = "" # This string is passed directly to kubelet via --node-labels= if set. It should be comma delimited with no spaces. If left empty no --node-labels switch is added.
100-
subnets = "" # A comma delimited string of subnets to place the worker nodes in. i.e. subnet-123,subnet-456,subnet-789
101-
autoscaling_enabled = false # Sets whether policy and matching tags will be added to allow autoscaling.
102-
}
81+
default = {}
10382
}
10483

10584
variable "worker_security_group_id" {

workers.tf

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
resource "aws_autoscaling_group" "workers" {
22
name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups[count.index], "name", count.index)}"
3-
desired_capacity = "${lookup(var.worker_groups[count.index], "asg_desired_capacity", lookup(var.workers_group_defaults, "asg_desired_capacity"))}"
4-
max_size = "${lookup(var.worker_groups[count.index], "asg_max_size",lookup(var.workers_group_defaults, "asg_max_size"))}"
5-
min_size = "${lookup(var.worker_groups[count.index], "asg_min_size",lookup(var.workers_group_defaults, "asg_min_size"))}"
3+
desired_capacity = "${lookup(var.worker_groups[count.index], "asg_desired_capacity", lookup(local.workers_group_defaults, "asg_desired_capacity"))}"
4+
max_size = "${lookup(var.worker_groups[count.index], "asg_max_size",lookup(local.workers_group_defaults, "asg_max_size"))}"
5+
min_size = "${lookup(var.worker_groups[count.index], "asg_min_size",lookup(local.workers_group_defaults, "asg_min_size"))}"
66
launch_configuration = "${element(aws_launch_configuration.workers.*.id, count.index)}"
77
vpc_zone_identifier = ["${split(",", coalesce(lookup(var.worker_groups[count.index], "subnets", ""), join(",", var.subnets)))}"]
88
count = "${var.worker_group_count}"
@@ -23,26 +23,26 @@ resource "aws_autoscaling_group" "workers" {
2323

2424
resource "aws_launch_configuration" "workers" {
2525
name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups[count.index], "name", count.index)}"
26-
associate_public_ip_address = "${lookup(var.worker_groups[count.index], "public_ip", lookup(var.workers_group_defaults, "public_ip"))}"
26+
associate_public_ip_address = "${lookup(var.worker_groups[count.index], "public_ip", lookup(local.workers_group_defaults, "public_ip"))}"
2727
security_groups = ["${local.worker_security_group_id}"]
2828
iam_instance_profile = "${aws_iam_instance_profile.workers.id}"
2929
image_id = "${lookup(var.worker_groups[count.index], "ami_id", data.aws_ami.eks_worker.id)}"
30-
instance_type = "${lookup(var.worker_groups[count.index], "instance_type", lookup(var.workers_group_defaults, "instance_type"))}"
31-
key_name = "${lookup(var.worker_groups[count.index], "key_name", lookup(var.workers_group_defaults, "key_name"))}"
30+
instance_type = "${lookup(var.worker_groups[count.index], "instance_type", lookup(local.workers_group_defaults, "instance_type"))}"
31+
key_name = "${lookup(var.worker_groups[count.index], "key_name", lookup(local.workers_group_defaults, "key_name"))}"
3232
user_data_base64 = "${base64encode(element(data.template_file.userdata.*.rendered, count.index))}"
33-
ebs_optimized = "${lookup(var.worker_groups[count.index], "ebs_optimized", lookup(local.ebs_optimized, lookup(var.worker_groups[count.index], "instance_type", lookup(var.workers_group_defaults, "instance_type")), false))}"
34-
enable_monitoring = "${lookup(var.worker_groups[count.index], "enable_monitoring", lookup(var.workers_group_defaults, "enable_monitoring"))}"
35-
spot_price = "${lookup(var.worker_groups[count.index], "spot_price", lookup(var.workers_group_defaults, "spot_price"))}"
33+
ebs_optimized = "${lookup(var.worker_groups[count.index], "ebs_optimized", lookup(local.ebs_optimized, lookup(var.worker_groups[count.index], "instance_type", lookup(local.workers_group_defaults, "instance_type")), false))}"
34+
enable_monitoring = "${lookup(var.worker_groups[count.index], "enable_monitoring", lookup(local.workers_group_defaults, "enable_monitoring"))}"
35+
spot_price = "${lookup(var.worker_groups[count.index], "spot_price", lookup(local.workers_group_defaults, "spot_price"))}"
3636
count = "${var.worker_group_count}"
3737

3838
lifecycle {
3939
create_before_destroy = true
4040
}
4141

4242
root_block_device {
43-
volume_size = "${lookup(var.worker_groups[count.index], "root_volume_size", lookup(var.workers_group_defaults, "root_volume_size"))}"
44-
volume_type = "${lookup(var.worker_groups[count.index], "root_volume_type", lookup(var.workers_group_defaults, "root_volume_type"))}"
45-
iops = "${lookup(var.worker_groups[count.index], "root_iops", lookup(var.workers_group_defaults, "root_iops"))}"
43+
volume_size = "${lookup(var.worker_groups[count.index], "root_volume_size", lookup(local.workers_group_defaults, "root_volume_size"))}"
44+
volume_type = "${lookup(var.worker_groups[count.index], "root_volume_type", lookup(local.workers_group_defaults, "root_volume_type"))}"
45+
iops = "${lookup(var.worker_groups[count.index], "root_iops", lookup(local.workers_group_defaults, "root_iops"))}"
4646
delete_on_termination = true
4747
}
4848
}

0 commit comments

Comments
 (0)