Skip to content

Instantly share code, notes, and snippets.

@Dentrax
Last active December 13, 2021 14:56
Show Gist options
  • Save Dentrax/e5353065429bef48b596998461ffcc34 to your computer and use it in GitHub Desktop.
Save Dentrax/e5353065429bef48b596998461ffcc34 to your computer and use it in GitHub Desktop.
Get all images in helm chart and copy to internal registry
#!/bin/bash
set -e
function usage(){
echo "$(basename $0) --registry registry.gitlab.com/images --platform linux/amd64 --chart fluent/fluent-bit --version 0.19.10" >&2
}
function teardown {
rm -rf "./tmp"
}
function copy() {
local registry=$1
local platform=$2
local chart=$3
local version=$4
command -v helm >/dev/null 2>&1 || { echo >&2 "I require 'helm' but it's not installed! Aborting."; exit 1; }
command -v crane >/dev/null 2>&1 || { echo >&2 "I require 'crane' but it's not installed! Aborting."; exit 1; }
helm pull "$3" --version "$4" --untar --untardir tmp
pushd tmp
pushd */
# https://github.com/helm-lab/helm-images/blob/master/images.sh
images=$(helm install --generate-name --dry-run "." | grep "image:" | awk -F" " '{print $2}' | sed 's/^"//g' | sed 's/\"//g' | sort | awk '!seen[$0]++')
# traverse all the images
# image format: foo/bar:baz
for src in $images; do
dst="$registry/$src"
echo "[+] Copying image from SRC: $src to DST: $dst"
# what if image tag already in the registry?
# TODO: prompt [y/N] to override if digest matched
crane copy --platform $platform $src $dst
done
popd
popd
}
# https://stackoverflow.com/a/14203146/5685796
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-r|--registry)
REGISTRY="$2"
shift
shift
;;
-p|--platform)
PLATFORM="$2"
shift
shift
;;
-c|--chart)
CHART="$2"
shift
shift
;;
-v|--version)
VERSION="$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
trap teardown EXIT
copy $REGISTRY $PLATFORM $CHART $VERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment