Skip to content

Commit

Permalink
Add update_bundle script to get latest version and update input bundl…
Browse files Browse the repository at this point in the history
…e for staging and prod
  • Loading branch information
jhaanvi5 committed Nov 7, 2024
1 parent 3cb6452 commit 0bd984b
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions generatebundlefile/hack/update_bundles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/bin/bash

set -e
set -o pipefail

BASE_DIRECTORY=$(git rev-parse --show-toplevel)
# Function to display usage information
function usage() {
echo "Usage: $0 -e <environment>"
echo " -e Specify the environment (staging or prod)"
exit 1
}

# Function to check if required tools are installed
function check_requirements() {
local required_tools=("aws" "jq" "yq")
for tool in "${required_tools[@]}"; do
if ! command -v "$tool" &> /dev/null; then
echo "Error: $tool is not installed or not in PATH" >&2
exit 1
fi
done
}

# Function to get the latest version from ECR
function get_latest_version() {
local registry=$1
local repo=$2
local k8s_version=$3

local json_output=$(aws ecr describe-images \
--registry-id "$registry" \
--repository-name "$repo" \
--output json)

# For cluster autoscaler filter tag based on k8s version (1.28,1.29,etc)
if [ "$repo" = "cluster-autoscaler/charts/cluster-autoscaler" ]; then
output=$(echo $json_output | jq -r --arg kv "1.$k8s_version" '
.imageDetails
| map(select(.imageTags | length > 0))
| map(select(.imageTags | map(test($kv)) | any))
| sort_by(.imagePushedAt)
| last
| .imageTags')
# For metrics-server filter tag based on k8s version (1-28,1-29,etc)
elif [ "$repo" = "metrics-server/charts/metrics-server" ]; then
output=$(echo $json_output | jq -r --arg kv "1-$k8s_version" '
.imageDetails
| map(select(.imageTags | length > 0))
| map(select(.imageTags | map(test($kv)) | any))
| sort_by(.imagePushedAt)
| last
| .imageTags')
else
output=$(echo $json_output | jq -r '
.imageDetails
| sort_by(.imagePushedAt)
| last(.[]).imageTags')
fi

# Convert tags to array
local versions=()
for value in $(echo "$output" | jq -r '.[]'); do
versions+=("$value")
done

# Get latest version and remove tags with helm
local latest_version=$(printf "%s\n" "${versions[@]}" | \
grep -v "helm" | \
sort -rV | \
head -n 1)

echo $latest_version
}


# Function to update input bundle file for a given Kubernetes version
function update_bundle() {
k8s_version=$1
environment=$2
k8s_yaml_file="${BASE_DIRECTORY}/generatebundlefile/data/bundles_$environment/1-$k8s_version.yaml"
packages=$(yq e '.packages' "$k8s_yaml_file")
package_count=$(echo "$packages" | yq e 'length' -)

echo "Updating input file for kubernetes version: 1-$k8s_version"
# Iterate over all packages in yaml file
for package_index in $(seq 0 $(($package_count - 1))); do
package=$(echo "$packages" | yq e ".[$package_index]" -)
org=$(echo "$package" | yq e '.org' -)
projects=$(echo "$package" | yq e '.projects' -)
project_count=$(echo "$projects" | yq e 'length' -)

# Iterate over each project in org
for project_index in $(seq 0 $((project_count - 1))); do
project=$(echo "$projects" | yq e ".[$project_index]" -)

# Extract registry and repository from each project
registry=$(echo "$project" | yq e '.registry' -)
repository=$(echo "$project" | yq e '.repository' -)

# Get the latest version for the repository
latest_tag=$(get_latest_version $registry $repository $k8s_version)

if [ "$latest_tag" == "None" ]; then
echo "No tags found for repository: $repository in registry $registry. Skipping..."
continue
fi

yq e -i ".packages[$package_index].projects[$project_index].versions[0].name |= \"$latest_tag\"" "$k8s_yaml_file"
echo "Updated $org/$repository to latest_tag $latest_tag"
done
done
}

function main() {

local environment=""
# Parse command line options
while getopts "e:" opt; do
case ${opt} in
e )
environment=$OPTARG
;;
\? ) # Invalid Option
usage
exit;;
esac
done

# Check if environment is provided and valid
if [[ -z "$environment" ]]; then
echo "Error: Environment not specified" >&2
usage
fi

if [[ "$environment" != "staging" && "$environment" != "prod" ]]; then
echo "Error: Invalid environment. Use 'staging' or 'prod'" >&2
usage
fi

check_requirements

# Generate bundle files for Kubernetes versions 1.27 to 1.31 for given environment
for version in {27..31}; do
update_bundle "$version" "$environment"
done

echo "Bundle update complete."

}

main "$@"

0 comments on commit 0bd984b

Please sign in to comment.