penguin/monok8s

k8s image for Mono Gateway Dev Kit

devtools/build-all.sh

raw · 5013 bytes

#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUT_DIR="$(realpath "$SCRIPT_DIR/../out/")"

cd "$SCRIPT_DIR/../"

# ===== CONFIG TABLE =====
# Add supported concrete builds here.
# format: kube=crio
CONFIGS=(
  "v1.33.3=cri-o.arm64.v1.33.3"
  "v1.34.1=cri-o.arm64.v1.34.1"
  "v1.35.3=cri-o.arm64.v1.35.2"
)

# ===== HELPERS =====

strip_v() {
  echo "${1#v}"
}

minor_of_version() {
  # input: 1.35.3 or v1.35.3
  local v="${1#v}"
  IFS='.' read -r major minor patch <<< "$v"
  echo "$major.$minor"
}

version_sort() {
  sort -t. -k1,1n -k2,2n -k3,3n
}

list_configs() {
  echo "Available build targets:"
  for c in "${CONFIGS[@]}"; do
    local kube="${c%%=*}"
    local crio="${c##*=}"
    echo "  ${kube#v}  (CRI-O: $crio)"
  done
}

list_minors() {
  echo "Latest supported target per minor:"
  latest_per_minor | while read -r kube crio; do
    echo "  ${kube#v}  (CRI-O: $crio)"
  done
}

latest_per_minor() {
  # Output lines: <kube> <crio>
  local minors
  minors="$(
    for c in "${CONFIGS[@]}"; do
      kube="${c%%=*}"
      minor_of_version "$kube"
    done | sort -uV
  )"

  while read -r minor; do
    [ -n "$minor" ] || continue
    resolve_minor "$minor"
  done <<< "$minors"
}

resolve_exact() {
  local target="$1"
  local target_v="v${target#v}"

  for c in "${CONFIGS[@]}"; do
    local kube="${c%%=*}"
    local crio="${c##*=}"
    if [[ "$kube" == "$target_v" ]]; then
      echo "$kube $crio"
      return 0
    fi
  done

  return 1
}

resolve_minor() {
  # input: 1.35 or v1.35
  local want_minor
  want_minor="$(minor_of_version "${1#v}.0")"

  local matches=()
  local c kube crio
  for c in "${CONFIGS[@]}"; do
    kube="${c%%=*}"
    crio="${c##*=}"
    if [[ "$(minor_of_version "$kube")" == "$want_minor" ]]; then
      matches+=("${kube}=${crio}")
    fi
  done

  if [[ ${#matches[@]} -eq 0 ]]; then
    return 1
  fi

  local latest_kube
  latest_kube="$(
    for c in "${matches[@]}"; do
      echo "${c%%=*#}" | sed 's/^v//'
    done | version_sort | tail -n1
  )"

  resolve_exact "$latest_kube"
}

build_exact() {
  local target="$1"
  local resolved
  if ! resolved="$(resolve_exact "$target")"; then
    echo "❌ Unknown exact target: $target" >&2
    echo >&2
    list_configs >&2
    exit 1
  fi

  local kube crio
  read -r kube crio <<< "$resolved"

  echo ">>> Building Kubernetes $kube with $crio"
  make release CRIO_VERSION="$crio" KUBE_VERSION="$kube" 
}

build_minor() {
  local minor="$1"
  local resolved
  if ! resolved="$(resolve_minor "$minor")"; then
    echo "❌ No supported target found for minor: $minor" >&2
    echo >&2
    list_minors >&2
    exit 1
  fi

  local kube crio
  read -r kube crio <<< "$resolved"

  echo ">>> Minor $minor resolved to latest supported target ${kube#v}"
  echo ">>> Building Kubernetes $kube with $crio"
  make release CRIO_VERSION="$crio" KUBE_VERSION="$kube" 
}

build_range() {
  # input like 1.33-1.35
  local range="$1"
  local start="${range%-*}"
  local end="${range#*-}"

  local start_minor="${start#v}"
  local end_minor="${end#v}"

  local any=0
  while read -r minor; do
    [ -n "$minor" ] || continue

    # simple lexical-safe because format is N.N and sort -V was used
    if [[ "$(printf '%s\n%s\n' "$start_minor" "$minor" | sort -V | head -n1)" != "$start_minor" ]]; then
      continue
    fi
    if [[ "$(printf '%s\n%s\n' "$minor" "$end_minor" | sort -V | head -n1)" != "$minor" ]]; then
      continue
    fi

    any=1
    build_minor "$minor"
  done < <(
    for c in "${CONFIGS[@]}"; do
      kube="${c%%=*}"
      minor_of_version "$kube"
    done | sort -uV
  )

  if [[ $any -eq 0 ]]; then
    echo "❌ No supported minors found in range: $range" >&2
    echo >&2
    list_minors >&2
    exit 1
  fi
}

build_all() {
  local c kube crio
  for c in "${CONFIGS[@]}"; do
    kube="${c%%=*}"
    crio="${c##*=}"
    echo ">>> Building Kubernetes $kube with $crio"
    make release CRIO_VERSION="$crio" KUBE_VERSION="$kube" 
  done
}

usage() {
  cat <<'EOF'
Usage:
  ./devtools/build-all.sh
  ./devtools/build-all.sh list
  ./devtools/build-all.sh list-minors
  ./devtools/build-all.sh 1.35.3
  ./devtools/build-all.sh 1.35
  ./devtools/build-all.sh 1.33-1.35

Behavior:
  no args       Build all configured targets
  list          List all exact configured targets
  list-minors   List latest supported target per minor
  X.Y.Z         Build exact version
  X.Y           Build latest supported patch in that minor
  X.Y-A.B       Build latest supported patch for each minor in range
EOF
}

# ===== ENTRY =====

if [[ $# -eq 0 ]]; then
  echo "No target specified -> building all configured targets"
  build_all
  exit 0
fi

case "$1" in
  list)
    list_configs
    ;;
  list-minors)
    list_minors
    ;;
  -*|--help|help)
    usage
    ;;
  *-*)
    build_range "$1"
    ;;
  *.*.*)
    build_exact "$1"
    ;;
  *.*)
    build_minor "$1"
    ;;
  *)
    echo "❌ Unrecognized target: $1" >&2
    echo >&2
    usage >&2
    exit 1
    ;;
esac