#!/usr/bin/env bash
set -euo pipefail

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=scripts/cluster/common.sh
source "$script_dir/common.sh"
load_cluster_env

registry_config="$cluster_root/deploy/kubernetes/registries.yaml"
[[ -f $registry_config && ! -L $registry_config ]] || {
  echo 'K3s registry mirror configuration is unavailable' >&2
  exit 1
}

restart_node() {
  local host=$1
  local node=$2
  local observer=$3
  local ready=false
  cluster_scp "$registry_config" "$host:/etc/rancher/k3s/registries.yaml"
  cluster_ssh "$host" \
    'chmod 0600 /etc/rancher/k3s/registries.yaml && systemctl restart k3s'
  for _ in $(seq 1 60); do
    if cluster_ssh "$observer" \
      "k3s kubectl wait --for=condition=Ready node/$node --timeout=5s >/dev/null 2>&1"; then
      ready=true
      break
    fi
    sleep 2
  done
  [[ $ready == true ]] || {
    echo "K3s node did not return Ready after registry mirror update: $node" >&2
    return 1
  }
  cluster_ssh "$host" \
    "grep -RFq 'https://docker.m.daocloud.io' /var/lib/rancher/k3s/agent/etc/containerd && k3s crictl pull docker.io/rancher/mirrored-pause:3.6 >/dev/null"
  echo "[registry] $node ready"
}

# Restart one etcd member at a time and observe it from a different server.
restart_node "$CLUSTER_LOS_ANGELES_HOST" easyai-los-angeles "$CLUSTER_NINGBO_HOST"
restart_node "$CLUSTER_HONGKONG_HOST" easyai-hongkong "$CLUSTER_NINGBO_HOST"
restart_node "$CLUSTER_NINGBO_HOST" easyai-ningbo "$CLUSTER_HONGKONG_HOST"

echo 'k3s_registry_mirrors=PASS nodes=3 quorum_preserved=true'
