|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright NVIDIA CORPORATION |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +COMPONENT=${1:-} |
| 19 | + |
| 20 | +if [[ -z "${COMPONENT}" ]]; then |
| 21 | + echo "Usage: $0 <toolkit|device-plugin|mig-manager>" >&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# Verify regctl is available |
| 26 | +if ! command -v regctl &> /dev/null; then |
| 27 | + echo "Error: regctl not found. Please install regctl first." >&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# Map component names to GHCR image repositories and GitHub source repositories |
| 32 | +case "${COMPONENT}" in |
| 33 | + toolkit) |
| 34 | + IMAGE_REPO="ghcr.io/nvidia/container-toolkit" |
| 35 | + GITHUB_REPO="NVIDIA/nvidia-container-toolkit" |
| 36 | + ;; |
| 37 | + device-plugin) |
| 38 | + IMAGE_REPO="ghcr.io/nvidia/k8s-device-plugin" |
| 39 | + GITHUB_REPO="NVIDIA/k8s-device-plugin" |
| 40 | + ;; |
| 41 | + mig-manager) |
| 42 | + IMAGE_REPO="ghcr.io/nvidia/k8s-mig-manager" |
| 43 | + GITHUB_REPO="NVIDIA/mig-parted" |
| 44 | + ;; |
| 45 | + *) |
| 46 | + echo "Error: Unknown component '${COMPONENT}'" >&2 |
| 47 | + echo "Valid components: toolkit, device-plugin, mig-manager" >&2 |
| 48 | + exit 1 |
| 49 | + ;; |
| 50 | +esac |
| 51 | + |
| 52 | +echo "Fetching latest commit from ${GITHUB_REPO}..." >&2 |
| 53 | + |
| 54 | +# Get the latest commit SHA from the main branch using GitHub API. |
| 55 | +# NOTE: We use 8-char truncated SHAs as image tags. This must match the |
| 56 | +# tag convention used by each component's CI pipeline when publishing images. |
| 57 | +GITHUB_API_URL="https://api.github.com/repos/${GITHUB_REPO}/commits/main" |
| 58 | + |
| 59 | +# Use GITHUB_TOKEN if available for authentication (higher rate limits) |
| 60 | +if [[ -n "${GITHUB_TOKEN:-}" ]]; then |
| 61 | + LATEST_COMMIT=$(curl -sSL \ |
| 62 | + -H "Authorization: Bearer ${GITHUB_TOKEN}" \ |
| 63 | + -H "Accept: application/vnd.github.v3+json" \ |
| 64 | + "${GITHUB_API_URL}" | \ |
| 65 | + jq -r '.sha[0:8]') |
| 66 | +else |
| 67 | + LATEST_COMMIT=$(curl -sSL \ |
| 68 | + -H "Accept: application/vnd.github.v3+json" \ |
| 69 | + "${GITHUB_API_URL}" | \ |
| 70 | + jq -r '.sha[0:8]') |
| 71 | +fi |
| 72 | + |
| 73 | +if [[ -z "${LATEST_COMMIT}" || "${LATEST_COMMIT}" == "null" ]]; then |
| 74 | + echo "Error: Failed to fetch latest commit from ${GITHUB_REPO}" >&2 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +echo "Latest commit SHA: ${LATEST_COMMIT}" >&2 |
| 79 | + |
| 80 | +# Construct full image path with commit tag |
| 81 | +FULL_IMAGE="${IMAGE_REPO}:${LATEST_COMMIT}" |
| 82 | + |
| 83 | +echo "Verifying image exists: ${FULL_IMAGE}" >&2 |
| 84 | + |
| 85 | +# Verify the image exists using regctl with retry |
| 86 | +MAX_RETRIES=5 |
| 87 | +RETRY_DELAY=30 |
| 88 | +for i in $(seq 1 ${MAX_RETRIES}); do |
| 89 | + if regctl manifest head "${FULL_IMAGE}" &> /dev/null; then |
| 90 | + echo "Verified ${COMPONENT} image: ${FULL_IMAGE}" >&2 |
| 91 | + echo "${FULL_IMAGE}" |
| 92 | + exit 0 |
| 93 | + fi |
| 94 | + |
| 95 | + if [[ $i -lt ${MAX_RETRIES} ]]; then |
| 96 | + echo "Image not found (attempt $i/${MAX_RETRIES}), waiting ${RETRY_DELAY}s for CI to build..." >&2 |
| 97 | + sleep ${RETRY_DELAY} |
| 98 | + # Exponential backoff: 30s, 60s, 120s, 240s |
| 99 | + RETRY_DELAY=$((RETRY_DELAY * 2)) |
| 100 | + fi |
| 101 | +done |
| 102 | + |
| 103 | +echo "Error: Image ${FULL_IMAGE} does not exist after ${MAX_RETRIES} attempts" >&2 |
| 104 | +echo "The image may not have been built yet for commit ${LATEST_COMMIT}" >&2 |
| 105 | +exit 1 |
0 commit comments