Skip to content

Commit

Permalink
Move variable defaults to -var-file (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermckinnon authored Nov 21, 2022
1 parent 21870b9 commit 5fcd34e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 38 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PACKER_BINARY ?= packer
PACKER_VARIABLES := $(shell $(PACKER_BINARY) inspect -machine-readable eks-worker-al2.json | grep 'template-variable' | awk -F ',' '{print $$4}')
AVAILABLE_PACKER_VARIABLES := $(shell $(PACKER_BINARY) inspect -machine-readable eks-worker-al2.json | grep 'template-variable' | awk -F ',' '{print $$4}')

K8S_VERSION_PARTS := $(subst ., ,$(kubernetes_version))
K8S_VERSION_MINOR := $(word 1,${K8S_VERSION_PARTS}).$(word 2,${K8S_VERSION_PARTS})
Expand Down Expand Up @@ -65,14 +65,20 @@ lint: ## Check the source files for syntax and format issues
test: ## run the test-harness
test/test-harness.sh

# include only variables which have a defined value
PACKER_VARIABLES := $(foreach packerVar,$(AVAILABLE_PACKER_VARIABLES),$(if $($(packerVar)),$(packerVar)))
PACKER_VAR_FLAGS := -var-file eks-worker-al2-variables.json \
$(if $(PACKER_VARIABLE_FILE),--var-file=$(PACKER_VARIABLE_FILE),) \
$(foreach packerVar,$(PACKER_VARIABLES),-var $(packerVar)='$($(packerVar))')

.PHONY: validate
validate: ## Validate packer config
$(PACKER_BINARY) validate $(foreach packerVar,$(PACKER_VARIABLES), $(if $($(packerVar)),--var $(packerVar)='$($(packerVar))',)) eks-worker-al2.json
$(PACKER_BINARY) validate $(PACKER_VAR_FLAGS) eks-worker-al2.json

.PHONY: k8s
k8s: validate ## Build default K8s version of EKS Optimized AL2 AMI
@echo "$(T_GREEN)Building AMI for version $(T_YELLOW)$(kubernetes_version)$(T_GREEN) on $(T_YELLOW)$(arch)$(T_RESET)"
$(PACKER_BINARY) build -timestamp-ui -color=false $(foreach packerVar,$(PACKER_VARIABLES), $(if $($(packerVar)),--var $(packerVar)='$($(packerVar))',)) eks-worker-al2.json
$(PACKER_BINARY) build -timestamp-ui -color=false $(PACKER_VAR_FLAGS) eks-worker-al2.json

# Build dates and versions taken from https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ To build an Amazon EKS Worker AMI for a particular Kubernetes version run the fo
```bash
make 1.23 ## Build a Amazon EKS Worker AMI for k8s 1.23
```

### AMI template variables

Default values for most variables are defined in [a default variable file](eks-worker-al2-variables.json).

Users have the following options for specifying their own values:

1. Provide a variable file with the `PACKER_VARIABLE_FILE` argument to `make`. Values in this file will override values in the default variable file. Your variable file does not need to include all possible variables, as it will be merged with the default variable file.
2. Pass a key-value pair for any template variable to `make`. These values will override any values specified using the first method.

**Note** that some variables (such as `arch` and `kubernetes_version`) do not have a sensible, static default, and are satisfied by the Makefile. Such variables do not appear in the default variable file, and must be overridden (if necessary) by the second method described above.

### Building against other versions of Kubernetes binaries
To build an Amazon EKS Worker AMI with other versions of Kubernetes that are not listed above run the following AWS Command
Line Interface (AWS CLI) commands to obtain values for KUBERNETES_VERSION, KUBERNETES_BUILD_DATE, PLATFORM, ARCH from S3
Expand Down
36 changes: 36 additions & 0 deletions eks-worker-al2-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"additional_yum_repos": "",
"ami_description": "EKS Kubernetes Worker AMI with AmazonLinux2 image",
"ami_regions": "",
"ami_users": "",
"associate_public_ip_address": "",
"aws_access_key_id": "{{env `AWS_ACCESS_KEY_ID`}}",
"aws_region": "us-west-2",
"aws_secret_access_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
"aws_session_token": "{{env `AWS_SESSION_TOKEN`}}",
"binary_bucket_name": "amazon-eks",
"binary_bucket_region": "us-west-2",
"cache_container_images": "false",
"cni_plugin_version": "v0.8.6",
"containerd_version": "1.6.6-1.amzn2.0.2",
"creator": "{{env `USER`}}",
"docker_version": "20.10.17-1.amzn2.0.1",
"encrypted": "false",
"kernel_version": "",
"kms_key_id": "",
"launch_block_device_mappings_volume_size": "8",
"pause_container_version": "3.5",
"pull_cni_from_github": "true",
"remote_folder": "",
"runc_version": "1.1.3-1.amzn2.0.2",
"security_group_id": "",
"sonobuoy_e2e_registry": "",
"source_ami_filter_name": "amzn2-ami-minimal-hvm-*",
"source_ami_id": "",
"source_ami_owners": "137112412989",
"ssh_interface": "",
"ssh_username": "ec2-user",
"subnet_id": "",
"temporary_security_group_source_cidrs": "",
"volume_type": "gp2"
}
71 changes: 36 additions & 35 deletions eks-worker-al2.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
{
"_comment": "All template variables are enumerated here; and most variables have a default value defined in eks-worker-al2-variables.json",
"variables": {
"additional_yum_repos": "",
"ami_description": "EKS Kubernetes Worker AMI with AmazonLinux2 image",
"additional_yum_repos": null,
"ami_description": null,
"ami_name": null,
"ami_regions": "",
"ami_users": "",
"ami_regions": null,
"ami_users": null,
"arch": null,
"associate_public_ip_address": "",
"aws_access_key_id": "{{env `AWS_ACCESS_KEY_ID`}}",
"aws_region": "us-west-2",
"aws_secret_access_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
"aws_session_token": "{{env `AWS_SESSION_TOKEN`}}",
"binary_bucket_name": "amazon-eks",
"binary_bucket_region": "us-west-2",
"cache_container_images": "false",
"cni_plugin_version": "v0.8.6",
"containerd_version": "1.6.6-1.amzn2.0.2",
"creator": "{{env `USER`}}",
"docker_version": "20.10.17-1.amzn2.0.1",
"encrypted": "false",
"associate_public_ip_address": null,
"aws_access_key_id": null,
"aws_region": null,
"aws_secret_access_key": null,
"aws_session_token": null,
"binary_bucket_name": null,
"binary_bucket_region": null,
"cache_container_images": null,
"cni_plugin_version": null,
"containerd_version": null,
"creator": null,
"docker_version": null,
"encrypted": null,
"instance_type": null,
"kernel_version": "",
"kms_key_id": "",
"kernel_version": null,
"kms_key_id": null,
"kubernetes_build_date": null,
"kubernetes_version": null,
"launch_block_device_mappings_volume_size": "8",
"pause_container_version": "3.5",
"pull_cni_from_github": "true",
"remote_folder": "",
"runc_version": "1.1.3-1.amzn2.0.2",
"security_group_id": "",
"sonobuoy_e2e_registry": "",
"source_ami_filter_name": "amzn2-ami-minimal-hvm-*",
"source_ami_id": "",
"source_ami_owners": "137112412989",
"ssh_interface": "",
"ssh_username": "ec2-user",
"subnet_id": "",
"temporary_security_group_source_cidrs": "",
"volume_type": "gp2"
"launch_block_device_mappings_volume_size": null,
"pause_container_version": null,
"pull_cni_from_github": null,
"remote_folder": null,
"runc_version": null,
"security_group_id": null,
"sonobuoy_e2e_registry": null,
"source_ami_filter_name": null,
"source_ami_id": null,
"source_ami_owners": null,
"ssh_interface": null,
"ssh_username": null,
"subnet_id": null,
"temporary_security_group_source_cidrs": null,
"volume_type": null
},
"builders": [
{
Expand Down Expand Up @@ -223,4 +224,4 @@
}
}
]
}
}

0 comments on commit 5fcd34e

Please sign in to comment.