Last active
June 16, 2022 15:05
-
-
Save voor/49af5d9e4dcefa52093c7ea2a61c823b to your computer and use it in GitHub Desktop.
Build script for handling imgpkg with helm, ytt, and even Package Repositories.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
USAGE="Usage: $0 PACKAGE ACTION REPO # ACTION should be test or deploy" | |
if [ "$#" == "0" ]; then | |
echo "$USAGE" | |
exit 1 | |
fi | |
PACKAGE=${1} | |
SERVICE_FOLDER=${PACKAGE} | |
ACTION=${2} | |
if [ -z "$PACKAGE" ] | |
then | |
echo "build package failed. must set PACKAGE" | |
echo "$USAGE" | |
exit 2 | |
fi | |
if [ -z "$ACTION" ] | |
then | |
echo "build package failed. must set ACTION" | |
echo "$USAGE" | |
exit 2 | |
elif [ "$ACTION" == 'deploy' ]; then | |
HARBOR_REPO=${3} | |
if [ -z "$HARBOR_REPO" ] | |
then | |
echo "build package failed. must set REPO for deploy" | |
echo "$USAGE" | |
exit 2 | |
fi | |
fi | |
vendir sync | |
WORKING_FOLDER=${PWD}/bundle | |
BUILD_SCRIPT=$(basename $0) | |
# Create folders we'll need for imgpkg and additional manifests, follow | |
mkdir -p ${WORKING_FOLDER}/.imgpkg sample versions packages | |
# Check if using helm chart; if so, set default values for the helm chart | |
if [ -d "${PWD}/bundle/chart" ]; then | |
# Apply our values overlay to the defaults in the chart. | |
mv ${WORKING_FOLDER}/chart/values.yaml ${WORKING_FOLDER}/chart/values.yaml.original | |
echo -e "#@data/values\n---\n\n" > ${WORKING_FOLDER}/chart/values.yaml | |
# Add chart and app version to the end of the values file. | |
yq eval '{"chartInfo": { "appVersion": .appVersion, "chartVersion": .version } }' ${WORKING_FOLDER}/chart/Chart.yaml >> ${WORKING_FOLDER}/chart/values.yaml | |
CHART_VERSION=$(yq eval .version ${WORKING_FOLDER}/chart/Chart.yaml) | |
APP_VERSION=$(yq eval .appVersion ${WORKING_FOLDER}/chart/Chart.yaml) | |
FAKE_VALUES="" | |
if [ -f "${PWD}/sample/fake-values.yaml" ]; then | |
FAKE_VALUES=" -f sample/fake-values.yaml " | |
fi | |
ytt -f ${WORKING_FOLDER}/chart/values.yaml.original \ | |
--file-mark "values.yaml.original:type=yaml-plain" \ | |
-f helm/values-overlay.yaml \ | |
>> ${WORKING_FOLDER}/chart/values.yaml | |
# Cannot actually pass data-value here as it's not a true #@data/values input->output | |
#--data-value chartInfo.appVersion=$APP_VERSION \ | |
#--data-value chartInfo.chartVersion=$CHART_VERSION \ | |
#--data-value fullnameOverride=${PACKAGE}-${CHART_VERSION}" | |
# Stupid CHART_VERSION not in yq subshell, so weird quotes | |
# TODO: Verify we want to make all charts create k8s objects with PACKAGE_NAME-CHART_VERSION naming | |
# yq --inplace '.fullnameOverride = strenv(PACKAGE)+"-"+"'$CHART_VERSION'"' ${WORKING_FOLDER}/chart/values.yaml | |
echo -e "#! I am a sample that was automatically generated with ${BUILD_SCRIPT}\n" > sample/${SERVICE_FOLDER}-${APP_VERSION}.yaml | |
helm template ${PACKAGE} ${WORKING_FOLDER}/chart --namespace ${PACKAGE} --include-crds $FAKE_VALUES \ | |
| ytt --ignore-unknown-comments -f ${WORKING_FOLDER}/chart/values.yaml $FAKE_VALUES -f - -f ${WORKING_FOLDER}/config \ | |
| kbld -f - --imgpkg-lock-output ${WORKING_FOLDER}/.imgpkg/images.yml >> sample/${SERVICE_FOLDER}-${APP_VERSION}.yaml | |
# Not a helm chart, so using just raw k8s yaml from a vendir sync pulling from github (probably) | |
elif [ -d "${PWD}/bundle/packages.temp" ] || [ -d "${PWD}/bundle/packages" ]; then | |
# FIXME This is pretty hacky at the moment because you can't do overlays during reconciling Package Repository. | |
# See https://github.com/vmware-tanzu/carvel-kapp-controller/issues/397 for more information. | |
# Look at this again if there is a way to pipeline in ytt actions to do stuff with vendir and imgpkg | |
APP_VERSION="$(yq eval '.. | select(has("tag")).tag, .. | select(has("tags")).tags[0]' vendir.yml vendir.lock.yml | head -n1)" | |
mkdir -p ${WORKING_FOLDER}/packages | |
echo -e "#! I am after overlay from ${BUILD_SCRIPT}\n" > ${WORKING_FOLDER}/packages/packages.yaml | |
ytt --ignore-unknown-comments -f ${WORKING_FOLDER}/packages.temp \ | |
-f ${WORKING_FOLDER}/../manifests/ >> ${WORKING_FOLDER}/packages/packages.yaml | |
rm -rf ${WORKING_FOLDER}/packages.temp | |
echo -e "#! I am a sample that was automatically generated with ${BUILD_SCRIPT}\n" > sample/${SERVICE_FOLDER}-${APP_VERSION}.yaml | |
ytt --ignore-unknown-comments -f ${WORKING_FOLDER}/packages -f ${WORKING_FOLDER}/.imgpkg \ | |
| kbld -f - >>sample/${SERVICE_FOLDER}-${APP_VERSION}.yaml | |
else | |
if [ -f "${PWD}/version-override.yml" ]; then | |
APP_VERSION=$(yq eval '.. | select(has("tag")).tag' version-override.yml) | |
else | |
APP_VERSION=$(yq eval '.. | select(has("tag")).tag, .. | select(has("tags")).tags[0]' vendir.yml vendir.lock.yml | head -n1) | |
fi | |
FAKE_VALUES="" | |
if [ -f "${PWD}/sample/fake-values.yaml" ]; then | |
FAKE_VALUES=" -f sample/fake-values.yaml " | |
fi | |
echo -e "#! I am a sample that was automatically generated with ${BUILD_SCRIPT}\n" > sample/${SERVICE_FOLDER}-${APP_VERSION}.yaml | |
ytt --ignore-unknown-comments -f ${WORKING_FOLDER}/config $FAKE_VALUES \ | |
| kbld -f - --imgpkg-lock-output ${WORKING_FOLDER}/.imgpkg/images.yml >>sample/${SERVICE_FOLDER}-${APP_VERSION}.yaml | |
fi | |
if [ "$ACTION" == 'deploy' ]; then | |
IMGPKG_PUSH=${HARBOR_REPO}/imgpkg/push/${SERVICE_FOLDER} | |
imgpkg push -b ${IMGPKG_PUSH}:${APP_VERSION} -f ${WORKING_FOLDER} | |
IMGPKG_BUNDLE=${HARBOR_REPO}/imgpkg/charts/${SERVICE_FOLDER} | |
imgpkg copy -b ${IMGPKG_PUSH}:${APP_VERSION} --to-repo ${IMGPKG_BUNDLE} --lock-output current-version.yml | |
cp current-version.yml versions/${SERVICE_FOLDER}-${APP_VERSION}.yml | |
if [ -f package-template.yaml ]; then | |
ytt -f package-template.yaml --data-values-file current-version.yml > packages/${SERVICE_FOLDER}-${APP_VERSION}.pkg.yml | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment