From 7d9aadd3a6482651f20c009282c6e56ee65017ff Mon Sep 17 00:00:00 2001 From: Corneil du Plessis Date: Mon, 8 Jul 2024 17:24:23 +0200 Subject: [PATCH] [CI] Improve load-image.sh for minkube --- src/deploy/k8s/load-image.sh | 121 +++++++++++++---------------------- 1 file changed, 43 insertions(+), 78 deletions(-) diff --git a/src/deploy/k8s/load-image.sh b/src/deploy/k8s/load-image.sh index 16bc76cf80..4aad1e268d 100755 --- a/src/deploy/k8s/load-image.sh +++ b/src/deploy/k8s/load-image.sh @@ -1,10 +1,11 @@ #!/bin/bash + function print_args() { echo "Arguments: []" 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: : will pull if not present" + echo "pull: true - will always pull" + echo "pull: false - will never pull" + echo "pull: : will pull if not present" } if [ "$K8S_DRIVER" = "" ]; then K8S_DRIVER=kind @@ -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" @@ -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" \ No newline at end of file