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

Added an optional kernel parameter test to Packer Build #1929

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.DS_Store
site/
.git-commit
sysctl_logs/
57 changes: 57 additions & 0 deletions templates/al2/provisioners/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,63 @@ validate_file_nonexists '/var/log/cloud-init.log'
validate_file_nonexists '/var/log/secure'
validate_file_nonexists '/var/log/wtmp'

THROW_ERR=$SYSCTL_TEST_THROW_ERR
K_VERSION_ARR=(${KUBERNETES_VERSION//./ })
KUBERNETES_MINOR_VERSION="${K_VERSION_ARR[0]}${K_VERSION_ARR[1]}"

if [[ $AMI_NAME == *"gpu"* ]]; then
AMI_FLAVOR="gpu"
else
AMI_FLAVOR="standard"
fi

SYSCTL_TEST_FILE=$AMI_ALIAS

if [ -f $(pwd)/resources/$SYSCTL_TEST_FILE ]; then
sysctl_cmd=$(eval sudo sysctl -a -e > ./sysctl_log.txt)
while [ ! -f $(pwd)/sysctl_log.txt ]; do sleep 10; done

sysctl_blacklist=()
while IFS= read -r line; do
sysctl_blacklist+=("$line")
done < "$(pwd)/resources/sysctl_blacklist"

diff_command="diff -b"
for pattern in "${sysctl_blacklist[@]}"; do
diff_command+=" -I \"$pattern\""
done

diff_command+=" $(pwd)/resources/$SYSCTL_TEST_FILE"
diff_command+=" $(pwd)/sysctl_log.txt"

if [ -f $(pwd)/sysctl_log.txt ]; then
echo "Executing diff command: $diff_command ..."
if ! eval $diff_command; then
echo "Compared $AMI_NAME : $SYSCTL_TEST_FILE with $(pwd)/sysctl_log.txt"
if $THROW_ERR ; then
echo "Failure: Sysctl has unexpected changes. Either include the new logs in templates/shared/resources/ or undo the kernel parameter changes..."
exit 1
else
echo "Warning: Sysctl has unexpected changes. Either include the new logs in templates/shared/resources/ or undo the kernel parameter changes..."
fi
else
echo "Success: Sysctl has no unexpected changes!"
fi
else
echo "Unhandled error: sysctl_log.txt not generated!"
if $THROW_ERR ; then
exit 1
fi
fi
else
echo "Failure: $(pwd)/resources/$SYSCTL_TEST_FILE does not exist in the list of known AMIs."
if $THROW_ERR ; then
exit 1
fi
fi

echo "Done checking sysctl"

actual_kernel=$(uname -r)
echo "Verifying that kernel version $actual_kernel matches $KERNEL_VERSION..."

Expand Down
24 changes: 22 additions & 2 deletions templates/al2/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
"temporary_security_group_source_cidrs": null,
"user_data_file": null,
"volume_type": null,
"working_dir": null
"working_dir": null,
"sysctl_test_throw_err": null,
"k8s_major_minor": "{{ split (user `kubernetes_version`) `.` 0}}{{split (user `kubernetes_version`) `.` 1}}",
"ami_type": null,
"ami_alias": "{{ user `k8s_major_minor` }}-al2-{{ user `arch` }}-{{ user `ami_type` }}"
},
"builders": [
{
Expand Down Expand Up @@ -221,12 +225,22 @@
"ADDITIONAL_YUM_REPOS={{user `additional_yum_repos`}}"
]
},
{
"destination": "/home/ec2-user/resources",
"source": "{{template_dir}}/../shared/resources",
"type": "file"
},
{
"type": "shell",
"remote_folder": "{{ user `remote_folder`}}",
"script": "{{template_dir}}/provisioners/validate.sh",
"environment_vars": [
"KERNEL_VERSION={{user `kernel_version`}}"
"KUBERNETES_VERSION={{user `kubernetes_version`}}",
"AMI_NAME={{user `ami_name`}}",
"ARCH={{user `arch`}}",
"KERNEL_VERSION={{user `kernel_version`}}",
"AMI_ALIAS={{user `ami_alias`}}",
"SYSCTL_TEST_THROW_ERR={{user `sysctl_test_throw_err`}}"
]
},
{
Expand All @@ -244,6 +258,12 @@
"source": "{{user `working_dir`}}/version-info.json",
"destination": "{{ user `ami_name` }}-version-info.json"
},
{
"type": "file",
"direction": "download",
"source": "/home/ec2-user/sysctl_log.txt",
"destination": "sysctl_logs/{{user `ami_alias`}}"
},
{
"type": "shell",
"remote_folder": "{{ user `remote_folder`}}",
Expand Down
4 changes: 3 additions & 1 deletion templates/al2/variables-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@
"temporary_security_group_source_cidrs": "",
"user_data_file": null,
"volume_type": "gp2",
"working_dir": "{{user `remote_folder`}}/worker"
"working_dir": "{{user `remote_folder`}}/worker",
"sysctl_test_throw_err": "false",
"ami_type": "standard"
}
57 changes: 57 additions & 0 deletions templates/al2023/provisioners/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,63 @@ validate_file_nonexists '/var/log/cloud-init.log'
validate_file_nonexists '/var/log/secure'
validate_file_nonexists '/var/log/wtmp'

THROW_ERR=$SYSCTL_TEST_THROW_ERR
K_VERSION_ARR=(${KUBERNETES_VERSION//./ })
KUBERNETES_MINOR_VERSION="${K_VERSION_ARR[0]}${K_VERSION_ARR[1]}"

if [[ $AMI_NAME == *"gpu"* ]]; then
AMI_FLAVOR="gpu"
else
AMI_FLAVOR="standard"
fi

SYSCTL_TEST_FILE=$AMI_ALIAS

if [ -f $(pwd)/resources/$SYSCTL_TEST_FILE ]; then
sysctl_cmd=$(eval sudo sysctl -a -e > ./sysctl_log.txt)
while [ ! -f $(pwd)/sysctl_log.txt ]; do sleep 10; done

sysctl_blacklist=()
while IFS= read -r line; do
sysctl_blacklist+=("$line")
done < "$(pwd)/resources/sysctl_blacklist"

diff_command="diff -b"
for pattern in "${sysctl_blacklist[@]}"; do
diff_command+=" -I \"$pattern\""
done

diff_command+=" $(pwd)/resources/$SYSCTL_TEST_FILE"
diff_command+=" $(pwd)/sysctl_log.txt"

if [ -f $(pwd)/sysctl_log.txt ]; then
echo "Executing diff command: $diff_command ..."
if ! eval $diff_command; then
echo "Compared $AMI_NAME : $SYSCTL_TEST_FILE with $(pwd)/sysctl_log.txt"
if $THROW_ERR ; then
echo "Failure: Sysctl has unexpected changes. Either include the new logs in templates/shared/resources/ or undo the kernel parameter changes..."
exit 1
else
echo "Warning: Sysctl has unexpected changes. Either include the new logs in templates/shared/resources/ or undo the kernel parameter changes..."
fi
else
echo "Success: Sysctl has no unexpected changes!"
fi
else
echo "Unhandled error: sysctl_log.txt not generated!"
if $THROW_ERR ; then
exit 1
fi
fi
else
echo "Failure: $(pwd)/resources/$SYSCTL_TEST_FILE does not exist in the list of known AMIs."
if $THROW_ERR ; then
exit 1
fi
fi

echo "Done checking sysctl log."

REQUIRED_COMMANDS=(unpigz)

for ENTRY in "${REQUIRED_COMMANDS[@]}"; do
Expand Down
27 changes: 25 additions & 2 deletions templates/al2023/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
"temporary_security_group_source_cidrs": null,
"user_data_file": null,
"volume_type": null,
"working_dir": null
"working_dir": null,
"sysctl_test_throw_err": null,
"k8s_major_minor": "{{ split (user `kubernetes_version`) `.` 0}}{{split (user `kubernetes_version`) `.` 1}}",
"ami_type": null,
"ami_alias": "{{ user `k8s_major_minor` }}-al2023-{{ user `arch` }}-{{ user `ami_type` }}"
},
"builders": [
{
Expand Down Expand Up @@ -201,10 +205,23 @@
"remote_folder": "{{ user `remote_folder`}}",
"script": "{{template_dir}}/../shared/provisioners/cleanup.sh"
},
{
"destination": "/home/ec2-user/resources",
"source": "{{template_dir}}/../shared/resources",
"type": "file"
},
{
"type": "shell",
"remote_folder": "{{ user `remote_folder`}}",
"script": "{{template_dir}}/provisioners/validate.sh"
"script": "{{template_dir}}/provisioners/validate.sh",
"environment_vars": [
"KUBERNETES_VERSION={{user `kubernetes_version`}}",
"AMI_NAME={{user `ami_name`}}",
"ARCH={{user `arch`}}",
"KERNEL_VERSION={{user `kernel_version`}}",
"AMI_ALIAS={{user `ami_alias`}}",
"SYSCTL_TEST_THROW_ERR={{user `sysctl_test_throw_err`}}"
]
},
{
"type": "shell",
Expand All @@ -218,6 +235,12 @@
"source": "{{user `working_dir`}}/version-info.json",
"destination": "{{ user `ami_name` }}-version-info.json"
},
{
"type": "file",
"direction": "download",
"source": "/home/ec2-user/sysctl_log.txt",
"destination": "sysctl_logs/{{user `ami_alias`}}"
},
{
"type": "shell",
"inline": [
Expand Down
4 changes: 3 additions & 1 deletion templates/al2023/variables-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
"temporary_security_group_source_cidrs": "",
"user_data_file": null,
"volume_type": "gp3",
"working_dir": "{{user `remote_folder`}}/worker"
"working_dir": "{{user `remote_folder`}}/worker",
"sysctl_test_throw_err": "false",
"ami_type": "standard"
}
Loading