Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Last active August 25, 2022 00:23
Show Gist options
  • Save nitrocode/715d262cf705b1bf1fa5930b88e795e3 to your computer and use it in GitHub Desktop.
Save nitrocode/715d262cf705b1bf1fa5930b88e795e3 to your computer and use it in GitHub Desktop.
Return the correct apiVersion for the corresponding awscli version

k8s-api-version-for-awscli-version

$ get-api-version 1.23.8
v1alpha1
$ get-api-version 1.23.9
v1beta1
$ get-api-version 2.6.4
v1alpha1
$ get-api-version 2.7.0
v1beta1
#!/usr/bin/env sh
get-api-version() {
current_version=$1
current_version_major=$(echo "${current_version}" | cut -d'.' -f1)
# awscli v2.7.0, v1.23.9 defaults to an apiVersion of v1beta1, older versions use v1alpha1
# https://github.com/aws/aws-cli/blob/bf65dae5085176420a59ef3d944c397174e6a885/CHANGELOG.rst#L440
# https://github.com/aws/aws-cli/blob/bf65dae5085176420a59ef3d944c397174e6a885/CHANGELOG.rst#L4691
last_version_before_v1beta_awsv1="1.23.8"
last_version_before_v1beta_awsv2="2.6.4"
# if current major version is v2 or greater
if [ "$current_version_major" -ge "2" ]; then
last_version_before_v1beta=${last_version_before_v1beta_awsv2}
else
last_version_before_v1beta=${last_version_before_v1beta_awsv1}
fi
# sort algorithm puts the earliest version first
# ref: https://stackoverflow.com/a/25731924
get_earliest_version=$(printf '%s\n%s' "${last_version_before_v1beta}" "${current_version}" | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -g | head -1)
# if the last_version_before_v1beta < current_version, then get_earliest_version will equal current_version
# thus use v1alpha1 (old), otherwise use v1beta1 (new)
if [ "$get_earliest_version" = "$current_version" ]; then
api_version="v1alpha1"
else
api_version="v1beta1"
fi
echo $api_version
}
current_version=$(aws --version | cut -d'/' -f2 | cut -d' ' -f1)
get-api-version ${current_version}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment