Skip to content

Commit

Permalink
Fix formatting for script
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaanvi5 committed Nov 7, 2024
1 parent 0bd984b commit 902bb84
Showing 1 changed file with 61 additions and 64 deletions.
125 changes: 61 additions & 64 deletions generatebundlefile/hack/update_bundles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,30 @@ 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
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
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 repo=$1
local k8s_version=$2

local json_output=$(aws ecr describe-images \
--registry-id "$registry" \
--repository-name "$repo" \
--output json)
--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
Expand All @@ -41,7 +39,7 @@ function get_latest_version() {
| map(select(.imageTags | map(test($kv)) | any))
| sort_by(.imagePushedAt)
| last
| .imageTags')
| .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" '
Expand All @@ -56,7 +54,7 @@ function get_latest_version() {
.imageDetails
| sort_by(.imagePushedAt)
| last(.[]).imageTags')
fi
fi

# Convert tags to array
local versions=()
Expand All @@ -65,65 +63,64 @@ function get_latest_version() {
done

# Get latest version and remove tags with helm
local latest_version=$(printf "%s\n" "${versions[@]}" | \
grep -v "helm" | \
sort -rV | \
head -n 1)
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
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 $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;;
e)
environment=$OPTARG
;;
\?) # Invalid Option
usage
exit
;;
esac
done

Expand All @@ -138,6 +135,7 @@ function main() {
usage
fi

# Check if required tools(aws,jq,yq) are installed
check_requirements

# Generate bundle files for Kubernetes versions 1.27 to 1.31 for given environment
Expand All @@ -150,4 +148,3 @@ function main() {
}

main "$@"

0 comments on commit 902bb84

Please sign in to comment.