Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packer build failed with connection refused & Redirection (301) without location #13227

Open
eravindar12 opened this issue Dec 12, 2024 · 0 comments
Labels

Comments

@eravindar12
Copy link

Overview of the Issue

Packer build failed with connection refused, can't SSH

Error

2024/12/12 00:38:20 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:20 [ERROR] reconnection error: dial tcp 10.0.158.108:22: connect: connection refused

Packer version :

Packer version: 1.11.2 [go1.21.12 linux amd64]

command:

$ export PACKER_LOG=1
$ make 1.29 aws_region="us-east-1" source_ami_id="ami-xxxx" source_ami_owners=67xxxx](https://gitlab.com/xxx-xxx/xxx/xxx-infra/xxx-eks-ami-creation/-/jobs/8618705150#L32)41 source_ami_filter_name="CIS Amazon EKS Optimized Amazon Linux 2 Benchmark - Kubernetes v1.31 - Level 1 - v10 -prod-m4rb3t4ugztyi" kubernetes_build_date="2024-11-15" kubernetes_version="1.29.10" subnet_id="subnet-0a7xxxx" vpc_id="vpc-xxxx"

packer logs

2024/12/12 00:38:14 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:14 [INFO] RPC endpoint: Communicator ended with: 2300218
2024/12/12 00:38:14 [INFO] 0 bytes written for 'stdout'
2024/12/12 00:38:14 [INFO] 0 bytes written for 'stderr'
2024/12/12 00:38:14 [INFO] RPC client: Communicator ended with: 2300218
2024/12/12 00:38:14 [INFO] RPC endpoint: Communicator ended with: 2300218
2024/12/12 00:38:14 packer-provisioner-shell plugin: [INFO] 0 bytes written for 'stdout'
2024/12/12 00:38:14 packer-provisioner-shell plugin: [INFO] 0 bytes written for 'stderr'
2024/12/12 00:38:14 packer-provisioner-shell plugin: [INFO] RPC client: Communicator ended with: 2300218
2024/12/12 00:38:14 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:14 [DEBUG] Opening new ssh session
2024/12/12 00:38:14 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:14 [ERROR] ssh session open error: 'EOF', attempting reconnect
2024/12/12 00:38:14 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:14 [DEBUG] reconnecting to TCP connection for SSH
2024/12/12 00:38:14 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:14 [DEBUG] handshaking with SSH
2024/12/12 00:38:14 packer-provisioner-shell plugin: Retryable error: Error removing temporary script at /home/ec2-user/script_2508.sh: ssh: handshake failed: EOF
2024/12/12 00:38:16 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:16 [DEBUG] Opening new ssh session
2024/12/12 00:38:16 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:16 [ERROR] ssh session open error: 'client not available', attempting reconnect
2024/12/12 00:38:16 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:16 [DEBUG] reconnecting to TCP connection for SSH
2024/12/12 00:38:16 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:16 [ERROR] reconnection error: dial tcp 10.0.158.108:22: connect: connection refused
2024/12/12 00:38:16 packer-provisioner-shell plugin: Retryable error: Error removing temporary script at /home/ec2-user/script_2508.sh: dial tcp 10.0.158.108:22: connect: connection refused
2024/12/12 00:38:18 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:18 [DEBUG] Opening new ssh session
2024/12/12 00:38:18 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:18 [ERROR] ssh session open error: 'client not available', attempting reconnect
2024/12/12 00:38:18 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:18 [DEBUG] reconnecting to TCP connection for SSH
2024/12/12 00:38:18 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:18 [ERROR] reconnection error: dial tcp 10.0.158.108:22: connect: connection refused
2024/12/12 00:38:18 packer-provisioner-shell plugin: Retryable error: Error removing temporary script at /home/ec2-user/script_2508.sh: dial tcp 10.0.158.108:22: connect: connection refused
2024/12/12 00:38:20 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:20 [DEBUG] Opening new ssh session
2024/12/12 00:38:20 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:20 [ERROR] ssh session open error: 'client not available', attempting reconnect
2024/12/12 00:38:20 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:20 [DEBUG] reconnecting to TCP connection for SSH
2024/12/12 00:38:20 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:20 [ERROR] reconnection error: dial tcp 10.0.158.108:22: connect: connection refused
2024/12/12 00:38:20 packer-provisioner-shell plugin: Retryable error: Error removing temporary script at /home/ec2-user/script_2508.sh: dial tcp 10.0.158.108:22: connect: connection refused
2024/12/12 00:38:22 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:22 [DEBUG] Opening new ssh session
2024/12/12 00:38:22 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:22 [ERROR] ssh session open error: 'client not available', attempting reconnect
2024/12/12 00:38:22 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:22 [DEBUG] reconnecting to TCP connection for SSH
2024/12/12 00:38:37 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:37 [ERROR] reconnection error: dial tcp 10.0.158.108:22: i/o timeout
2024/12/12 00:38:37 packer-provisioner-shell plugin: Retryable error: Error removing temporary script at /home/ec2-user/script_2508.sh: dial tcp 10.0.158.108:22: i/o timeout
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [DEBUG] Opening new ssh session
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [ERROR] ssh session open error: 'client not available', attempting reconnect
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [DEBUG] reconnecting to TCP connection for SSH
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [DEBUG] handshaking with SSH
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [DEBUG] handshake complete!
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [INFO] no local agent socket, will not connect agent
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [DEBUG] starting remote command: rm -f /home/ec2-user/script_2508.sh
2024/12/12 00:38:39 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:38:39 [INFO] RPC endpoint: Communicator ended with: 0
2024/12/12 00:38:39 [INFO] RPC client: Communicator ended with: 0
2024/12/12 00:38:39 [INFO] RPC endpoint: Communicator ended with: 0
2024/12/12 00:38:39 packer-provisioner-shell plugin: [INFO] RPC client: Communicator ended with: 0
2024-12-12T00:41:03Z:     amazon-ebs: HTTP request sent, awaiting response... 301 Moved Permanently
2024-12-12T00:41:03Z:     amazon-ebs: Location: unspecified
2024-12-12T00:41:03Z:     amazon-ebs: ERROR: Redirection (301) without location.
2024/12/12 00:41:03 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:41:03 [ERROR] Remote command exited with '8': AWS_ACCESS_KEY_ID='' AWS_SECRET_ACCESS_KEY='Iee3g1I+hyUtJvpN5kUEkS40yUvlU+uOfOMGHnVW' AWS_SESSION_TOKEN='' BINARY_BUCKET_NAME='amazon-eks' BINARY_BUCKET_REGION='us-east-1' CACHE_CONTAINER_IMAGES='false' CNI_PLUGIN_VERSION='v1.2.0' CONTAINERD_VERSION='1.7.*' DOCKER_VERSION='none' KUBERNETES_BUILD_DATE='2024-11-15' KUBERNETES_VERSION='1.29.10' PACKER_BUILDER_TYPE='amazon-ebs' PACKER_BUILD_NAME='amazon-ebs' PAUSE_CONTAINER_VERSION='3.5' PULL_CNI_FROM_GITHUB='true' RUNC_VERSION='1.1.*' SSM_AGENT_VERSION='' WORKING_DIR='/home/ec2-user/worker'  bash '/home/ec2-user/script_3952.sh'
2024/12/12 00:41:03 packer-plugin-amazon_v1.3.3_x5.0_linux_amd64 plugin: 2024/12/12 00:41:03 [INFO] RPC endpoint: Communicator ended with: 8

2024-12-12T00:41:03Z: ==> amazon-ebs: Provisioning step had errors: Running the cleanup provisioner, if present...

==> Some builds didn't complete successfully and had errors:
2024/12/12 00:42:05 machine readable: amazon-ebs,error []string{"Script exited with non-zero exit status: 8. Allowed exit codes are: [0]"}

eks-worker-al2.json

{
  "_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": null,
    "ami_component_description": null,
    "ami_description": null,
    "ami_name": null,
    "ami_regions": null,
    "ami_users": null,
    "arch": null,
    "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,
    "enable_fips": null,
    "instance_type": null,
    "kernel_version": null,
    "kms_key_id": null,
    "kubernetes_build_date": null,
    "kubernetes_version": null,
    "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,
    "source_ami_filter_name": null,
    "source_ami_id": null,
    "source_ami_owners": null,
    "ssh_interface": null,
    "ssh_username": null,
    "ssm_agent_version": null,
    "subnet_id": null,
    "temporary_security_group_source_cidrs": null,
    "volume_type": null,
    "working_dir": null
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "region": "{{user `aws_region`}}",
      "source_ami": "{{user `source_ami_id`}}",
      "ami_users": "{{user `ami_users`}}",
      "snapshot_users": "{{user `ami_users`}}",
      "source_ami_filter": {
        "filters": {
          "name": "{{user `source_ami_filter_name`}}",
          "architecture": "{{user `arch`}}",
          "root-device-type": "ebs",
          "state": "available",
          "virtualization-type": "hvm"
        },
        "owners": [
          "{{user `source_ami_owners`}}"
        ],
        "most_recent": true
      },
      "instance_type": "{{user `instance_type`}}",
      "launch_block_device_mappings": [
        {
          "device_name": "/dev/xvda",
          "volume_type": "{{user `volume_type`}}",
          "volume_size": "{{user `launch_block_device_mappings_volume_size`}}",
          "delete_on_termination": true
        }
      ],
      "ami_block_device_mappings": [
        {
          "device_name": "/dev/xvda",
          "volume_type": "{{user `volume_type`}}",
          "volume_size": 20,
          "delete_on_termination": true
        }
      ],
      "aws_polling": {
        "delay_seconds": 30,
        "max_attempts": 90
      },
      "ami_regions": "{{user `ami_regions`}}",
      "ssh_username": "{{user `ssh_username`}}",
      "ssh_interface": "{{user `ssh_interface`}}",
      "temporary_security_group_source_cidrs": "{{user `temporary_security_group_source_cidrs`}}",
      "security_group_id": "{{user `security_group_id`}}",
      "associate_public_ip_address": "{{user `associate_public_ip_address`}}",
      "ssh_pty": true,
      "encrypt_boot": "{{user `encrypted`}}",
      "kms_key_id": "{{user `kms_key_id`}}",
      "run_tags": {
        "creator": "{{user `creator`}}"
      },
      "subnet_id": "{{user `subnet_id`}}",
      "tags": {
        "Name": "{{user `ami_name`}}",
        "created": "{{timestamp}}",
        "build_region": "{{ .BuildRegion }}",
        "source_ami_id": "{{ .SourceAMI }}",
        "source_ami_name": "{{ .SourceAMIName }}",
        "docker_version": "{{ user `docker_version`}}",
        "containerd_version": "{{ user `containerd_version`}}",
        "kubernetes": "{{ user `kubernetes_version`}}/{{ user `kubernetes_build_date` }}/bin/linux/{{ user `arch` }}",
        "cni_plugin_version": "{{ user `cni_plugin_version`}}",
        "ssm_agent_version": "{{ user `ssm_agent_version`}}"
      },
      "ami_name": "{{user `ami_name`}}",
      "ami_description": "{{ user `ami_description` }}, {{ user `ami_component_description` }}",
      "metadata_options": {
        "http_tokens": "required"
      }
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "inline": [
        "mkdir -p {{user `working_dir`}}",
        "mkdir -p {{user `working_dir`}}/log-collector-script"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/install_additional_repos.sh",
      "environment_vars": [
        "ADDITIONAL_YUM_REPOS={{user `additional_yum_repos`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "file",
      "source": "{{template_dir}}/files/",
      "destination": "{{user `working_dir`}}"
    },
    {
      "type": "file",
      "source": "{{template_dir}}/log-collector-script/linux/",
      "destination": "{{user `working_dir`}}/log-collector-script/"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "inline": [
        "sudo chmod -R a+x {{user `working_dir`}}/bin/",
        "sudo mv {{user `working_dir`}}/bin/* /usr/bin/"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/upgrade_kernel.sh",
      "environment_vars": [
        "KUBERNETES_VERSION={{user `kubernetes_version`}}",
        "KERNEL_VERSION={{user `kernel_version`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/enable-fips.sh",
      "environment_vars": [
        "ENABLE_FIPS={{user `enable_fips`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "inline": ["sudo reboot"],
      "expect_disconnect": true,
      "pause_after": "90s", 
      "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/install-worker.sh",
      "environment_vars": [
        "KUBERNETES_VERSION={{user `kubernetes_version`}}",
        "KUBERNETES_BUILD_DATE={{user `kubernetes_build_date`}}",
        "BINARY_BUCKET_NAME={{user `binary_bucket_name`}}",
        "BINARY_BUCKET_REGION={{user `binary_bucket_region`}}",
        "DOCKER_VERSION={{user `docker_version`}}",
        "CONTAINERD_VERSION={{user `containerd_version`}}",
        "RUNC_VERSION={{user `runc_version`}}",
        "CNI_PLUGIN_VERSION={{user `cni_plugin_version`}}",
        "PULL_CNI_FROM_GITHUB={{user `pull_cni_from_github`}}",
        "AWS_ACCESS_KEY_ID={{user `aws_access_key_id`}}",
        "AWS_SECRET_ACCESS_KEY={{user `aws_secret_access_key`}}",
        "AWS_SESSION_TOKEN={{user `aws_session_token`}}",
        "PAUSE_CONTAINER_VERSION={{user `pause_container_version`}}",
        "CACHE_CONTAINER_IMAGES={{user `cache_container_images`}}",
        "WORKING_DIR={{user `working_dir`}}",
        "SSM_AGENT_VERSION={{user `ssm_agent_version`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/cleanup.sh", 
      "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/cleanup_additional_repos.sh",
      "environment_vars": [
        "ADDITIONAL_YUM_REPOS={{user `additional_yum_repos`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/validate.sh",
      "environment_vars": [
        "KERNEL_VERSION={{user `kernel_version`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "script": "{{template_dir}}/scripts/generate-version-info.sh",
      "execute_command": "chmod +x {{ .Path }}; {{ .Path }} {{user `working_dir`}}/version-info.json",
      "environment_vars": [
        "CACHE_CONTAINER_IMAGES={{user `cache_container_images`}}"
      ]
    },
    {
      "type": "file",
      "direction": "download",
      "source": "{{user `working_dir`}}/version-info.json",
      "destination": "{{ user `ami_name` }}-version-info.json"
    },
    {
      "type": "shell",
      "remote_folder": "{{ user `remote_folder`}}",
      "inline": [
        "rm -rf {{user `working_dir`}}"
      ], "execute_command": "{{.Vars}} bash '{{.Path}}'"
    }
  ],
  "post-processors": [
    {
      "type": "manifest",
      "output": "manifest.json",
      "strip_path": true,
      "custom_data": {
        "source_ami_name": "{{ build `SourceAMIName` }}",
        "source_ami_id": "{{ build `SourceAMI` }}"
      }
    },
    {
      "type": "manifest",
      "output": "{{user `ami_name`}}-manifest.json",
      "strip_path": true,
      "custom_data": {
        "source_ami_name": "{{ build `SourceAMIName` }}",
        "source_ami_id": "{{ build `SourceAMI` }}"
      }
    }
  ]
}

manifest.json

{
  "builds": [
    {
      "name": "amazon-ebs",
      "builder_type": "amazon-ebs",
      "build_time": 1701888229,
      "files": null,
      "artifact_id": "",
      "packer_run_uuid": "",
      "custom_data": {
        "source_ami_id": "ami-0d9bc44ba053d93dc",
        "source_ami_name": "CIS Amazon EKS Optimized Amazon Linux 2 Benchmark - Kubernetes v1.31 - Level 1 - v10 -prod-m4rb3t4ugztyi"
      }
    }

  ],
  
  "last_run_uuid": "35aae120-2239-6e1b-abc1-3d0d6c54ae02"
}

Makefile

MAKEFILE_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

PACKER_DEFAULT_VARIABLE_FILE ?= $(MAKEFILE_DIR)/eks-worker-al2-variables.json
PACKER_TEMPLATE_FILE ?= $(MAKEFILE_DIR)/eks-worker-al2.json
PACKER_BINARY ?= packer
AVAILABLE_PACKER_VARIABLES := $(shell $(PACKER_BINARY) inspect -machine-readable $(PACKER_TEMPLATE_FILE) | 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})

# expands to 'true' if PACKER_VARIABLE_FILE is non-empty
# and the file contains the string passed as the first argument
# otherwise, expands to 'false'
packer_variable_file_contains = $(if $(PACKER_VARIABLE_FILE),$(shell grep -Fq $1 $(PACKER_VARIABLE_FILE) && echo true || echo false),false)

# expands to 'true' if the version comparison is affirmative
# otherwise expands to 'false'
vercmp = $(shell $(MAKEFILE_DIR)/files/bin/vercmp "$1" "$2" "$3")

# Docker is not present on 1.25+ AMI's
# TODO: remove this when 1.24 reaches EOL
ifeq ($(call vercmp,$(kubernetes_version),gteq,1.25.0), true)
	# do not tag the AMI with the Docker version
	docker_version ?= none
	# do not include the Docker version in the AMI description
	ami_component_description ?= (k8s: {{ user `kubernetes_version` }}, containerd: {{ user `containerd_version` }})
endif

AMI_VERSION ?= v$(shell date '+%Y%m%d')
AMI_VARIANT ?= amazon-eks
ifneq (,$(findstring al2023, $(PACKER_TEMPLATE_FILE)))
	AMI_VARIANT := $(AMI_VARIANT)-al2023
endif
arch ?= x86_64
ifeq ($(arch), arm64)
	instance_type ?= m6g.large
	AMI_VARIANT := $(AMI_VARIANT)-arm64
else
	instance_type ?= m5.large
endif
ifeq ($(enable_fips), true)
	AMI_VARIANT := $(AMI_VARIANT)-fips
endif
ami_name ?= $(AMI_VARIANT)-node-$(K8S_VERSION_MINOR)-$(AMI_VERSION)

ifeq ($(aws_region), cn-northwest-1)
	source_ami_owners ?= 141808717104
endif

ifeq ($(aws_region), us-gov-west-1)
	source_ami_owners ?= 045324592363
endif

T_RED := \e[0;31m
T_GREEN := \e[0;32m
T_YELLOW := \e[0;33m
T_RESET := \e[0m

# default to the latest supported Kubernetes version
k8s=1.29

.PHONY: build
build: ## Build EKS Optimized AL2 AMI
	$(MAKE) k8s $(shell hack/latest-binaries.sh $(k8s))

# ensure that these flags are equivalent to the rules in the .editorconfig
SHFMT_FLAGS := --list \
--language-dialect auto \
--indent 2 \
--binary-next-line \
--case-indent \
--space-redirects

SHFMT_COMMAND := $(shell which shfmt)
ifeq (, $(SHFMT_COMMAND))
	SHFMT_COMMAND = docker run --rm -v $(MAKEFILE_DIR):$(MAKEFILE_DIR) mvdan/shfmt
endif

.PHONY: fmt
fmt: ## Format the source files
	$(SHFMT_COMMAND) $(SHFMT_FLAGS) --write $(MAKEFILE_DIR)

SHELLCHECK_COMMAND := $(shell which shellcheck)
ifeq (, $(SHELLCHECK_COMMAND))
	SHELLCHECK_COMMAND = docker run --rm -v $(MAKEFILE_DIR):$(MAKEFILE_DIR) koalaman/shellcheck:stable
endif
SHELL_FILES := $(shell find $(MAKEFILE_DIR) -type f -name '*.sh')

.PHONY: transform-al2-to-al2023
transform-al2-to-al2023:
	PACKER_TEMPLATE_FILE=$(PACKER_TEMPLATE_FILE) \
	PACKER_DEFAULT_VARIABLE_FILE=$(PACKER_DEFAULT_VARIABLE_FILE) \
		hack/transform-al2-to-al2023.sh

.PHONY: lint
lint: lint-docs ## Check the source files for syntax and format issues
	$(SHFMT_COMMAND) $(SHFMT_FLAGS) --diff $(MAKEFILE_DIR)
	$(SHELLCHECK_COMMAND) --format gcc --severity error $(SHELL_FILES)
	hack/lint-space-errors.sh

.PHONY: test
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 $(PACKER_DEFAULT_VARIABLE_FILE) \
$(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 $(PACKER_VAR_FLAGS) $(PACKER_TEMPLATE_FILE)

.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 $(PACKER_VAR_FLAGS) $(PACKER_TEMPLATE_FILE)

.PHONY: 1.23
1.23: ## Build EKS Optimized AL2 AMI - K8s 1.23
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.23)

.PHONY: 1.24
1.24: ## Build EKS Optimized AL2 AMI - K8s 1.24
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.24)

.PHONY: 1.25
1.25: ## Build EKS Optimized AL2 AMI - K8s 1.25
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.25)

.PHONY: 1.26
1.26: ## Build EKS Optimized AL2 AMI - K8s 1.26
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.26)

.PHONY: 1.27
1.27: ## Build EKS Optimized AL2 AMI - K8s 1.27
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.27)

.PHONY: 1.28
1.28: ## Build EKS Optimized AL2 AMI - K8s 1.28
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.28)

.PHONY: 1.29
1.29: ## Build EKS Optimized AL2 AMI - K8s 1.29
	$(MAKE) k8s $(shell hack/latest-binaries.sh 1.29)

.PHONY: lint-docs
lint-docs: ## Lint the docs
	hack/lint-docs.sh

.PHONY: clean
clean:
	rm *-manifest.json
	rm *-version-info.json

.PHONY: help
help: ## Display help
	@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n  make \033[36m<target>\033[0m\n"} /^[\.a-zA-Z_0-9\-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant