mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-03 10:37:21 +09:00
Add an optional, opt-in operator that automatically scales the number of read replicas of a managed database in response to load, driven by a new DatabaseHorizontalAutoscaler (DHA) custom resource in the autoscaling.cozystack.io/v1alpha1 group. Implements the database horizontal-autoscaling design proposal. The operator reads the driver metric from VictoriaMetrics and the linked WorkloadMonitor, computes the desired total instance count under the design's guardrails (replica model with PrimaryCount, quorum floor, replication-lag brake with write-activity gating, scale-up/scale-down stabilization windows, step limit, single-flight with a convergence deadline and rollback, tenant-quota ceiling, dry-run), and applies its decision by JSON-merge-patching the target Application's replicas value with a dedicated field manager plus a managed-by marker annotation. Because server-side-apply field ownership does not hold on the aggregated apps.cozystack.io API (opaque spec, managed-fields not round-tripped), a validating admission webhook enforces ownership on the backing Flux HelmRelease -- where kube-apiserver actually runs admission and where a force-applying GitOps writer targets the object directly -- rejecting a replicas change from any identity other than the autoscaler or the apps API extension server. Ships the Postgres/CloudNativePG topology adapter, a Helm package (2-replica leader-elected deployment, PDB, anti-affinity, RBAC with tenant aggregation, cert-manager, VMServiceScrape, PrometheusRule alerts), a PackageSource depending on the monitoring stack, and a bundle opt-in. Covered by table-driven unit tests for the decision math and every guardrail, httptest-backed VictoriaMetrics tests, webhook decision tests, and an apiserver-backed envtest suite that validates the CRD schema and exercises a full reconcile. Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>