Skip to content

Commit

Permalink
Get lates release with goproxy
Browse files Browse the repository at this point in the history
Signed-off-by: peppi-lotta <[email protected]>
  • Loading branch information
peppi-lotta committed Nov 25, 2024
1 parent 5e6f687 commit 6f2842f
Showing 1 changed file with 74 additions and 29 deletions.
103 changes: 74 additions & 29 deletions lib/releases.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
#!/bin/bash

# Requires parameters url and version. An optional parameter can be given to
# exclude some versions.
# Example usage:
# get_latest_release_from_goproxy "https://proxy.golang.org/sigs.k8s.io/cluster-api/@v/list" "v1.8." "beta|rc|pre|alpha"
function get_latest_release_from_goproxy() {

# fail when release_path is not passed
local proxuUrl="${1:?no release path is given}"

# fail when release is not passed
local release="${2:?no release given}"

set +x

response=$(curl -si "${proxuUrl}")

# Divide response to headers and body
response_headers=$(echo "${response}" | awk 'BEGIN {RS="\r\n\r\n"} NR==1 {print}')
if ! echo "${response_headers}" | grep -q 'Connection established'; then
response_body=$(echo "${response}" | awk 'BEGIN {RS="\r\n\r\n"} NR==2 {print}')
else
response_headers=$(echo "${response}" | awk 'BEGIN {RS="\r\n\r\n"} NR==2 {print}')
response_body=$(echo "${response}" | awk 'BEGIN {RS="\r\n\r\n"} NR==3 {print}')
fi

# This gets the latest release
sorted=$(echo "${response_body}" | sort -rV)
if [ -z "${3}" ]; then
release_tag=$(echo "${sorted}" | grep "${release}" -m 1)
else
# prune based on exluded values given in the command
pruned=$(echo "${sorted}" | grep -v -E "${3}" )
release_tag=$(echo "${pruned}" | grep "${release}" -m 1)
fi

set -x

# if release_tag is not found
if [ -z "${release_tag}" ]; then
echo "Error: release is not found from ${release_path}" >&2
exit 1
else
echo "${release_tag}"
fi
}

function get_latest_release() {

# fail when release_path is not passed
Expand Down Expand Up @@ -65,56 +111,55 @@ function get_latest_release() {
fi
}

# CAPM3, CAPI and BMO release path
CAPIRELEASEPATH="{https://api.github.com/repos/${CAPI_BASE_URL:-kubernetes-sigs/cluster-api}/releases}"

CAPIGOPROXY="https://proxy.golang.org/sigs.k8s.io/cluster-api/@v/list"
# CAPM3, CAPI and BMO releases
if [ "${CAPM3RELEASEBRANCH}" = "release-1.6" ]; then
# 1.6.99 points to the head of the release-1.6 branch. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.6.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.6.")}"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release_from_goproxy "${CAPIGOPROXY}" "v1.6.")}"
elif [ "${CAPM3RELEASEBRANCH}" = "release-1.7" ]; then
# 1.7.99 points to the head of the release-1.7 branch. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.7.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.7.")}"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release_from_goproxy "${CAPIGOPROXY}" "v1.7.")}"
elif [ "${CAPM3RELEASEBRANCH}" = "release-1.8" ]; then
# 1.8.99 points to the head of the release-1.8 branch. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.8.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.8.")}"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release_from_goproxy "${CAPIGOPROXY}" "v1.8.")}"
else
# 1.9.99 points to the head of the main branch of CAPM3. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.9.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.8.")}"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release_from_goproxy "${CAPIGOPROXY}" "v1.8.")}"
fi

CAPIBRANCH="${CAPIBRANCH:-${CAPIRELEASE}}"

# On first iteration, jq might not be installed
if [[ "$CAPIRELEASE" == "" ]]; then
command -v jq &>/dev/null && echo "Failed to fetch CAPI release from Github" && exit 1
command -v jq &>/dev/null && echo "Failed to fetch CAPI release from Github"
# CAPM3, CAPI and BMO release path
CAPIRELEASEPATH="{https://api.github.com/repos/${CAPI_BASE_URL:-kubernetes-sigs/cluster-api}/releases}"

# CAPM3, CAPI and BMO releases
if [ "${CAPM3RELEASEBRANCH}" = "release-1.6" ]; then
# 1.6.99 points to the head of the release-1.6 branch. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.6.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.6.")}"
elif [ "${CAPM3RELEASEBRANCH}" = "release-1.7" ]; then
# 1.7.99 points to the head of the release-1.7 branch. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.7.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.7.")}"
elif [ "${CAPM3RELEASEBRANCH}" = "release-1.8" ]; then
# 1.8.99 points to the head of the release-1.8 branch. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.8.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.8.")}"
else
# 1.9.99 points to the head of the main branch of CAPM3. Local override for CAPM3 is created for this version.
export CAPM3RELEASE="v1.9.99"
export CAPIRELEASE="${CAPIRELEASE:-$(get_latest_release "${CAPIRELEASEPATH}" "v1.8.")}"
fi

fi

if [[ "$CAPM3RELEASE" == "" ]]; then
command -v jq &>/dev/null && echo "Failed to fetch CAPM3 release from Github" && exit 1
fi

# Set CAPI_CONFIG_FOLDER variable according to CAPIRELEASE minor version
# Starting from CAPI v1.5.0 version cluster-api config folder location has changed
# to XDG_CONFIG_HOME folder.
# Following code defines the cluster-api config folder location according to CAPI
# release version

# TODO(Sunnatillo): Following condition should be removed when CAPM3 v1.4 reaches EOL
# NOTE(Sunnatillo): When CAPM3 v1.4 reaches EOL CAPI_CONFIG_FOLDER variable can be removed
# for the sake of reducing variables

version_string="${CAPIRELEASE#v}"
IFS='.' read -r _ minor _ <<< "$version_string"

if [ "$minor" -lt 5 ]; then
export CAPI_CONFIG_FOLDER="${HOME}/.cluster-api"
else
# Default CAPI_CONFIG_FOLDER to $HOME/.config folder if XDG_CONFIG_HOME not set
CONFIG_FOLDER="${XDG_CONFIG_HOME:-$HOME/.config}"
export CAPI_CONFIG_FOLDER="${CONFIG_FOLDER}/cluster-api"
fi

0 comments on commit 6f2842f

Please sign in to comment.