|
| 1 | +#!/usr/bin/env bash |
| 2 | +[ -z "$DEBUG" ] || set -x |
| 3 | +set -e |
| 4 | + |
| 5 | +# Bash isn't a good tool for producing a tar from a build context |
| 6 | +# but this script is an experiment on how to compose an image |
| 7 | +# from a runtime and some static files using go-containerregistry's crane |
| 8 | +# The user shouldn't need to write Dockerfile or .dockerignore, |
| 9 | +# but we could probably use a generated Dockerfile + kaniko instead, or Buildpacks |
| 10 | + |
| 11 | +# Settings |
| 12 | +DEFAULT_REGISTRY=builds-registry.ystack.svc.cluster.local |
| 13 | +[ -z "$BUILDS_REGISTRY" ] && BUILDS_REGISTRY=$DEFAULT_REGISTRY |
| 14 | +[ -z "$PUSH_REGISTRY" ] && PUSH_REGISTRY=$DEFAULT_REGISTRY |
| 15 | + |
| 16 | +if [ "$(curl -s --connect-timeout 3 http://$BUILDS_REGISTRY/v2/)" != "{}" ] |
| 17 | +then |
| 18 | + echo "ERROR Skaffold need local access to the builds registry for digest lookup" |
| 19 | + echo "Registry: $BUILDS_REGISTRY" |
| 20 | + echo "Look for y-stack's ingress or port-forward utilities" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +[ -z "$IMAGE" ] && echo "No IMAGE env (from for example Skaffold)" && exit 1 |
| 25 | + |
| 26 | +IMAGE=$IMAGE |
| 27 | +case "$IMAGE" in |
| 28 | + $BUILDS_REGISTRY/* ) ;; |
| 29 | + $PUSH_REGISTRY/* ) echo "Unlike y-build this script won't push to non-build registries" && exit 1 ;; |
| 30 | + * ) echo "Output is restricted to PUSH_REGISTRY=$PUSH_REGISTRY. Got: $IMAGE" && exit 1 ;; |
| 31 | +esac |
| 32 | + |
| 33 | +RUNTIME_IMAGE=$1 |
| 34 | +[ -z "$RUNTIME_IMAGE" ] && echo "First argument must be a runtime image to append the layer to" \ |
| 35 | + && echo "To improve build times use a runtime image in the target repo" && exit 1 |
| 36 | + |
| 37 | +# crane hangs for a long time if it doesn't know that the registry is plain http |
| 38 | +RUNTIME_IMAGE=$(echo $RUNTIME_IMAGE | sed 's|.local/|.local:80/|') |
| 39 | +IMAGE=$(echo $IMAGE | sed 's|.local/|.local:80/|') |
| 40 | + |
| 41 | +# This is a PoC, let's make a lot of assumptions to simplify |
| 42 | +context=. |
| 43 | +src='**' |
| 44 | +# assuming a single manual sync, which is the reasonable use case for a runtime |
| 45 | +[ ! -f skaffold.yaml ] && echo "This composition example assumes a sync defined in a skaffold.yaml" && exit 1 |
| 46 | +dest=$(cat skaffold.yaml | grep 'dest:' | awk '{ print $2 }') |
| 47 | +# this avoids "tar: Removing leading `/' from member names" and could come in handy if we can't use --transform |
| 48 | +dest=$(echo $dest | sed 's|^/||') |
| 49 | + |
| 50 | +list=$(mktemp) |
| 51 | +(cd $context; git ls-files -c -o --exclude-standard -- . || find . -type f) > $list |
| 52 | +tar=$(mktemp) |
| 53 | +tar --transform "s|^|$dest/|" --show-transformed-names -cvhf $tar -T $list --mode='ug+rw' --group=65534 --owner=65532 |
| 54 | +rm $list |
| 55 | + |
| 56 | +set -x |
| 57 | +y-crane append --insecure -b $RUNTIME_IMAGE -f $tar -t $IMAGE |
| 58 | +set +x |
| 59 | +rm $tar |
0 commit comments