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

# Clustered libvirt Provider (ADR-0007 P1) — spec.topology: cluster
#
# This is the CLUSTERED counterpart to provider-libvirt.yaml: instead of one
# gRPC process bound to one host, a clustered provider fronts N bare hosts that
# the admin registers as Host CRs under a HostPool (see hostpool-clustered.yaml,
# which references this Provider as "libvirt-cluster").
#
# What spec.topology: cluster does: the Provider controller gathers the Host CRs
# in THIS Provider's namespace whose spec.providerRef names this Provider,
# renders them into a versioned-schema, Provider-owned Secret named
# "<provider>-hosts" (here: libvirt-cluster-hosts), and mounts it read-only into
# the provider pod at /etc/virtrigaud/hosts (ADR-0007 D3). The provider watches
# that file and hot-reloads its per-host connections.
#
# The rendered Secret CONTAINS CREDENTIALS: besides each host's id, endpoint and
# labels, it inlines that host's SSH private key and known_hosts, copied from
# the host's credentialSecretRef (or, when unset, this Provider's
# credentialSecretRef). Protect it like the source credential Secrets (RBAC on
# Secrets in this namespace). It is owner-referenced to this Provider and is
# garbage-collected with it; if you flip a Provider back to topology: single,
# delete "<provider>-hosts" by hand if the credentials should not linger.
#
# SAME-NAMESPACE RULE (security): Hosts, HostPools, and every credential Secret
# they use must be in this Provider's namespace. credentialSecretRef below must
# NOT name another namespace: for a clustered Provider a foreign-namespace
# credentialSecretRef is refused (HostCredentialsReady=False, reason
# CredentialRefNamespaceRejected) and never read.
#
# topology: cluster is restricted to libvirt by the Provider validating webhook.
# Single-topology providers (topology: single, the default) are byte-for-byte
# unaffected (ADR-0007 D9).
apiVersion: infra.virtrigaud.io/v1beta1
kind: Provider
metadata:
  name: libvirt-cluster
  namespace: virtrigaud-system
spec:
  type: libvirt
  # The discriminator: cluster puts VirtRigaud in charge of a host set. Default
  # is single (today's one-process-one-host behavior).
  topology: cluster
  # A cluster provider does not bind to a single host endpoint; the per-host
  # endpoints live on the Host CRs. This endpoint is the provider's own gRPC
  # address contract and is otherwise unused by the clustered host path.
  endpoint: qemu+ssh://virt@placeholder.example.com/system
  # Default SSH credentials for every Host that has no credentialSecretRef of
  # its own. Same namespace as this Provider (no namespace field).
  credentialSecretRef:
    name: libvirt-cluster-creds
  runtime:
    mode: Remote
    image: "ghcr.io/projectbeskar/virtrigaud/provider-libvirt:v0.4.2"
    service:
      port: 9443
      # TLS is on-by-default (v0.3.7+); provision a Secret with tls.crt/tls.key/
      # ca.crt, or set enabled: false to opt into plaintext for a lab.
      tls:
        enabled: true
        secretRef:
          name: libvirt-cluster-tls
