Skip to content

Commit

Permalink
refactor to use remove charts repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed Jan 6, 2025
1 parent db63c8f commit 41c09f4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
11 changes: 8 additions & 3 deletions scripts/build-chart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -e

source $(dirname $0)/version
source $(dirname $0)/util-chart
source $(dirname $0)/util-team-charts

# We don't do this for helm-locker
if [[ "${BUILD_TARGET}" == "helm-locker" ]]; then
Expand All @@ -21,10 +22,14 @@ fi
CHART=${CHART:-${DEFAULT_CHART_TARGET}}

if [ "$BUILD_TARGET" == "prometheus-federator" ]; then
CHART_VERSION=${EMBEDDED_CHART_VERSION:-$(find "./charts/${CHART}" -maxdepth 1 -mindepth 1 -type d | tr - \~ | sort -rV | tr \~ - | head -n1 | cut -d'/' -f4)}
EMBEDDED_CHART_VERSION="0.3.4" # workaround until ready to remove chart lock
CHART_VERSION=${EMBEDDED_CHART_VERSION:-$(newest-chart-version "rancher-project-monitoring")}
# Fetch asset from github OB charts repo
fetch-team-chart "rancher-project-monitoring" "$CHART_VERSION"
CHART_DESTINATION="./build/charts/${CHART}-${CHART_VERSION}.tgz"

# Prepare chart for embedding location
base64 -i "assets/${CHART}/${CHART}-${CHART_VERSION}.tgz" > "cmd/${BUILD_TARGET}/fs/${CHART}.tgz.base64"
base64 -i "${CHART_DESTINATION}" > "cmd/${BUILD_TARGET}/fs/${CHART}.tgz.base64"
elif [ "$BUILD_TARGET" == "helm-project-operator" ]; then
# Prepare base chart for build
clean-old-chart "$CHART"
Expand All @@ -38,4 +43,4 @@ elif [ "$BUILD_TARGET" == "helm-project-operator" ]; then
base64 -i "./build/charts/${CHART}-${HELM_CHART_VERSION}.tgz" > "cmd/${BUILD_TARGET}/fs/${CHART}.tgz.base64"
fi

echo "Completed ${CHART} (ver: ${HELM_CHART_VERSION:-"n/a"}) build process."
echo "Completed ${CHART} (ver: ${HELM_CHART_VERSION:-"n/a"}) chart build process."
10 changes: 7 additions & 3 deletions scripts/package-helm
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ fi

source "$(dirname "$0")/version"
source "$(dirname "$0")/util-chart"
source "$(dirname "$0")/util-team-charts"

# We don't do the full process for prometheus-federator
if [[ "${BUILD_TARGET}" == "prometheus-federator" ]]; then
mkdir -p ./dist/artifacts/
CHART_VERSION=${EMBEDDED_CHART_VERSION:-$(find "./charts/${BUILD_TARGET}" -maxdepth 1 -mindepth 1 -type d | tr - \~ | sort -rV | tr \~ - | head -n1 | cut -d'/' -f4)}
cp -a "./assets/${BUILD_TARGET}/${BUILD_TARGET}-${CHART_VERSION}.tgz" ./build/charts/
EMBEDDED_CHART_VERSION="0.3.4" # workaround until ready to remove chart lock
CHART_VERSION=${EMBEDDED_CHART_VERSION:-$(newest-chart-version "rancher-project-monitoring")}
# Fetch asset from github OB charts repo
fetch-team-chart "rancher-project-monitoring" "$CHART_VERSION"
PROM_FED_CHART=$(newest-chart-version "prometheus-federator")
fetch-team-chart "prometheus-federator" "$PROM_FED_CHART"
exit
fi

Expand Down
87 changes: 87 additions & 0 deletions scripts/util-team-charts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env bash

function fetch-team-charts-index() {
TEAM_INDEX="https://raw.githubusercontent.com/mallardduck/ob-team-charts/refs/heads/main/index.yaml"
BUILD_DIR="./build"
LOCAL_FILE="$BUILD_DIR/charts-index.yaml"
LOCAL_ETAG_FILE="$BUILD_DIR/charts-index.etag"

# Ensure the build directory exists
mkdir -p "$BUILD_DIR"

# Fetch the ETag from the remote file headers
REMOTE_ETAG=$(curl -sI "$TEAM_INDEX" | grep -i "etag" | awk '{print $2}' | tr -d '\r')

if [[ -z "$REMOTE_ETAG" ]]; then
return 1
fi

# Check if a local ETag exists
if [[ -f "$LOCAL_ETAG_FILE" ]]; then
LOCAL_ETAG=$(cat "$LOCAL_ETAG_FILE")
# If the ETag matches, skip the download
if [[ "$REMOTE_ETAG" == "$LOCAL_ETAG" ]]; then
return 0
fi
fi

# Download the file as it is either outdated or does not exist
echo "Downloading file..."
if curl -s "$TEAM_INDEX" -o "$LOCAL_FILE"; then
# Save the new ETag
echo "$REMOTE_ETAG" > "$LOCAL_ETAG_FILE"
return 0
else
return 1
fi
}

function newest-chart-version() {
LOCAL_INDEX_FILE="./build/charts-index.yaml"
# Fetch the latest index
fetch-team-charts-index >/dev/null

CHART_TARGET=$1
YQ_QUERY=".entries[\"${CHART_TARGET}\"][0].version"
NEWEST_CHART=$(yq "$YQ_QUERY" "$LOCAL_INDEX_FILE")
echo "$NEWEST_CHART"
}

function fetch-team-chart() {
CHART_TARGET="${1}"
BASE_FETCH_URL="https://raw.githubusercontent.com/mallardduck/ob-team-charts/refs/heads/main/assets"
CHART_VERSION="${2}"
FETCH_URL="${BASE_FETCH_URL}/${CHART_TARGET}/${CHART_TARGET}-${CHART_VERSION}.tgz"
BUILD_DIR="./build"
LOCAL_FILE="$BUILD_DIR/charts/${CHART_TARGET}-${CHART_VERSION}.tgz"
LOCAL_ETAG_FILE="$LOCAL_FILE.etag"

# Ensure the build directory exists
mkdir -p "$BUILD_DIR"

# Fetch the ETag from the remote file headers
REMOTE_ETAG=$(curl -sI "$FETCH_URL" | grep -i "etag" | awk '{print $2}' | tr -d '\r')

if [[ -z "$REMOTE_ETAG" ]]; then
return 1
fi

# Check if a local ETag exists
if [[ -f "$LOCAL_ETAG_FILE" ]]; then
LOCAL_ETAG=$(cat "$LOCAL_ETAG_FILE")
# If the ETag matches, skip the download
if [[ "$REMOTE_ETAG" == "$LOCAL_ETAG" ]]; then
return 0
fi
fi

# Download the file as it is either outdated or does not exist
echo "Downloading file..."
if curl -s "$FETCH_URL" -o "$LOCAL_FILE"; then
# Save the new ETag
echo "$REMOTE_ETAG" > "$LOCAL_ETAG_FILE"
return 0
else
return 1
fi
}

0 comments on commit 41c09f4

Please sign in to comment.