From b09db740014ad58fdcfd07e3cc2f95db2d78ee2e Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 25 Oct 2026 12:19:26 -0400 Subject: [PATCH 1/2] fix(cni): Keep traffic policy classifying when the shard watch is down In hashed egress mode, galactic-cni gave traffic policy its NAT shard locators only after the EgressShard watch had listed the cluster. Every restart resets the datapath's "locators synced" flag, so a node whose watch could not start classified every encapsulated path as unknown, and any rule naming a path dropped the packet with reason classification_error. Tenant egress still left through the shards, but the replies and all private traffic from other nodes were dropped at the workload. Each sweep now hands traffic policy every shard SID the node can egress through: the configured shards, every member of a published shard group, and the watch's shards once it has synced. usid_egress sends only to configured shards or published group members, so that set covers every path the node can take before the watch syncs. A sweep that cannot read a group skips the update rather than mark an incomplete set complete. A datapath without the shard group map hands over the configured shards, the only ones it can route to. Fixes #1084 Co-Authored-By: Claude Opus 5.5 --- internal/installer/egressgroup.go | 45 ++++++++++++-- internal/installer/egressgroup_test.go | 84 ++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/internal/installer/egressgroup.go b/internal/installer/egressgroup.go index 6cf5e8dc4..8d3a607c5 100644 --- a/internal/installer/egressgroup.go +++ b/internal/installer/egressgroup.go @@ -71,9 +71,12 @@ type egressGroup struct { cfg config.EgressGroupConfig staticSIDs []net.IP - // natLocators, when set, receives every configured and cluster shard SID - // once the EgressShard watch has listed the cluster, for traffic policy's - // path classification. + // natLocators, when set, receives on every sweep the SID of every shard + // this node can egress through, for traffic policy's path + // classification: the configured shards, every member of a published + // group, and the cluster's EgressShards once the watch has listed them. + // The first two cover every path usid_egress can take, so classification + // works before the watch syncs, and while it cannot start. natLocators func([]net.IP) pinDir string trigger chan struct{} @@ -236,6 +239,9 @@ func (g *egressGroup) sweepWith( foreignTableIDs map[uint32]struct{}, ) error { if groups == nil { + if g.natLocators != nil { + g.natLocators(g.staticSIDs) + } _, err := g.refresh(table, foreignTableIDs, g.staticSIDs, nil) return err } @@ -345,8 +351,17 @@ func (g *egressGroup) sweepHashed( } } - if synced && g.natLocators != nil { - g.natLocators(append(append([]net.IP(nil), recognized...), shardSIDs(shards)...)) + if g.natLocators != nil { + var listed []bgpv1alpha1.EgressShard + if synced { + listed = shards + } + if sids, err := g.knownShardSIDs(groups, listed); err != nil { + slog.Error("Failed to read the egress shard groups; traffic policy's NAT locators are not updated", + "err", err) + } else { + g.natLocators(sids) + } } refsKnown := g.refs != nil @@ -933,6 +948,26 @@ func isUplink(ifindex int) bool { return ok } +// knownShardSIDs is every shard SID usid_egress can send this node's tenants +// to: the configured shards, every member of a published group, and shards, +// the cluster's EgressShards when the watch has listed them. It fails when a +// group cannot be read, rather than report an incomplete set. +func (g *egressGroup) knownShardSIDs( + groups *egressroutemap.ShardGroupTable, shards []bgpv1alpha1.EgressShard, +) ([]net.IP, error) { + sids := append(append([]net.IP(nil), g.staticSIDs...), shardSIDs(shards)...) + for id := range uint32(egressroutemap.MaxShardGroups) { + members, err := groups.Members(id) + if err != nil { + return nil, fmt.Errorf("read egress shard group %d members: %w", id, err) + } + for _, m := range members { + sids = append(sids, net.IP(m.SID.AsSlice())) + } + } + return sids, nil +} + // shardSIDs are the parseable status.shardSIDs of shards, whatever their // readiness: a draining or unready shard's replies still return through it. func shardSIDs(shards []bgpv1alpha1.EgressShard) []net.IP { diff --git a/internal/installer/egressgroup_test.go b/internal/installer/egressgroup_test.go index ade1b27bf..ef58b5196 100644 --- a/internal/installer/egressgroup_test.go +++ b/internal/installer/egressgroup_test.go @@ -574,6 +574,90 @@ func TestSweepHashed_UnsyncedOrFailingWatch(t *testing.T) { } } +// natLocatorSink records the shard SIDs each sweep hands traffic policy. +type natLocatorSink struct { + calls int + sids map[string]bool +} + +func (s *natLocatorSink) set(sids []net.IP) { + s.calls++ + s.sids = map[string]bool{} + for _, sid := range sids { + s.sids[sid.String()] = true + } +} + +// TestSweepHashed_NATLocatorsWithoutWatch checks that traffic policy gets the +// shard SIDs this node can egress through on every sweep, whether or not the +// EgressShard watch has started or synced (#1084): the configured shards and +// every member of a published group, plus the watch's shards once synced. +// Without them every tenant packet on a path rule fails classification. +func TestSweepHashed_NATLocatorsWithoutWatch(t *testing.T) { + configured := shardSIDFor(0x10) + published := []string{staticSIDFor(0x20), staticSIDFor(0x21)} + watched := shardSIDFor(0x30) + cases := map[string]struct { + lister egressShardLister + want []string + }{ + "WatchNotStarted": {nil, append([]string{configured}, published...)}, + "WatchUnsynced": {&fakeLister{synced: false}, append([]string{configured}, published...)}, + "WatchFailing": { + &fakeLister{shards: []bgpv1alpha1.EgressShard{dual("watched", 0x30)}, synced: true, err: errInjected}, + append([]string{configured}, published...), + }, + "WatchSynced": { + &fakeLister{shards: []bgpv1alpha1.EgressShard{dual("watched", 0x30)}, synced: true}, + append([]string{configured, watched}, published...), + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + f := newFixture(t, testHashed, []string{configured}, nil) + candidates := []egressroutemap.ShardCandidate{ + {SID: net.ParseIP(shardSIDFor(0x20))}, {SID: net.ParseIP(shardSIDFor(0x21))}, + } + if _, err := f.groups.Apply(0, egressroutemap.GroupPolicy{}, candidates); err != nil { + t.Fatal(err) + } + if tc.lister != nil { + f.g.shards = tc.lister + } + sink := &natLocatorSink{} + f.g.natLocators = sink.set + _ = f.sweep(t) + if sink.calls != 1 { + t.Fatalf("natLocators called %d times, want once", sink.calls) + } + for _, sid := range tc.want { + if !sink.sids[sid] { + t.Errorf("natLocators got %v, missing %s", sink.sids, sid) + } + } + if len(sink.sids) != len(tc.want) { + t.Errorf("natLocators got %d SIDs %v, want %v", len(sink.sids), + sink.sids, tc.want) + } + }) + } +} + +// TestSweep_NATLocatorsWithoutGroupMap checks that a datapath without the +// shard group map, which can only route to the configured shards, hands +// traffic policy those shards. +func TestSweep_NATLocatorsWithoutGroupMap(t *testing.T) { + f := newFixture(t, testHashed, []string{shardSIDFor(0x10)}, nil) + sink := &natLocatorSink{} + f.g.natLocators = sink.set + if err := f.g.sweepWith(context.Background(), f.routes, nil, nil); err != nil { + t.Fatal(err) + } + if sink.calls != 1 || len(sink.sids) != 1 || !sink.sids[shardSIDFor(0x10)] { + t.Errorf("natLocators got %d calls with %v, want the configured shard", sink.calls, sink.sids) + } +} + // TestSweepHashed_ShardNode checks a node that runs an egress shard: its own // shard is a local member, not an unreachable one, while a remote shard whose // SID does not resolve is still unreachable. Neither is alive, so neither From a997ac932ce5232fe4008aa86da7e7ac741db296 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 25 Oct 2026 12:31:04 -0400 Subject: [PATCH 2/2] fix(cni): Fail closed when no shard SID is known yet Before the EgressShard watch has synced, the sweep handed traffic policy the seeded shard SIDs even when the seed was empty. An empty set marked the NAT locators complete, so a reply from an unknown shard was classified as private instead of being dropped. The sweep now calls the locator hook before the watch syncs only when the seed holds at least one SID. A node with no configured shard and no published group keeps traffic policy unsynced until the watch lists the shards. Once the watch has synced, the behavior is unchanged. Also reword the knownShardSIDs doc comment. Co-Authored-By: Claude Opus 5.5 --- internal/installer/egressgroup.go | 12 +++++++---- internal/installer/egressgroup_test.go | 29 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/internal/installer/egressgroup.go b/internal/installer/egressgroup.go index 8d3a607c5..cdc3ce445 100644 --- a/internal/installer/egressgroup.go +++ b/internal/installer/egressgroup.go @@ -76,7 +76,11 @@ type egressGroup struct { // classification: the configured shards, every member of a published // group, and the cluster's EgressShards once the watch has listed them. // The first two cover every path usid_egress can take, so classification - // works before the watch syncs, and while it cannot start. + // works before the watch syncs, and while it cannot start. Before the + // watch has synced it is called only when that seed holds at least one + // SID: an empty set would mark the locators complete, so a node with no + // configured shard and no published group keeps traffic policy + // unsynced and fails closed. natLocators func([]net.IP) pinDir string trigger chan struct{} @@ -359,7 +363,7 @@ func (g *egressGroup) sweepHashed( if sids, err := g.knownShardSIDs(groups, listed); err != nil { slog.Error("Failed to read the egress shard groups; traffic policy's NAT locators are not updated", "err", err) - } else { + } else if synced || len(sids) > 0 { g.natLocators(sids) } } @@ -949,8 +953,8 @@ func isUplink(ifindex int) bool { } // knownShardSIDs is every shard SID usid_egress can send this node's tenants -// to: the configured shards, every member of a published group, and shards, -// the cluster's EgressShards when the watch has listed them. It fails when a +// to: the configured shards, every member of a published group, and the +// cluster's EgressShards when the watch has listed them. It fails when a // group cannot be read, rather than report an incomplete set. func (g *egressGroup) knownShardSIDs( groups *egressroutemap.ShardGroupTable, shards []bgpv1alpha1.EgressShard, diff --git a/internal/installer/egressgroup_test.go b/internal/installer/egressgroup_test.go index ef58b5196..91871ce15 100644 --- a/internal/installer/egressgroup_test.go +++ b/internal/installer/egressgroup_test.go @@ -643,6 +643,35 @@ func TestSweepHashed_NATLocatorsWithoutWatch(t *testing.T) { } } +// TestSweepHashed_NATLocatorsEmptySeed checks that, with no configured shard +// and no published group, traffic policy's locators are left unsynced while +// the watch has not synced, so it keeps failing closed, and are set once the +// watch has synced. +func TestSweepHashed_NATLocatorsEmptySeed(t *testing.T) { + cases := map[string]struct { + lister egressShardLister + calls int + }{ + "WatchNotStarted": {nil, 0}, + "WatchUnsynced": {&fakeLister{synced: false}, 0}, + "WatchSyncedEmpty": {&fakeLister{synced: true}, 1}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + f := newFixture(t, testHashed, nil, nil) + if tc.lister != nil { + f.g.shards = tc.lister + } + sink := &natLocatorSink{} + f.g.natLocators = sink.set + _ = f.sweep(t) + if sink.calls != tc.calls { + t.Errorf("natLocators called %d times, want %d", sink.calls, tc.calls) + } + }) + } +} + // TestSweep_NATLocatorsWithoutGroupMap checks that a datapath without the // shard group map, which can only route to the configured shards, hands // traffic policy those shards.