# Copyright (c) 2026 VirtRigaud Creators
# SPDX-License-Identifier: Apache-2.0

# Cross-hypervisor migration over S3 (ADR-0006 Slice 1): vSphere → S3 → libvirt.
#
# This is the first REAL cross-hypervisor disk transfer: the vSphere SOURCE
# exports its disk (native vmdk) and streams it up to S3 through the provider
# pod; the libvirt TARGET downloads it from S3 and streams it over SSH into a
# host-side `qemu-img convert -f vmdk -O qcow2` before creating the pool volume.
# Bytes flow host/vCenter → pod → S3 → pod → host; they NEVER traverse a CSI PVC.
#
# Direction scope: all three hypervisors (vSphere, libvirt, Proxmox VE) are now
# validated as both SOURCE and TARGET over S3 in any direction (ADR-0006 Slices
# 1–3). This example shows vSphere → libvirt; see vmmigration-proxmox-s3.yaml for
# a Proxmox direction. NFS is a second validated staging backend (see
# vmmigration-nfs.yaml). The `direct` transfer mode is not implemented and is
# rejected with an actionable message at the Validating phase.
#
# Transfer mode: relay (the universal floor — the provider pod is the S3 client,
# so backend credentials never leave the pod). `auto` resolves to `relay` here.
#
# ─────────────────────────────────────────────────────────────────────────────
# S3 credentials Secret. Credentials are ALWAYS referenced via a Secret — never
# inline in the VMMigration. Required keys: accessKeyID, secretAccessKey.
# Optional: sessionToken (STS / temporary credentials). The Secret must live in
# the same namespace as the VMMigration. Replace the placeholders below with your
# object-store credentials.
apiVersion: v1
kind: Secret
metadata:
  name: s3-migration-credentials
  namespace: default
type: Opaque
stringData:
  accessKeyID: "REPLACE_WITH_ACCESS_KEY_ID"
  secretAccessKey: "REPLACE_WITH_SECRET_ACCESS_KEY"
  # sessionToken: "REPLACE_WITH_SESSION_TOKEN"   # only for temporary/STS creds
---
apiVersion: infra.virtrigaud.io/v1beta1
kind: VMMigration
metadata:
  name: migration-vsphere-to-libvirt-s3
  namespace: default
  labels:
    app: my-app
    environment: production
spec:
  # SOURCE: a VM managed by a vSphere provider.
  source:
    vmRef:
      name: prod-app-server      # an existing VirtualMachine backed by vSphere
    createSnapshot: false
    deleteAfterMigration: false

  # TARGET: create the migrated VM on a libvirt provider.
  target:
    name: prod-app-server-migrated
    providerRef:
      name: libvirt-provider      # a Provider of type Libvirt
      namespace: default
    classRef:
      name: high-performance
    networks:
      - name: production-network

  # S3-compatible object storage staging. The example endpoint uses
  # plaintext HTTP with path-style addressing (a self-hosted S3 service); swap for AWS S3 / MinIO / Ceph RGW
  # as needed. The provider pods must be able to reach this endpoint.
  storage:
    type: s3
    transferMode: relay          # relay (implemented) | auto→relay; direct is rejected
    s3:
      bucket: virtrigaud
      endpoint: http://s3.example.com:9000   # http:// ⇒ plaintext; omit for AWS
      region: us-east-1
      prefix: prod-app-server/    # object key prefix under the bucket
      usePathStyle: true          # true for MinIO/Ceph RGW/self-hosted S3; false for AWS
      credentialsSecretRef:
        name: s3-migration-credentials

  options:
    # The SOURCE always exports its native format and the TARGET converts on
    # import (ADR-0006 D4). For vSphere→libvirt the staged object is a vmdk and
    # libvirt converts it to qcow2 on the host; diskFormat is advisory here.
    diskFormat: qcow2
    compress: false
    verifyChecksums: true         # SHA256 of the transferred object (ADR-0006 D5)
    timeout: "6h"

  metadata:
    purpose: "provider-change"
    project: "cloud-migration-2026"
    createdBy: "platform-team"
