Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Gate the run.cua.ai SPA rollout on a blackbox probe of the canary (step 2 of 2) (#6928)

r33drichards2f5a2a6b9455
diff --git a/libs/fleet/backend/cmd/manifest-contract/main.go b/libs/fleet/backend/cmd/manifest-contract/main.go
index 353b63d644..11dfdfa485 100644
--- a/libs/fleet/backend/cmd/manifest-contract/main.go
+++ b/libs/fleet/backend/cmd/manifest-contract/main.go
@@ -11,6 +11,13 @@ import (
"sigs.k8s.io/yaml"
)
+// cyclops-cs (the SPA) is managed by a Flagger Canary, which owns its replica
+// count and serves production traffic from a generated -primary Deployment.
+// Two invariants below are therefore checked differently for it than for the
+// backend; everything else still applies to both. See
+// docs/decisions/2026-08-16-cyclops-cs-flagger-canary.md.
+const flaggerManaged = "cyclops-cs"
+
var (
servingNames = []string{"cyclops-cs", "cyclops-cs-backend"}
projectorImageTag = regexp.MustCompile(`^main-[0-9]+$`)
@@ -24,8 +31,13 @@ type manifestObject struct {
Namespace string `json:"namespace"`
} `json:"metadata"`
Spec struct {
- Replicas int `json:"replicas"`
- ProgressDeadlineSeconds int `json:"progressDeadlineSeconds"`
+ Replicas int `json:"replicas"`
+ MinReplicas int `json:"minReplicas"`
+ MaxReplicas int `json:"maxReplicas"`
+ ScaleTargetRef objectRef `json:"scaleTargetRef"`
+ TargetRef objectRef `json:"targetRef"`
+ AutoscalerRef objectRef `json:"autoscalerRef"`
+ ProgressDeadlineSeconds int `json:"progressDeadlineSeconds"`
Strategy struct {
Type string `json:"type"`
RollingUpdate struct {
@@ -51,6 +63,13 @@ type labelSelector struct {
MatchLabels map[string]string `json:"matchLabels"`
}
+// Comparable so the scaleTargetRef / autoscalerRef checks can be a single ==.
+type objectRef struct {
+ APIVersion string `json:"apiVersion"`
+ Kind string `json:"kind"`
+ Name string `json:"name"`
+}
+
type topologySpreadConstraint struct {
MaxSkew int `json:"maxSkew"`
TopologyKey string `json:"topologyKey"`
@@ -134,6 +153,10 @@ func verify(reader io.Reader) error {
}
}
+ if err := verifyFlaggerScaling(objects); err != nil {
+ return err
+ }
+
for _, object := range objects {
if object.Metadata.Name == "k8s-state-projector" || object.Metadata.Name == "cyclops-cs-k8s-state-projector" {
return fmt.Errorf("serving manifest must not contain state projector")
@@ -203,8 +226,19 @@ func verifyDeployment(deployment manifestObject, name string) error {
if deployment.APIVersion != "apps/v1" || deployment.Metadata.Namespace != "cyclops-cs" {
return fmt.Errorf("Deployment %q has wrong apiVersion or namespace", name)
}
- if deployment.Spec.Replicas != 2 || deployment.Spec.ProgressDeadlineSeconds != 600 {
- return fmt.Errorf("Deployment %q must set replicas 2 and progressDeadlineSeconds 600", name)
+ if deployment.Spec.ProgressDeadlineSeconds != 600 {
+ return fmt.Errorf("Deployment %q must set progressDeadlineSeconds 600", name)
+ }
+ // Flagger scales this Deployment to zero between rollouts and restores it
+ // during one, so a replica count in git is not merely redundant — Flux
+ // would restore it on every reconcile and fight the scale-down. Requiring
+ // its ABSENCE keeps that from being reintroduced by accident.
+ if name == flaggerManaged {
+ if deployment.Spec.Replicas != 0 {
+ return fmt.Errorf("Deployment %q must not set replicas: the Flagger Canary owns it", name)
+ }
+ } else if deployment.Spec.Replicas != 2 {
+ return fmt.Errorf("Deployment %q must set replicas 2", name)
}
if deployment.Spec.Strategy.Type != "RollingUpdate" || deployment.Spec.Strategy.RollingUpdate.MaxUnavailable != 0 || deployment.Spec.Strategy.RollingUpdate.MaxSurge != 1 {
return fmt.Errorf("Deployment %q must use RollingUpdate maxUnavailable 0 and maxSurge 1", name)
@@ -223,12 +257,60 @@ func verifyDeployment(deployment manifestObject, name string) error {
return fmt.Errorf("Deployment %q must spread pods by app label %q", name, name)
}
+// The Flagger-managed workload deliberately has no replica count in git, so
+// "run.cua.ai serves from two pods" rests entirely on this pair: a pinned HPA
+// that owns the number, and an autoscalerRef pointing Flagger at it. Drop
+// either and the Deployment is defaulted to 1 by the API server, Flagger
+// copies that onto the primary that serves production, and the SPA quietly
+// halves to a single pod — no error, no alert, and CyclopsCSNginxDown only
+// fires once the last pod is gone. Both halves are checked because either one
+// alone is enough to cause it.
+func verifyFlaggerScaling(objects []manifestObject) error {
+ hpa, err := findObject(objects, "HorizontalPodAutoscaler", flaggerManaged)
+ if err != nil {
+ return err
+ }
+ if hpa.APIVersion != "autoscaling/v2" || hpa.Metadata.Namespace != "cyclops-cs" {
+ return fmt.Errorf("HorizontalPodAutoscaler %q has wrong apiVersion or namespace", flaggerManaged)
+ }
+ if hpa.Spec.MinReplicas != 2 || hpa.Spec.MaxReplicas != 2 {
+ return fmt.Errorf("HorizontalPodAutoscaler %q must pin minReplicas and maxReplicas to 2", flaggerManaged)
+ }
+ wantTarget := objectRef{APIVersion: "apps/v1", Kind: "Deployment", Name: flaggerManaged}
+ if hpa.Spec.ScaleTargetRef != wantTarget {
+ return fmt.Errorf("HorizontalPodAutoscaler %q must scale Deployment %q", flaggerManaged, flaggerManaged)
+ }
+
+ canary, err := findObject(objects, "Canary", flaggerManaged)
+ if err != nil {
+ return err
+ }
+ if canary.APIVersion != "flagger.app/v1beta1" || canary.Metadata.Namespace != "cyclops-cs" {
+ return fmt.Errorf("Canary %q has wrong apiVersion or namespace", flaggerManaged)
+ }
+ if canary.Spec.TargetRef != wantTarget {
+ return fmt.Errorf("Canary %q must target Deployment %q", flaggerManaged, flaggerManaged)
+ }
+ wantAutoscaler := objectRef{APIVersion: "autoscaling/v2", Kind: "HorizontalPodAutoscaler", Name: flaggerManaged}
+ if canary.Spec.AutoscalerRef != wantAutoscaler {
+ return fmt.Errorf("Canary %q must set autoscalerRef to HorizontalPodAutoscaler %q", flaggerManaged, flaggerManaged)
+ }
+ return nil
+}
+
func verifyPDB(pdb manifestObject, name string) error {
if pdb.APIVersion != "policy/v1" || pdb.Metadata.Namespace != "cyclops-cs" {
return fmt.Errorf("PodDisruptionBudget %q has wrong apiVersion or namespace", name)
}
- if pdb.Spec.MinAvailable != 1 || pdb.Spec.Selector.MatchLabels["app"] != name {
- return fmt.Errorf("PodDisruptionBudget %q must select app %q with minAvailable 1", name, name)
+ // The Flagger-managed workload serves from the generated -primary
+ // Deployment, whose pods carry that label. A PDB selecting the bare app
+ // label would match zero pods and protect nothing, silently.
+ wantApp := name
+ if name == flaggerManaged {
+ wantApp = name + "-primary"
+ }
+ if pdb.Spec.MinAvailable != 1 || pdb.Spec.Selector.MatchLabels["app"] != wantApp {
+ return fmt.Errorf("PodDisruptionBudget %q must select app %q with minAvailable 1", name, wantApp)
}
return nil
}
diff --git a/libs/fleet/backend/cmd/manifest-contract/main_test.go b/libs/fleet/backend/cmd/manifest-contract/main_test.go
index 256afd1f13..5099e07b13 100644
--- a/libs/fleet/backend/cmd/manifest-contract/main_test.go
+++ b/libs/fleet/backend/cmd/manifest-contract/main_test.go
@@ -133,7 +133,6 @@ metadata:
name: cyclops-cs
namespace: cyclops-cs
spec:
- replicas: 2
progressDeadlineSeconds: 600
strategy:
type: RollingUpdate
@@ -165,7 +164,7 @@ spec:
minAvailable: 1
selector:
matchLabels:
- app: cyclops-cs
+ app: cyclops-cs-primary
---
apiVersion: apps/v1
kind: Deployment
@@ -214,5 +213,138 @@ spec:
selector:
matchLabels:
app: cyclops-cs-backend
+` + hpaDocument() + canaryDocument()
+}
+
+// These two documents are the only thing keeping the Flagger-managed
+// Deployment at two replicas, since its count deliberately isn't in git.
+func hpaDocument() string {
+ return `---
+apiVersion: autoscaling/v2
+kind: HorizontalPodAutoscaler
+metadata:
+ name: cyclops-cs
+ namespace: cyclops-cs
+spec:
+ scaleTargetRef:
+ apiVersion: apps/v1
+ kind: Deployment
+ name: cyclops-cs
+ minReplicas: 2
+ maxReplicas: 2
+`
+}
+
+func canaryDocument() string {
+ return `---
+apiVersion: flagger.app/v1beta1
+kind: Canary
+metadata:
+ name: cyclops-cs
+ namespace: cyclops-cs
+spec:
+ provider: kubernetes
+ targetRef:
+ apiVersion: apps/v1
+ kind: Deployment
+ name: cyclops-cs
+ autoscalerRef:
+ apiVersion: autoscaling/v2
+ kind: HorizontalPodAutoscaler
+ name: cyclops-cs
`
}
+
+// The two Flagger-specific branches exist to catch a regression that puts the
+// old shapes back. Without these, re-adding replicas: 2 or flipping the PDB
+// selector to app: cyclops-cs would pass the suite silently — and a PDB on the
+// bare app label matches zero pods, so it protects nothing while looking fine.
+func TestVerifyRejectsReplicasOnTheFlaggerManagedDeployment(t *testing.T) {
+ manifest := strings.Replace(
+ validManifest(),
+ " name: cyclops-cs\n namespace: cyclops-cs\nspec:\n progressDeadlineSeconds: 600",
+ " name: cyclops-cs\n namespace: cyclops-cs\nspec:\n replicas: 2\n progressDeadlineSeconds: 600",
+ 1,
+ )
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), "must not set replicas") {
+ t.Fatalf("verify() error = %v, want a must-not-set-replicas failure", err)
+ }
+}
+
+func TestVerifyRejectsBarePDBSelectorOnTheFlaggerManagedWorkload(t *testing.T) {
+ manifest := strings.Replace(validManifest(), " app: cyclops-cs-primary", " app: cyclops-cs", 1)
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), `must select app "cyclops-cs-primary"`) {
+ t.Fatalf("verify() error = %v, want a PDB selector failure", err)
+ }
+}
+
+// With no replicas in git, deleting the HPA or dropping the Canary's
+// autoscalerRef silently returns run.cua.ai to a single pod. Each of these is
+// individually sufficient to cause that, so each is rejected on its own.
+func TestVerifyRejectsMissingPinnedHPA(t *testing.T) {
+ manifest := strings.Replace(validManifest(), hpaDocument(), "", 1)
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), `HorizontalPodAutoscaler "cyclops-cs"`) {
+ t.Fatalf("verify() error = %v, want a missing-HPA failure", err)
+ }
+}
+
+func TestVerifyRejectsUnpinnedHPA(t *testing.T) {
+ for _, test := range []struct{ name, from, to string }{
+ {name: "min", from: " minReplicas: 2", to: " minReplicas: 1"},
+ {name: "max", from: " maxReplicas: 2", to: " maxReplicas: 5"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ manifest := strings.Replace(validManifest(), test.from, test.to, 1)
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), "must pin minReplicas and maxReplicas to 2") {
+ t.Fatalf("verify() error = %v, want an unpinned-HPA failure", err)
+ }
+ })
+ }
+}
+
+func TestVerifyRejectsCanaryWithoutAutoscalerRef(t *testing.T) {
+ manifest := strings.Replace(validManifest(), canaryDocument(), `---
+apiVersion: flagger.app/v1beta1
+kind: Canary
+metadata:
+ name: cyclops-cs
+ namespace: cyclops-cs
+spec:
+ provider: kubernetes
+ targetRef:
+ apiVersion: apps/v1
+ kind: Deployment
+ name: cyclops-cs
+`, 1)
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), "must set autoscalerRef") {
+ t.Fatalf("verify() error = %v, want an autoscalerRef failure", err)
+ }
+}
+
+func TestVerifyRejectsCanaryOutsideServingNamespace(t *testing.T) {
+ wrongCanary := strings.Replace(canaryDocument(), " namespace: cyclops-cs", " namespace: default", 1)
+ manifest := strings.Replace(validManifest(), canaryDocument(), wrongCanary, 1)
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), "wrong apiVersion or namespace") {
+ t.Fatalf("verify() error = %v, want a Canary namespace failure", err)
+ }
+}
+
+func TestVerifyRejectsCanaryTargetingAnotherDeployment(t *testing.T) {
+ wrongCanary := strings.Replace(
+ canaryDocument(),
+ " targetRef:\n apiVersion: apps/v1\n kind: Deployment\n name: cyclops-cs",
+ " targetRef:\n apiVersion: apps/v1\n kind: Deployment\n name: another-deployment",
+ 1,
+ )
+ manifest := strings.Replace(validManifest(), canaryDocument(), wrongCanary, 1)
+ err := verify(strings.NewReader(manifest))
+ if err == nil || !strings.Contains(err.Error(), `must target Deployment "cyclops-cs"`) {
+ t.Fatalf("verify() error = %v, want a Canary targetRef failure", err)
+ }
+}