#!/usr/bin/env bash

# Copyright 2022 The cert-manager Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


set -eu -o pipefail

# This script can be used to update kind images. However, you should check kind
# release notes as often specific images need to be used with a specific release
# of kind https://github.com/kubernetes-sigs/kind/releases

export KIND_IMAGE_REPO="docker.io/kindest/node"

CRANE=crane
TAGS=$(mktemp)

trap 'rm -f -- "$TAGS"' EXIT

if ! command -v $CRANE >/dev/null 2>&1; then
	echo -e "Couldn't find crane. Try running:\ngo install github.com/google/go-containerregistry/cmd/crane@latest" >&2
	exit 1
fi

function latest_kind_tag () {
	grep -E "^v$1" $TAGS | sort --version-sort | tail -1
}

$CRANE ls $KIND_IMAGE_REPO > $TAGS

# the TAGS file will now look like:
# ...
# v1.19.4
# v1.19.7
# v1.20.0
# v1.20.2
# v1.20.7
# ...

LATEST_122_TAG=$(latest_kind_tag "1\\.22")
LATEST_123_TAG=$(latest_kind_tag "1\\.23")
LATEST_124_TAG=$(latest_kind_tag "1\\.24")
LATEST_125_TAG=$(latest_kind_tag "1\\.25")
LATEST_126_TAG=$(latest_kind_tag "1\\.26")


LATEST_122_DIGEST=$(crane digest $KIND_IMAGE_REPO:$LATEST_122_TAG)
LATEST_123_DIGEST=$(crane digest $KIND_IMAGE_REPO:$LATEST_123_TAG)
LATEST_124_DIGEST=$(crane digest $KIND_IMAGE_REPO:$LATEST_124_TAG)
LATEST_125_DIGEST=$(crane digest $KIND_IMAGE_REPO:$LATEST_125_TAG)
LATEST_126_DIGEST=$(crane digest $KIND_IMAGE_REPO:$LATEST_126_TAG)

# k8s 1.27 is manually added to ensure that we use the exact documented tag as per kind recommendation
LATEST_127_TAG=v1.27.3
LATEST_127_DIGEST=sha256:3966ac761ae0136263ffdb6cfd4db23ef8a83cba8a463690e98317add2c9ba72

cat << EOF > ./make/kind_images.sh
# Copyright 2022 The cert-manager Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# generated by $0

KIND_IMAGE_K8S_122=$KIND_IMAGE_REPO@$LATEST_122_DIGEST
KIND_IMAGE_K8S_123=$KIND_IMAGE_REPO@$LATEST_123_DIGEST
KIND_IMAGE_K8S_124=$KIND_IMAGE_REPO@$LATEST_124_DIGEST
KIND_IMAGE_K8S_125=$KIND_IMAGE_REPO@$LATEST_125_DIGEST
KIND_IMAGE_K8S_126=$KIND_IMAGE_REPO@$LATEST_126_DIGEST

# Manually set- see hack/latest-kind-images.sh for details
KIND_IMAGE_K8S_127=$KIND_IMAGE_REPO@$LATEST_127_DIGEST

# $KIND_IMAGE_REPO:$LATEST_122_TAG
KIND_IMAGE_SHA_K8S_122=$LATEST_122_DIGEST

# $KIND_IMAGE_REPO:$LATEST_123_TAG
KIND_IMAGE_SHA_K8S_123=$LATEST_123_DIGEST

# $KIND_IMAGE_REPO:$LATEST_124_TAG
KIND_IMAGE_SHA_K8S_124=$LATEST_124_DIGEST

# $KIND_IMAGE_REPO:$LATEST_125_TAG
KIND_IMAGE_SHA_K8S_125=$LATEST_125_DIGEST

# $KIND_IMAGE_REPO:$LATEST_126_TAG
KIND_IMAGE_SHA_K8S_126=$LATEST_126_DIGEST

# Manually set - see hack/latest-kind-images.sh for details
# $KIND_IMAGE_REPO:$LATEST_127_TAG
KIND_IMAGE_SHA_K8S_127=$LATEST_127_DIGEST

# note that these 'full' digests should be avoided since not all tools support them
# prefer KIND_IMAGE_K8S_*** instead
KIND_IMAGE_FULL_K8S_122=$KIND_IMAGE_REPO:$LATEST_122_TAG@$LATEST_122_DIGEST
KIND_IMAGE_FULL_K8S_123=$KIND_IMAGE_REPO:$LATEST_123_TAG@$LATEST_123_DIGEST
KIND_IMAGE_FULL_K8S_124=$KIND_IMAGE_REPO:$LATEST_124_TAG@$LATEST_124_DIGEST
KIND_IMAGE_FULL_K8S_125=$KIND_IMAGE_REPO:$LATEST_125_TAG@$LATEST_125_DIGEST
KIND_IMAGE_FULL_K8S_126=$KIND_IMAGE_REPO:$LATEST_126_TAG@$LATEST_126_DIGEST

# Manually set - see hack/latest-kind-images.sh for details
KIND_IMAGE_FULL_K8S_127=$KIND_IMAGE_REPO:$LATEST_127_TAG@$LATEST_127_DIGEST

EOF

cat << EOF
# Images have been updated.
# Now check kind release notes and verify that if specific images are recommended to be used with the kind release that we are using, the script hasn't pulled in other images.
# https://github.com/kubernetes-sigs/kind/releases
EOF
