#!/bin/bash
# Copyright (c) 2026 Tigera, Inc. All rights reserved.
#
# 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.

# Deliver the locally-built component images onto the kubeadm nodes for the
# local-build win-fv flow. The Linux images (calico/calico, calico/node) come
# from the cross-block GCS cache as :latest-amd64; we re-tag them to
# DEV_IMAGE_TAG to match what the operator renders. calico/operator:<tag> is
# built by run-win-fv.sh before this runs. The Windows images are built here and
# ctr imported onto the Windows node.
#
# All refs land as docker.io/calico/<name>:${DEV_IMAGE_TAG} (test-build by
# default), which is what the operator requests; the Installation uses
# imagePullPolicy: IfNotPresent so kubelet uses the imported images without a
# registry pull.

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"

: "${ASO_DIR:=${SCRIPT_DIR}/../aso}"
: "${UTILS_DIR:=${SCRIPT_DIR}/../util}"
: "${CALICO_HOME:=${SCRIPT_DIR}/../../..}"

. "${UTILS_DIR}/utils.sh"
. "${ASO_DIR}/export-env.sh"
. ${ASO_DIR}/vmss.sh info

# Honor the tag the caller exported (run-win-fv.sh), defaulting to test-build so
# the re-tagged refs match what build-operator.sh rendered into the operator.
: "${DEV_IMAGE_TAG:=test-build}"
GIT_VERSION=$(git describe --tags --dirty --long --always --abbrev=12)

# ssh options shared with the connect commands generated by vmss.sh, so scp to
# the Linux node behaves the same as the ssh used elsewhere.
SSH_OPTS="-i ${SSH_KEY_FILE} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o ServerAliveInterval=5 -o ServerAliveCountMax=3"

function import_linux_images() {
  # Re-tag the GCS-cached images to the tag the operator renders. The operator
  # collapses cni/typha/kube-controllers/csi/apiserver/flexvol onto calico/calico,
  # so calico/calico + calico/node + calico/operator cover the whole Linux side.
  docker tag calico/calico:latest-amd64 "calico/calico:${DEV_IMAGE_TAG}"
  docker tag calico/node:latest-amd64 "calico/node:${DEV_IMAGE_TAG}"

  local tar="/tmp/winfv-linux-images.tar"
  docker save -o "${tar}" \
    "calico/calico:${DEV_IMAGE_TAG}" \
    "calico/node:${DEV_IMAGE_TAG}" \
    "calico/operator:${DEV_IMAGE_TAG}"

  scp ${SSH_OPTS} "${tar}" "aso@${LINUX_EIP}:/tmp/winfv-linux-images.tar"
  ${MASTER_CONNECT_COMMAND} "sudo ctr -n k8s.io images import /tmp/winfv-linux-images.tar"
  echo "Imported Linux images onto ${LINUX_EIP}"
}

function import_windows_images() {
  local windows_base_version
  case "${WINDOWS_SERVER_VERSION}" in
    windows-2019)
      windows_base_version="ltsc2019"
      ;;
    windows-2022)
      windows_base_version="ltsc2022"
      ;;
    windows-2025)
      windows_base_version="ltsc2025"
      ;;
    *)
      echo "Unsupported Windows Server version: ${WINDOWS_SERVER_VERSION}" >&2
      return 1
      ;;
  esac

  CALICO_NODE_IMAGE="node-windows-${GIT_VERSION}-${windows_base_version}.tar"
  CALICO_CNI_IMAGE="cni-windows-${GIT_VERSION}-${windows_base_version}.tar"

  # CI can supply the tarballs from an earlier build of the same commit.
  if [[ ! -f "${CALICO_HOME}/node/dist/windows/${CALICO_NODE_IMAGE}" ]]; then
    make -C "${CALICO_HOME}/node" image-windows WINDOWS_IMAGE=node-windows WINDOWS_VERSIONS="${windows_base_version}"
  fi
  if [[ ! -f "${CALICO_HOME}/cni-plugin/dist/windows/${CALICO_CNI_IMAGE}" ]]; then
    make -C "${CALICO_HOME}/cni-plugin" image-windows WINDOWS_IMAGE=cni-windows WINDOWS_VERSIONS="${windows_base_version}"
  fi

  ${ASO_DIR}/scp-to-windows.sh 0 "${CALICO_HOME}/node/dist/windows/${CALICO_NODE_IMAGE}" 'c:\calico-node-windows.tar'
  ${ASO_DIR}/scp-to-windows.sh 0 "${CALICO_HOME}/cni-plugin/dist/windows/${CALICO_CNI_IMAGE}" 'c:\calico-cni-plugin-windows.tar'

  # The tarballs carry the ref <name>:latest, so ctr imports them under
  # docker.io/library/. Re-tag to calico/<name>:${DEV_IMAGE_TAG} to match what
  # the operator renders.
  ${WINDOWS_CONNECT_COMMAND} 'c:\bin\ctr.exe --namespace k8s.io images import c:\calico-node-windows.tar --all-platforms'
  ${WINDOWS_CONNECT_COMMAND} 'c:\bin\ctr.exe --namespace k8s.io images import c:\calico-cni-plugin-windows.tar --all-platforms'
  ${WINDOWS_CONNECT_COMMAND} "c:\\bin\\ctr.exe --namespace k8s.io images tag docker.io/library/node-windows:latest docker.io/calico/node-windows:${DEV_IMAGE_TAG}"
  ${WINDOWS_CONNECT_COMMAND} "c:\\bin\\ctr.exe --namespace k8s.io images tag docker.io/library/cni-windows:latest docker.io/calico/cni-windows:${DEV_IMAGE_TAG}"
  echo "Imported Windows images onto ${WINDOWS_EIP}"
}

import_linux_images
import_windows_images
