Skip to content

Commit

Permalink
[CI] Improve load-image.sh for minkube
Browse files Browse the repository at this point in the history
  • Loading branch information
Corneil du Plessis committed Jul 8, 2024
1 parent 80faaa6 commit 7d9aadd
Showing 1 changed file with 43 additions and 78 deletions.
121 changes: 43 additions & 78 deletions src/deploy/k8s/load-image.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/bash

function print_args() {
echo "Arguments: <docker-image:tag> [<dont-pull>]"
echo "Not there is not space between name and tag. It is like on any docker registry"
echo "dont-pull: true - will not pull never"
echo "dont-pull: false | will pull always"
echo "dont-pull: <pot-provided>: will pull if not present"
echo "pull: true - will always pull"
echo "pull: false - will never pull"
echo "pull: <not-provided>: will pull if not present"
}
if [ "$K8S_DRIVER" = "" ]; then
K8S_DRIVER=kind
Expand All @@ -17,7 +18,9 @@ if [ "$2" != "" ] && [ "$2" != "true" ] && [ "$2" != "false" ]; then
print_args
exit 1
fi

DONT_PULL=$2
IMAGE="$1"
echo "image: $IMAGE, dont_pull=$DONT_PULL"
if [ "$K8S_DRIVER" != "tmc" ] && [ "$K8S_DRIVER" != "gke" ] ; then
DONT_PULL=$2
IMAGE="$1"
Expand All @@ -37,78 +40,40 @@ if [ "$K8S_DRIVER" != "tmc" ] && [ "$K8S_DRIVER" != "gke" ] ; then
echo "Exists:$IMAGE"
fi
fi
COUNT=$(docker images --filter "reference=$IMAGE" --format "{{ .Repository }}:{{ .Tag }}" 2> /dev/null | grep -c -F "$IMAGE")
if ((COUNT == 0)); then
echo "WARN:Image Not found:$IMAGE"
exit 0
fi
err=$(docker history "$IMAGE" 2> /dev/null)
rc=$?
if [[ $rc -ne 0 ]]; then
echo "$err" >&2
exit 1
fi
echo "Loading:$IMAGE"
case "$K8S_DRIVER" in
"kind")
echo "Loading $IMAGE to kind"
kind load docker-image "$IMAGE" "$IMAGE"
;;
"tce")
echo "Harbor push will be supported soon"
;;
"gke")
echo "gcr push will be supported soon"
;;
"tmc")
echo "not supported in TMC"
;;
"microk8s")
echo "Loading $IMAGE to microk8s"
docker tag $IMAGE localhost:32000/$IMAGE
docker push localhost:32000/$IMAGE
;;
*)
echo "Loading $IMAGE to minikube"
DOCKER_IDS=$(docker images --filter "reference=$IMAGE" --format "{{ .ID }}")
NAME="${IMAGE%%:*}"
colon=":"
TAG=${IMAGE#*$colon}
MK_IDS=$(minikube image ls --format table | grep -F "$NAME" | grep -F "$TAG" | awk '{print $6}')
for did in $DOCKER_IDS; do
for mid in $MK_IDS; do
# Docker id may be shorter than Minikube id.
if [ "${mid:0:12}" = "${did:0:12}" ]; then
echo "$IMAGE:$did already uploaded"
exit 0
fi
done
done
PULL=false
if [ "$DONT_PULL" == "false" ]; then
PULL=true
echo "Loading:$IMAGE"
else
echo "Loading:$IMAGE:$DOCKER_IDS"
fi
OPTIONS="--overwrite true --daemon true --pull $PULL"
if [ "$PULL" == "false" ]; then
OPTIONS="$OPTIONS --remote false"
fi
minikube image load "$IMAGE" $OPTIONS
MK_IDS=$(minikube image ls --format table | grep -F "$NAME" | grep -F "$TAG" | awk '{print $6}')
for did in $DOCKER_IDS; do
for mid in $MK_IDS; do
# Docker id may be shorter than Minikube id.
if [ "${mid:0:12}" = "${did:0:12}" ]; then
echo "$IMAGE:$did uploaded"
exit 0
fi
done
done
echo "Unable to load $IMAGE:$DOCKER_IDS. It may be in active use."
exit 0
;;
esac
echo "Loaded:$IMAGE"
fi
DOCKER_SHA=$(docker image inspect "$IMAGE" --format json | jq -r '.[0].Id')
case "$K8S_DRIVER" in
"kind")
echo "Loading $IMAGE to kind"
kind load docker-image "$IMAGE" "$IMAGE"
;;
"tce")
echo "Harbor push will be supported soon"
;;
"gke")
echo "gcr push will be supported soon"
;;
"tmc")
echo "not supported in TMC"
;;
"microk8s")
echo "Loading $IMAGE to microk8s"
docker tag $IMAGE localhost:32000/$IMAGE
docker push localhost:32000/$IMAGE
;;
*)
MINIKUBE_JSON=$(minikube image ls --format json | jq -c --arg image $IMAGE '.[] | {id: .id, image: .repoTags[0]} | select(.image | contains($image))')
if [ "$MINIKUBE_JSON" != "" ]; then
MINIKUBE_SHA="sha256:$(echo $MINIKUBE_JSON | jq -r '.id')"
if [ "$MINIKUBE_SHA" = "$DOCKER_SHA" ]; then
echo "$IMAGE already uploaded"
exit 0
else
echo "Image: $IMAGE, docker $DOCKER_SHA, minikube $MINIKUBE_SHA"
fi
fi
echo "Loading $IMAGE to minikube"
minikube image load "$IMAGE"
;;
esac
echo "Loaded:$IMAGE"

0 comments on commit 7d9aadd

Please sign in to comment.