feat: wire registry migration as final unattended step of init_gitlab.sh

- Remove premature migrate_registry_images_to_gitlab() call (was running
  before GitLab wait, against a not-yet-ready gitlab-registry)
- Add registry migration as the true final step of init_gitlab.sh, after
  the GitLab CR is Available and Kong ingress is configured
- Delegates to init_registry.sh migrate so logic lives in one place
- SKIP_REGISTRY_MIGRATE=1 suppresses migration if needed
- Fix migrate_registry_to_gitlab() in init_registry.sh:
  - dst_registry defaults to gitlab-registry.<gitlab_ns>.svc.cluster.local:5000
    (in-cluster address, no port-forward needed)
  - Checks registry:2 pod exists before attempting catalog fetch
  - Counts ok/fail per-repo and reports retry command on partial failure
  - Passes GITLAB_NAMESPACE from init_gitlab.sh caller context

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
chrisfu 2026-04-11 13:11:45 -07:00
parent 1f404dd07c
commit 091012a9e5
2 changed files with 63 additions and 82 deletions

View File

@ -933,60 +933,6 @@ fix_registry_arm64_configure() {
fix_registry_arm64_configure
# ---------------------------------------------------------------------------
# Image migration: registry:2 (knoe-system, port 5000) → gitlab-registry
# Enumerates repositories from the old registry via its HTTP API and emits
# skopeo copy commands. Executes them automatically if skopeo is on PATH.
# Run once after gitlab-registry is Running and the old registry:2 is still up.
# ---------------------------------------------------------------------------
migrate_registry_images_to_gitlab() {
local src_registry="${REGISTRY_MIGRATE_SRC:-registry.${REGISTRY_NAMESPACE:-knoe-system}.svc.cluster.local:5000}"
local dst_registry="${REGISTRY_MIGRATE_DST:-localhost:5000}" # port-forward to gitlab-registry
log "Checking for images to migrate from registry:2 (${src_registry}) to gitlab-registry..."
# Fetch repository catalog via the v2 API using a temporary pod on the cluster
local repos
repos=$(kubectl -n "${REGISTRY_NAMESPACE:-knoe-system}" run registry-catalog \
--image=alpine/curl:latest --restart=Never --rm --attach --quiet \
--overrides="{\"spec\":{\"tolerations\":[{\"operator\":\"Exists\"}],\"containers\":[{\"name\":\"c\",\"image\":\"alpine/curl:latest\",\"command\":[\"sh\",\"-c\",\"curl -s http://${src_registry}/v2/_catalog\"]}]}}" \
2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); [print(r) for r in d.get('repositories',[])]" 2>/dev/null || true)
if [[ -z "$repos" ]]; then
log "No repositories found in registry:2 (or registry unreachable) — skipping migration."
return 0
fi
log "Found repositories in registry:2:"
printf '%s\n' "$repos" | sed 's/^/ /'
log "Migration commands (copy each image to gitlab-registry):"
log " NOTE: run these from a host with 'skopeo' and access to both registries,"
log " or after port-forwarding: kubectl -n gitlab port-forward svc/gitlab-registry 5001:5000"
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
echo " skopeo copy --all docker://${src_registry}/${repo} docker://${dst_registry}/${repo} --dest-tls-verify=false --src-tls-verify=false"
done <<< "$repos"
if command -v skopeo >/dev/null 2>&1; then
log "skopeo detected — attempting automated migration..."
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
log " Copying ${repo}..."
skopeo copy --all \
"docker://${src_registry}/${repo}" \
"docker://${dst_registry}/${repo}" \
--dest-tls-verify=false --src-tls-verify=false 2>&1 || \
warn " Could not copy ${repo} (may require manual migration)."
done <<< "$repos"
log "Image migration complete."
else
log "skopeo not found — run the commands above manually to complete migration."
fi
}
migrate_registry_images_to_gitlab
log "GitLab CR applied — operator is reconciling (this may take 10-20 minutes)."
log "Monitor progress: kubectl -n ${NAMESPACE} get gitlab ${GITLAB_RELEASE} -w"
log "Watch pods: kubectl -n ${NAMESPACE} get pods -w"
@ -1038,6 +984,29 @@ spec:
number: 8181
EOF
# ---------------------------------------------------------------------------
# Registry migration: registry:2 (knoe-system) → gitlab-registry
# Runs unattended after GitLab is Ready. Delegates to init_registry.sh
# migrate which uses skopeo when available, otherwise prints commands.
# Skipped if the old registry:2 has no images or is already gone.
# Set SKIP_REGISTRY_MIGRATE=1 to suppress.
# ---------------------------------------------------------------------------
if [[ "${SKIP_REGISTRY_MIGRATE:-0}" != "1" ]]; then
_init_registry_sh="${SCRIPT_DIR}/init_registry.sh"
if [[ -x "$_init_registry_sh" ]]; then
log "--- Registry migration: registry:2 → gitlab-registry ---"
# Pass the gitlab namespace so the migrate action knows where to send images.
GITLAB_NAMESPACE="$NAMESPACE" \
REGISTRY_NAMESPACE="${REGISTRY_NAMESPACE:-${SERVICE_NAMESPACE:-knoe-system}}" \
"$_init_registry_sh" migrate || \
warn "Registry migration encountered errors — check output above."
else
warn "init_registry.sh not found at ${_init_registry_sh}; skipping registry migration."
warn "Run manually: ./etc/init_registry.sh migrate"
fi
unset _init_registry_sh
fi
log "Done."
log "GitLab will be reachable at http://${GITLAB_DOMAIN}/ once pods are Running."
log "Initial root password: kubectl -n ${NAMESPACE} get secret ${GITLAB_RELEASE}-gitlab-initial-root-password -o jsonpath='{.data.password}' | base64 -d"

View File

@ -456,20 +456,30 @@ status_registry() {
}
migrate_registry_to_gitlab() {
# Enumerate images from the registry:2 instance (registry.<ns>.svc.cluster.local:5000)
# and emit / execute skopeo copy commands to transfer them to gitlab-registry.
# Enumerate images from the registry:2 instance (registry.<src_ns>.svc.cluster.local:5000)
# and copy them to gitlab-registry using skopeo when available.
# Safe to run multiple times — skopeo copy is idempotent.
# Skipped gracefully if registry:2 has no images or is already gone.
local src_ns="${REGISTRY_NAMESPACE:-knoe-system}"
local src_registry="registry.${src_ns}.svc.cluster.local:5000"
local gitlab_ns="${GITLAB_NAMESPACE:-gitlab}"
local dst_registry="gitlab-registry.${gitlab_ns}.svc.cluster.local:5000"
local src_registry="${REGISTRY_MIGRATE_SRC:-registry.${src_ns}.svc.cluster.local:5000}"
local dst_registry="${REGISTRY_MIGRATE_DST:-gitlab-registry.${gitlab_ns}.svc.cluster.local:5000}"
# Check source registry is still reachable.
echo "Checking source registry:2 at ${src_registry} ..."
echo "Registry migration: ${src_registry}${dst_registry}"
# Verify the source registry:2 pod exists at all before trying to catalog it.
if ! kubectl -n "$src_ns" get deploy/registry >/dev/null 2>&1 && \
! kubectl -n "$src_ns" get pods -l app=registry --field-selector=status.phase=Running 2>/dev/null | grep -q Running; then
echo "INFO: registry:2 not found in namespace '${src_ns}' — nothing to migrate."
return 0
fi
# Fetch repository catalog via a temporary curl pod on the cluster.
local repos
repos=$(kubectl -n "$src_ns" run registry-catalog-migrate \
--image=curlimages/curl:latest --restart=Never --rm -it --quiet \
-- curl -sf "http://${src_registry}/v2/_catalog" 2>/dev/null \
--image=alpine/curl:latest --restart=Never --rm --attach --quiet \
--overrides="{\"spec\":{\"tolerations\":[{\"operator\":\"Exists\"}],\"containers\":[{\"name\":\"c\",\"image\":\"alpine/curl:latest\",\"command\":[\"sh\",\"-c\",\"curl -sf http://${src_registry}/v2/_catalog\"]}]}}" \
2>/dev/null \
| python3 -c "import sys,json; [print(r) for r in json.load(sys.stdin).get('repositories',[])]" 2>/dev/null || true)
if [[ -z "$repos" ]]; then
@ -477,35 +487,37 @@ migrate_registry_to_gitlab() {
return 0
fi
echo "Found repositories to migrate:"
echo "Repositories to migrate:"
printf '%s\n' "$repos" | sed 's/^/ /'
echo ""
echo "Migration commands (run on a host with skopeo and cluster access):"
echo " # Port-forward gitlab-registry: kubectl -n ${gitlab_ns} port-forward svc/gitlab-registry 5001:5000"
echo " # Replace localhost:5001 below with the actual dst if running in-cluster"
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
echo " skopeo copy --all docker://${src_registry}/${repo} docker://localhost:5001/${repo} --dest-tls-verify=false --src-tls-verify=false"
done <<< "$repos"
if command -v skopeo >/dev/null 2>&1; then
echo ""
echo "skopeo found — attempting automatic migration to ${dst_registry} ..."
echo "skopeo found — migrating images automatically..."
local _ok=0 _fail=0
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
echo " Copying ${repo} ..."
skopeo copy --all \
"docker://${src_registry}/${repo}" \
"docker://${dst_registry}/${repo}" \
--dest-tls-verify=false --src-tls-verify=false \
&& echo " [OK] ${repo}" || echo " [WARN] ${repo} copy failed — check manually"
if skopeo copy --all \
"docker://${src_registry}/${repo}" \
"docker://${dst_registry}/${repo}" \
--dest-tls-verify=false --src-tls-verify=false 2>&1; then
echo " [OK] ${repo}"
(( _ok++ )) || true
else
echo " [WARN] ${repo} — copy failed, may need manual retry"
(( _fail++ )) || true
fi
done <<< "$repos"
echo "Migration complete. Verify with: kubectl -n ${gitlab_ns} exec svc/gitlab-registry -- /bin/registry garbage-collect /etc/docker/registry/config.yml"
echo "Migration complete: ${_ok} succeeded, ${_fail} failed."
if [[ $_fail -gt 0 ]]; then
echo "Failed repos can be retried with: REGISTRY_MIGRATE_SRC=${src_registry} REGISTRY_MIGRATE_DST=${dst_registry} ./etc/init_registry.sh migrate"
fi
else
echo ""
echo "INFO: skopeo not found on this host — run the commands above manually to complete migration."
echo " Install: sudo apt-get install skopeo OR brew install skopeo"
echo "skopeo not found — run these commands manually (install: sudo apt-get install skopeo):"
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
echo " skopeo copy --all docker://${src_registry}/${repo} docker://${dst_registry}/${repo} --dest-tls-verify=false --src-tls-verify=false"
done <<< "$repos"
fi
}