feat(elbv2): implement ModifyListener (#268)

* feat(elbv2): implement ModifyListener

Adds AWS-compatible ModifyListener to the spinifex ELBv2 stack so
listeners can be edited in place — port, protocol, and default
actions — without the current delete+create dance that briefly drops
the listener from HAProxy.

- Handler interface, impl, and NATS wrapper get ModifyListener;
  validation mirrors CreateListener (LB type ↔ protocol, dup-port
  excluding self, target-group existence + protocol compatibility).
- Daemon subscribes to elbv2.ModifyListener on the spinifex-workers
  queue and a new gateway action dispatches it.
- Frontend exposes a per-row edit button that pre-fills a dialog from
  the existing listener and calls ModifyListenerCommand, replacing
  the "Listeners cannot be edited" note.
- Unit + NATS round-trip + frontend tests cover the new flow.

Cert / SslPolicy / Alpn handling stays out of scope and lands with
HTTPS listener support.

* test(e2e): cover ModifyListener in lb harness

Adds an Internal_ALB_ModifyListener subtest to TestLoadBalancer that
exercises in-place listener edits end-to-end:

- Provisions tgA + tgB sharing the same app instances.
- Brings up an internal ALB with HTTP:80 → tgA, baselines round-robin.
- ModifyListener port=8090, asserts DescribeListeners shows the new
  port and that probes on 8090 still round-robin — proves HAProxy is
  reconciled without dropping the listener (the regression mulga-944
  fixes; the frontend previously delete+created on every edit).
- ModifyListener DefaultActions → tgB, asserts traffic still flows.

Also factors a probeAtPort helper so the order JSON can target any
listener port, and updates TEST_COVERAGE.md with phase 3e.

* fix(ui): widen listener add/edit dialog so content stays inside the box

AlertDialogContent defaults to max-w-xs (20rem) which clipped the listener
form on the right edge. Use a 32rem cap with grid-cols-[minmax(0,1fr)] +
min-w-0 on the form so CSS grid tracks shrink instead of expanding to
max-content.

* test: cover ModifyListener + system instance dispatch

Lifts ModifyListener diff coverage by exercising nil/empty arn paths,
NLB protocol acceptance + rejection, protocol-only revalidation against
existing target groups, and the launcher-nats wire format error paths
(missing input, missing instance type, decode failures, no responder,
default timeout fallback).

Adds a new spinifex/daemon/daemon_system_dispatch_test.go covering
splitSubjectTail edge cases, respondWithSystem{Launch,Terminate}{Output,
OK,Error} envelope shape, handler entry points for invalid JSON and
missing instance ID, and subscribeSystemTerminate nil-conn + idempotent
re-subscribe behaviour.

Fixes the flaky TestHandleEC2DescribeInstanceTypes/NoFilter… subtest by
selecting a 2-vCPU instance type that also fits schedulable memory, so
hosts with smaller schedulableMem (CI 4 vCPU / ~13 GiB) don't pick a
16 GiB-mem type that fails allocation.

* test(e2e): wait for tgB after listener swap, not before

Spinifex health-checks only target groups wired into a listener's
HAProxy config. Waiting for tgB to report healthy before
modifyListenerDefaultTG attaches it to the listener is impossible —
the agent never probes its targets, so they stay in initial and the
2m EventuallyErr trips.

Drop the pre-swap tgB wait. After modifyListenerDefaultTG, replace
the fixed 3s sleep with WaitForTargetsHealthy on tgB (post-swap),
which is now the listener's DefaultAction.

* test(e2e): derive LB name from ARN in lbENI/deleteLB ENI filter

The ENI description filter was building the LB-name token from a
hard-coded "alb"/"nlb" template (e.g. `ELB app/lb-e2e-alb-int/<id>`),
which only matched LBs whose name happened to be `lb-e2e-alb-*` or
`lb-e2e-nlb-*`. The ModifyListener suite names its LB `lb-e2e-mod`,
so lbENI's EventuallyErr could never find the ENI and tripped after
30s. deleteLB had the same bug: sysInstanceID stayed empty and
WaitForENICleanup waited for a non-existent filter, allowing the
next suite's createLB to race the sys.micro VM reclaim.

Parse the LB name straight out of lb.ARN (penultimate path segment,
same shape createLB already relies on) so the filter matches any
caller-chosen name. Drop the now-redundant scheme arg from lbENI.

* test(e2e): poll for listener serving on new port after ModifyListener

The post-modifyListenerPort settle was a hard 3s sleep, but lb-agent
polls for new HAProxy config on a 5s tick. After the API returns, the
agent has typically not yet detected the config-hash change, so
HAProxy is still bound to the old port — every probe to the new port
RSTs and probeAtPort sees 0/0 successful.

Replace the sleep with waitForListenerServing: a polling loop that
fires 2 probes via the shared trigger client every 2s until at least
one returns a parsable instance_id, with a 60s ceiling. Factor the
trigger-and-fetch out of probeAtPort into probeOnce so the polling
helper can reuse it without the require.* asserts.
This commit is contained in:
Josh Siviour
2026-05-30 09:18:52 +10:00
committed by GitHub
parent c1f44b36da
commit 287487a4f2
19 changed files with 1441 additions and 42 deletions
+13
View File
@@ -760,6 +760,19 @@ Requires pool mode with external IPAM (NOT dev_networking).
- Terminate client VM
- Cleanup: delete LB, verify ENI removal, delete TG
### Phase 3e: ALB Internal — ModifyListener
- Create two HTTP target groups (tgA, tgB), register the same 2 app instances in each
- `create-load-balancer` (type=application, scheme=internal)
- `create-listener` (HTTP:80) → tgA
- Wait for ALB active, both TGs healthy
- Baseline probe at port 80 → assert round-robin across both instances
- `modify-listener` Port=8090 (same tgA), verify DescribeListeners persisted
- Probe at port 8090 → assert round-robin (proves HAProxy reconciled without
dropping the listener)
- `modify-listener` DefaultActions → tgB (same port 8090)
- Probe at port 8090 → assert round-robin still flows through tgB
- Cleanup: delete listener, LB, deregister, delete both TGs
### Phase 4: Cleanup (trap)
- Terminate app instances, wait for terminated
- Delete key pair, subnet, IGW, VPC
+195 -16
View File
@@ -105,6 +105,10 @@ func TestLoadBalancer(t *testing.T) {
time.Sleep(15 * time.Second)
runLBSuite(t, client, fixture, kindNLB, "internal", nil, "")
})
t.Run("Internal_ALB_ModifyListener", func(t *testing.T) {
time.Sleep(15 * time.Second)
runModifyListenerSuite(t, client, fixture)
})
}
// --- Fixture: shared VPC, subnet, IGW, SG, app instances ----------------
@@ -439,7 +443,7 @@ func runLBSuite(t *testing.T, c *harness.AWSClient, f *sharedFixture, kind lbKin
harness.WaitForLBActive(t, c, lb.ARN, label, 5*time.Minute)
harness.WaitForTargetsHealthy(t, c, tgArn, 2, label, 2*time.Minute)
eni := lbENI(t, c, scheme, eniDescPrefix, lb.ID)
eni := lbENI(t, c, eniDescPrefix, lb)
if scheme == "internet-facing" {
ip := publicIP(eni)
@@ -557,15 +561,14 @@ func deleteLB(t *testing.T, c *harness.AWSClient, lb lbInfo) {
if lb.ARN == "" {
return
}
prefix, lbName := "app", "alb"
prefix := "app"
if lb.Type == "network" {
prefix, lbName = "net", "nlb"
prefix = "net"
}
suffix := "int"
if lb.Scheme == "internet-facing" {
suffix = "inet"
}
filter := fmt.Sprintf("ELB %s/lb-e2e-%s-%s/%s", prefix, lbName, suffix, lb.ID)
parts := strings.Split(lb.ARN, "/")
require.GreaterOrEqualf(t, len(parts), 3, "deleteLB: malformed LB ARN %q", lb.ARN)
lbName := parts[len(parts)-2]
filter := fmt.Sprintf("ELB %s/%s/%s", prefix, lbName, lb.ID)
// Capture the underlying sys.micro VM id before deleting so we can wait
// for it to actually terminate. ELBv2.DeleteLoadBalancer returns once
@@ -656,17 +659,193 @@ func deleteListener(t *testing.T, c *harness.AWSClient, arn string) {
}
}
func lbENI(t *testing.T, c *harness.AWSClient, scheme, prefix, lbID string) *ec2.NetworkInterface {
// modifyListenerPort changes the listener's port in place. Verifies that
// HAProxy is reconciled without the listener being deleted (no traffic outage).
func modifyListenerPort(t *testing.T, c *harness.AWSClient, listenerArn string, port int64) {
t.Helper()
suffix := "int"
if scheme == "internet-facing" {
suffix = "inet"
_, err := c.ELBv2.ModifyListener(&elbv2.ModifyListenerInput{
ListenerArn: aws.String(listenerArn),
Port: aws.Int64(port),
})
require.NoErrorf(t, err, "modify-listener port=%d", port)
t.Logf("listener %s: port → %d", listenerArn, port)
}
// modifyListenerDefaultTG swaps the listener's default target group.
func modifyListenerDefaultTG(t *testing.T, c *harness.AWSClient, listenerArn, tgArn string) {
t.Helper()
_, err := c.ELBv2.ModifyListener(&elbv2.ModifyListenerInput{
ListenerArn: aws.String(listenerArn),
DefaultActions: []*elbv2.Action{{
Type: aws.String("forward"),
TargetGroupArn: aws.String(tgArn),
}},
})
require.NoError(t, err, "modify-listener default actions")
t.Logf("listener %s: default TG → %s", listenerArn, tgArn)
}
// describeListenerPort fetches the current port of a listener — used to
// assert that ModifyListener actually persisted before probing.
func describeListenerPort(t *testing.T, c *harness.AWSClient, listenerArn string) int64 {
t.Helper()
out, err := c.ELBv2.DescribeListeners(&elbv2.DescribeListenersInput{
ListenerArns: []*string{aws.String(listenerArn)},
})
require.NoError(t, err, "describe-listeners")
require.NotEmpty(t, out.Listeners)
return aws.Int64Value(out.Listeners[0].Port)
}
// runModifyListenerSuite verifies that an in-place listener edit reroutes
// traffic without dropping the listener — the regression mulga-944 fixes
// (frontend previously delete+created on every edit).
//
// - Start: listener HTTP:80 → tgA, probes round-robin across 2 app instances.
// - ModifyListener port=8090 → same tgA. Probe at 8090 must succeed
// immediately (port: 80 must not be active any more, port 8090 must serve).
// - ModifyListener defaultActions → tgB (same backends). Probe still
// round-robins; proves DefaultActions swap is honoured by HAProxy.
func runModifyListenerSuite(t *testing.T, c *harness.AWSClient, f *sharedFixture) {
t.Helper()
const label = "ALB internal ModifyListener"
const altPort int64 = 8090
tgA := createTargetGroup(t, c, f, "lb-e2e-mod-tg-a", "HTTP", httpPort, "/index.html")
t.Cleanup(func() { deleteTargetGroup(t, c, tgA) })
tgB := createTargetGroup(t, c, f, "lb-e2e-mod-tg-b", "HTTP", httpPort, "/index.html")
t.Cleanup(func() { deleteTargetGroup(t, c, tgB) })
registerTargets(t, c, tgA, f.AppInstanceIDs)
t.Cleanup(func() { deregisterTargets(t, c, tgA, f.AppInstanceIDs) })
registerTargets(t, c, tgB, f.AppInstanceIDs)
t.Cleanup(func() { deregisterTargets(t, c, tgB, f.AppInstanceIDs) })
lb := createLB(t, c, f, "lb-e2e-mod", "application", "internal")
t.Cleanup(func() { deleteLB(t, c, lb) })
listener := createListener(t, c, lb.ARN, "HTTP", httpPort, tgA)
t.Cleanup(func() { deleteListener(t, c, listener) })
harness.WaitForLBActive(t, c, lb.ARN, label, 5*time.Minute)
harness.WaitForTargetsHealthy(t, c, tgA, 2, label+" tgA", 2*time.Minute)
eni := lbENI(t, c, "app", lb)
priv := privateIP(eni)
require.NotEmpty(t, priv, label+" needs private IP")
// Baseline: traffic on the original port.
probeAtPort(t, f, priv, httpPort, label+" before modify (port 80)")
// In-place port change.
modifyListenerPort(t, c, listener, altPort)
assert.Equal(t, altPort, describeListenerPort(t, c, listener), "listener port persisted")
// lb-agent picks up the new HAProxy config on its next 5s tick, not
// synchronously with the ModifyListener API. Poll until the listener
// actually answers on altPort before driving the assertive probe.
waitForListenerServing(t, f, priv, altPort, label+" port 8090", 60*time.Second)
probeAtPort(t, f, priv, altPort, label+" after port-modify (port 8090)")
// Defaults: in-place TG swap on same port.
modifyListenerDefaultTG(t, c, listener, tgB)
harness.WaitForTargetsHealthy(t, c, tgB, 2, label+" tgB (post-swap)", 2*time.Minute)
probeAtPort(t, f, priv, altPort, label+" after TG-swap (still port 8090, now tgB)")
}
// waitForListenerServing polls the shared client with a tiny round of probes
// until at least one returns a successful instance_id payload. Used after
// ModifyListener calls so the assertive probeAtPort doesn't race the
// lb-agent's poll-driven HAProxy reload (5s tick + reload latency).
func waitForListenerServing(t *testing.T, f *sharedFixture, lbIP string, port int64, label string, timeout time.Duration) {
t.Helper()
harness.EventuallyErr(t, func() error {
r, err := probeOnce(f, lbIP, port, 2)
if err != nil {
return err
}
if r.Successful == 0 {
return fmt.Errorf("%s: listener not serving on :%d (0/%d successful)", label, port, r.Total)
}
return nil
}, timeout, 2*time.Second)
}
// probeOnce drives the shared client to issue `count` HTTP probes at
// lbIP:port and returns the parsed TrafficResult. Errors are returned rather
// than failing the test so callers can poll.
func probeOnce(f *sharedFixture, lbIP string, port int64, count int) (harness.TrafficResult, error) {
resultsFile := fmt.Sprintf("mod-%d-%d.txt", port, time.Now().UnixNano())
order := map[string]any{
"proto": "http",
"ip": lbIP,
"count": count,
"outfile": resultsFile,
"http_port": port,
"tcp_port": tcpPort,
}
lbName := "alb"
if prefix == "net" {
lbName = "nlb"
body, err := json.Marshal(order)
if err != nil {
return harness.TrafficResult{}, err
}
desc := fmt.Sprintf("ELB %s/lb-e2e-%s-%s/%s", prefix, lbName, suffix, lbID)
triggerURL := fmt.Sprintf("http://%s:%d/", f.ClientPublicIP, triggerPort)
client := &http.Client{Timeout: 90 * time.Second}
resp, err := client.Post(triggerURL, "application/json", bytes.NewReader(body))
if err != nil {
return harness.TrafficResult{}, fmt.Errorf("trigger: %w", err)
}
resp.Body.Close()
if resp.StatusCode != 200 {
return harness.TrafficResult{}, fmt.Errorf("trigger status %d", resp.StatusCode)
}
results, err := plainHTTPGet(
fmt.Sprintf("http://%s:%d/%s", f.ClientPublicIP, httpPort, resultsFile),
10*time.Second)
if err != nil {
return harness.TrafficResult{}, fmt.Errorf("fetch %s: %w", resultsFile, err)
}
return harness.VerifyResultsLines(results, "http"), nil
}
// probeAtPort runs round-robin probes from the shared client to lbIP:port and
// asserts both app instances respond. Mirrors runInternalTrafficViaClient but
// takes an explicit port so ModifyListener edits can be verified.
func probeAtPort(t *testing.T, f *sharedFixture, lbIP string, port int64, label string) {
t.Helper()
resultsFile := fmt.Sprintf("mod-%d-%d.txt", port, time.Now().UnixNano())
order := map[string]any{
"proto": "http",
"ip": lbIP,
"count": probesPerRun,
"outfile": resultsFile,
"http_port": port,
"tcp_port": tcpPort,
}
body, err := json.Marshal(order)
require.NoError(t, err)
triggerURL := fmt.Sprintf("http://%s:%d/", f.ClientPublicIP, triggerPort)
client := &http.Client{Timeout: 90 * time.Second}
resp, err := client.Post(triggerURL, "application/json", bytes.NewReader(body))
require.NoErrorf(t, err, "trigger probe %s", label)
resp.Body.Close()
require.Equalf(t, 200, resp.StatusCode, "trigger probe %s status", label)
results, err := plainHTTPGet(
fmt.Sprintf("http://%s:%d/%s", f.ClientPublicIP, httpPort, resultsFile),
10*time.Second)
require.NoErrorf(t, err, "fetch %s", resultsFile)
harness.AssertRoundRobin(t,
harness.VerifyResultsLines(results, "http"),
1, probesPerRun/2, label)
}
func lbENI(t *testing.T, c *harness.AWSClient, prefix string, lb lbInfo) *ec2.NetworkInterface {
t.Helper()
parts := strings.Split(lb.ARN, "/")
require.GreaterOrEqualf(t, len(parts), 3, "lbENI: malformed LB ARN %q", lb.ARN)
lbName := parts[len(parts)-2]
desc := fmt.Sprintf("ELB %s/%s/%s", prefix, lbName, lb.ID)
var eni *ec2.NetworkInterface
harness.EventuallyErr(t, func() error {
out, err := c.EC2.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{