Skip to content

Commit b3eab5f

Browse files
h4x3rotabclaude
andcommitted
refactor(consul-postgres-ha): peer VIPs + static allowlist in mesh-conn
PEERS_JSON shape shrinks from `{id, ports[8]}` per peer to `{id, vip}`. mesh-conn binds infra ports {21000, 21001, 8300, 8301} on 127.50.0.<peer-vip> for every other peer; the 3-byte stream header keeps its (tag, port) shape but the receiver dispatches by direct dial 127.0.0.1:<port> using the static allowlist as the validation set. bootstrap-secrets sheds its PROTOCOL_BASES + computePorts logic; info.json carries identity only (role, ordinal). Tests updated to the new shape, all pass. The allowlist gains 21001 beyond the brief's `{21000, 8300, 8301}` because stock Consul Connect requires one sidecar Envoy per inbound-mesh-accessible service: 21000 for webdemo, 21001 for postgres. SNI-routing both onto a single Envoy is left as a future optimization. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7f0d9e1 commit b3eab5f

3 files changed

Lines changed: 252 additions & 217 deletions

File tree

consul-postgres-ha/bootstrap-secrets/main.go

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
// restarts re-find their own slot.
1717
//
1818
// 3. Write everything dependent services need to a tmpfs volume
19-
// shared via compose. /run/secrets/{gossip,turn,ca-seed} are
19+
// shared via compose. /run/secrets/{turn,ca-seed} are
2020
// mode-0400 binary blobs; /run/instance/info.json carries the
21-
// identity + ordinal + computed per-protocol ports.
21+
// identity + ordinal. Per-protocol ports are no longer computed
22+
// here — every service binds its canonical well-known port on
23+
// 127.0.0.1 (Postgres 5432, Envoy public 21000, Consul 8500/8300/
24+
// 8301, …) and cross-peer routing happens via mesh-conn over
25+
// per-peer loopback VIPs.
2226
//
2327
// 4. Exit 0 so compose `depends_on` with
2428
// `condition: service_completed_successfully` can release the
@@ -131,23 +135,19 @@ func main() {
131135
}
132136
}
133137

134-
// 4. Compute per-protocol ports for this ordinal.
135-
ports := computePorts(cfg.ProtocolBases, ordinal)
136-
137138
instance := InstanceInfo{
138139
InstanceID: info.InstanceID,
139140
AppID: info.AppID,
140141
ComposeHash: info.ComposeHash,
141142
ClusterName: cfg.ClusterName,
142143
Role: cfg.Role,
143144
Ordinal: ordinal,
144-
Ports: ports,
145145
}
146146
if err := writeJSON("/run/instance/info.json", instance); err != nil {
147147
log.Fatalf("write instance info: %v", err)
148148
}
149149

150-
log.Printf("bootstrap done: role=%s ordinal=%d ports=%v", cfg.Role, ordinal, ports)
150+
log.Printf("bootstrap done: role=%s ordinal=%d", cfg.Role, ordinal)
151151
}
152152

153153
// =============================================================================
@@ -159,10 +159,9 @@ type Config struct {
159159
Role string // coordinator | worker
160160
ConsulHTTPAddr string // 127.0.0.1:<port> on the local agent
161161
ExpectedReplicas int // upper bound on ordinal slots to try
162-
ProtocolBases map[string]int
163-
WorkerOrdinal int // optional, set by cluster.tf per-worker (>0)
164-
CoordinatorOrdinal int // optional, set by cluster.tf per-coordinator (>=0)
165-
HasCoordinatorOrdinal bool // distinguishes ordinal=0 from "unset"
162+
WorkerOrdinal int // optional, set by cluster.tf per-worker (>0)
163+
CoordinatorOrdinal int // optional, set by cluster.tf per-coordinator (>=0)
164+
HasCoordinatorOrdinal bool // distinguishes ordinal=0 from "unset"
166165
}
167166

168167
func loadConfig() *Config {
@@ -187,11 +186,6 @@ func loadConfig() *Config {
187186
cfg.CoordinatorOrdinal = n
188187
cfg.HasCoordinatorOrdinal = true
189188
}
190-
// PROTOCOL_BASES: JSON object of name -> base port.
191-
rawBases := mustEnv("PROTOCOL_BASES")
192-
if err := json.Unmarshal([]byte(rawBases), &cfg.ProtocolBases); err != nil {
193-
log.Fatalf("PROTOCOL_BASES not valid JSON: %v", err)
194-
}
195189
if r := os.Getenv("EXPECTED_REPLICAS"); r != "" {
196190
n, err := strconv.Atoi(r)
197191
if err != nil || n <= 0 {
@@ -286,21 +280,12 @@ func claimOrdinal(cfg *Config, instanceID string) (int, error) {
286280
// =============================================================================
287281

288282
type InstanceInfo struct {
289-
InstanceID string `json:"instance_id"`
290-
AppID string `json:"app_id"`
291-
ComposeHash string `json:"compose_hash"`
292-
ClusterName string `json:"cluster_name"`
293-
Role string `json:"role"`
294-
Ordinal int `json:"ordinal"`
295-
Ports map[string]int `json:"ports"`
296-
}
297-
298-
func computePorts(bases map[string]int, ordinal int) map[string]int {
299-
out := make(map[string]int, len(bases))
300-
for name, base := range bases {
301-
out[name] = base + ordinal
302-
}
303-
return out
283+
InstanceID string `json:"instance_id"`
284+
AppID string `json:"app_id"`
285+
ComposeHash string `json:"compose_hash"`
286+
ClusterName string `json:"cluster_name"`
287+
Role string `json:"role"`
288+
Ordinal int `json:"ordinal"`
304289
}
305290

306291
// writeSecretEncoded writes b to path with the given encoding. 0444

0 commit comments

Comments
 (0)