carlos / platform Public

region HA G3-B10: config and create predicate

Co-Authored-By: Astra <noreply@openai.com>
claude agent, operated by paul@keymail.dev 4503da7e2cb24e931673c912c44331e5b1a8172c parent 7c5ed58
4 files changed, +107 −5
  • internal/console/ha_pool_scope_test.go +65 −0
  • internal/console/hacapacity.go +18 −4
  • internal/console/seedmove.go +12 −1
  • internal/console/server.go +12 −0

Intent

diff --git a/internal/console/ha_pool_scope_test.go b/internal/console/ha_pool_scope_test.go
new file mode 100644
index 000000000..4cbd3ac17
--- /dev/null
+++ b/internal/console/ha_pool_scope_test.go
@@ -0,0 +1,65 @@
+package console
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestHAPoolConfig(t *testing.T) {
+ for _, tc := range []struct {
+ name, field string
+ valid bool
+ }{
+ {"absent", "", true},
+ {"named", `,"ha_pools":["ha","other"]`, true},
+ {"empty", `,"ha_pools":[]`, false},
+ {"duplicate", `,"ha_pools":["ha","ha"]`, false},
+ {"shared", `,"ha_pools":[""]`, false},
+ {"shared-token", `,"ha_pools":["@shared"]`, false},
+ {"bad-name", `,"ha_pools":["HA"]`, false},
+ {"long-name", `,"ha_pools":["abcdefghijklmnopq"]`, false},
+ {"string", `,"ha_pools":"ha"`, false},
+ {"object", `,"ha_pools":{}`, false},
+ {"number", `,"ha_pools":[1]`, false},
+ {"null", `,"ha_pools":null`, false},
+ {"case", `,"HA_POOLS":["ha"]`, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ raw := strings.Replace(haTestConfig, `"aws_region":"eu-west-1"`, `"aws_region":"eu-west-1"`+tc.field, 1)
+ _, err := parseHACapacityConfig(raw, []string{"ie"}, "ie")
+ if (err == nil) != tc.valid {
+ t.Fatalf("parse = %v; valid=%v", err, tc.valid)
+ }
+ })
+ }
+}
+
+func TestHAPoolCreatePredicate(t *testing.T) {
+ raw := strings.ReplaceAll(haTestConfig, `"de"`, `"us"`)
+ raw = strings.Replace(raw, `"aws_region":"eu-central-1"`, `"ha_pools":["ha"],"aws_region":"eu-central-1"`, 1)
+ for _, invalid := range []bool{false, true} {
+ config := raw
+ if invalid {
+ config = ""
+ }
+ fx := newConsoleFixtureWith(t, consoleFixtureOptions{Tweak: func(c *Config) {
+ c.HomeRegion = "ie"
+ c.HARegions = []string{"ie", "us"}
+ c.HACapacityConfig = config
+ }})
+ if fx.srv.haScopeInvalid != invalid {
+ t.Fatalf("scope invalid = %v", fx.srv.haScopeInvalid)
+ }
+ for _, tc := range []struct {
+ region, pool string
+ want bool
+ }{
+ {"", "", true}, {"ie", "other", true}, {"us", "ha", true},
+ {"us", "", invalid}, {"us", "other", invalid}, {"uk", "ha", false},
+ } {
+ if got := fx.srv.haCreate(tc.region, tc.pool); got != tc.want {
+ t.Errorf("haCreate(%q,%q) = %v", tc.region, tc.pool, got)
+ }
+ }
+ }
+}
diff --git a/internal/console/hacapacity.go b/internal/console/hacapacity.go
index 9da297f15..0d68d17e5 100644
--- a/internal/console/hacapacity.go
+++ b/internal/console/hacapacity.go
@@ -40,9 +40,10 @@ var (
)
type haCoordinates struct {
- AWSRegion string `json:"aws_region"`
- DataBucket string `json:"data_bucket"`
- DataBucketRegion string `json:"data_bucket_region"`
+ HAPools []string `json:"ha_pools,omitempty"`
+ AWSRegion string `json:"aws_region"`
+ DataBucket string `json:"data_bucket"`
+ DataBucketRegion string `json:"data_bucket_region"`
}
type haFixedBox struct {
RegionCode string `json:"region_code"`
@@ -128,7 +129,20 @@ func parseHACapacityConfig(raw string, regions []string, home string) (*haCapaci
return nil, errHACapacity
}
for _, fields := range regionFields {
- if !haOnlyFields(fields, "aws_region", "data_bucket", "data_bucket_region") {
+ if _, present := fields["ha_pools"]; present {
+ pools, ok := haField[[]string](fields, "ha_pools")
+ if !ok || len(pools) == 0 {
+ return nil, errHACapacity
+ }
+ seen := map[string]bool{}
+ for _, pool := range pools {
+ if pool == "" || !provision.ValidPool(pool) || seen[pool] {
+ return nil, errHACapacity
+ }
+ seen[pool] = true
+ }
+ }
+ if !haOnlyFields(fields, "aws_region", "data_bucket", "data_bucket_region", "ha_pools") {
return nil, errHACapacity
}
}
diff --git a/internal/console/seedmove.go b/internal/console/seedmove.go
index 64c81429f..fd2a1c52c 100644
--- a/internal/console/seedmove.go
+++ b/internal/console/seedmove.go
@@ -37,6 +37,17 @@ func (s *Server) haRegion(region string) bool {
return region != "" && slices.Contains(s.cfg.HARegions, region)
}
+func (s *Server) haCreate(region, pool string) bool {
+ if !s.haRegion(region) {
+ return false
+ }
+ if region == "" {
+ region = s.cfg.HomeRegion
+ }
+ pools := s.haPools[region]
+ return s.haScopeInvalid || len(pools) == 0 || slices.Contains(pools, pool)
+}
+
// shouldStampHA is the one place a create decides a record is born HA: an
// instance-kind record with no role, in an HA region. Static records and
// replicas are never stamped: boxes ignore non-instance kinds, and a
@@ -45,7 +56,7 @@ func (s *Server) haRegion(region string) bool {
// runs on one box (spec C7), even on a single-tenant deployment whose region
// is in CONSOLE_HA_REGIONS.
func (s *Server) shouldStampHA(rec provision.Record) bool {
- return rec.KindOrDefault() == "instance" && rec.Role == "" && rec.Runtime == "" && s.haRegion(rec.Region)
+ return rec.KindOrDefault() == "instance" && rec.Role == "" && rec.Runtime == "" && s.haCreate(rec.Region, rec.Pool)
}
// haMoveSentence is RHA-P1a-C1 (approved): name is the host's region.
diff --git a/internal/console/server.go b/internal/console/server.go
index 734a3bba9..3b4a7ead0 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -995,6 +995,8 @@ type Config struct {
// Server is the console's http.Handler.
type Server struct {
+ haPools map[string][]string
+ haScopeInvalid bool
capacityStart sync.Once
capacityDone chan struct{}
haCapacityStart sync.Once
@@ -1845,6 +1847,16 @@ func NewServer(cfg Config, st *Store) (*Server, error) {
billing: newBillingRuntime(cfg.Billing),
seen: map[string]time.Time{},
}
+ if capacity, err := parseHACapacityConfig(cfg.HACapacityConfig, cfg.HARegions, cfg.HomeRegion); err != nil {
+ s.haScopeInvalid = len(cfg.HARegions) > 0
+ } else {
+ s.haPools = make(map[string][]string)
+ for region, coords := range capacity.Regions {
+ if len(coords.HAPools) > 0 {
+ s.haPools[region] = coords.HAPools
+ }
+ }
+ }
if cfg.Demo.Enabled {
// Seed the same cache requests use. Transient state refuses admission,
// never console boot; repairs are picked up after the cache expires.