Skip to content

Instantly share code, notes, and snippets.

@voor
Created August 12, 2021 10:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save voor/bbdbad4186da8064b21dbc2128d64db5 to your computer and use it in GitHub Desktop.
Save voor/bbdbad4186da8064b21dbc2128d64db5 to your computer and use it in GitHub Desktop.
Create an imgpkg bundle from a helm chart with any additional overlays you might need.
#!/usr/bin/env sh
set -eux -o pipefail
CHART_NAME=gitlab-runner
SERVICE_FOLDER=${CHART_NAME}
CHART_REPO_NAME=gitlab
CHART_REPO_URL=https://charts.gitlab.io
HARBOR_REPO=your.repo.example.com
USAGE="Usage: $0 ACTION # should be test or deploy"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
DEPLOY=${1}
# Move into the folder at the top level,
# since this is usually run from the root of the service folder.
cd ${SERVICE_FOLDER}
# Assume the repo is not already added, add it and update it.
helm repo add ${CHART_REPO_NAME}/${CHART_NAME} ${CHART_REPO_URL}
helm repo update
# Make a temporary working directory for the imgpkg bundle, dump the chart into it.
WORKING_FOLDER=$(mktemp -d)
helm pull ${CHART_REPO_NAME}/${CHART_NAME} --untar -d ${WORKING_FOLDER}
# Charts are downloaded with the chart name, move it into a generic "CHART" folder.
mv ${WORKING_FOLDER}/${CHART_NAME} ${WORKING_FOLDER}/chart
# 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
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
# Create folders we'll need for imgpkg and additional manifests, follow
mkdir -p ${WORKING_FOLDER}/.imgpkg ${WORKING_FOLDER}/config sample
CHART_VERSION=$(yq eval .version ${WORKING_FOLDER}/chart/Chart.yaml)
APP_VERSION=$(yq eval .appVersion ${WORKING_FOLDER}/chart/Chart.yaml)
cp -r manifests ${WORKING_FOLDER}/config
# This is a bit of a hack, it's so we have access to the helm chart variables inside ytt.
echo -e "#@data/values\n---\n\n#@overlay/match-child-defaults missing_ok=True\n" > ${WORKING_FOLDER}/config/zz-helm-chart-values.yaml
yq eval '{"chartInfo": { "appVersion": .appVersion } } ' ${WORKING_FOLDER}/chart/Chart.yaml >> ${WORKING_FOLDER}/config/zz-helm-chart-values.yaml
cat ${WORKING_FOLDER}/config/zz-helm-chart-values.yaml
echo "#! I am a sample that was automatically generated with $0\n\n---" > sample/${SERVICE_FOLDER}.yaml
helm template sample-helm-template ${WORKING_FOLDER}/chart --namespace sample-namespace --include-crds \
| ytt --ignore-unknown-comments -f - -f ${WORKING_FOLDER}/config \
| kbld -f - --imgpkg-lock-output ${WORKING_FOLDER}/.imgpkg/images.yml >>sample/${SERVICE_FOLDER}.yaml
if [ "$DEPLOY" == 'deploy' ]; then
IMGPKG_BUNDLE=${HARBOR_REPO}/imgpkg/charts/${CHART_NAME}
imgpkg push -b ${IMGPKG_BUNDLE}:${CHART_VERSION} -f ${WORKING_FOLDER}
imgpkg copy -b ${IMGPKG_BUNDLE}:${CHART_VERSION} --to-repo ${IMGPKG_BUNDLE} --lock-output current-version.yml
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment