diff --git a/internal/installer/egressgroup.go b/internal/installer/egressgroup.go index 6cf5e8dc4..cdc3ce445 100644 --- a/internal/installer/egressgroup.go +++ b/internal/installer/egressgroup.go @@ -71,9 +71,16 @@ 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. 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{} @@ -236,6 +243,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 +355,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 if synced || len(sids) > 0 { + g.natLocators(sids) + } } refsKnown := g.refs != nil @@ -933,6 +952,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 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..91871ce15 100644 --- a/internal/installer/egressgroup_test.go +++ b/internal/installer/egressgroup_test.go @@ -574,6 +574,119 @@ 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) + } + }) + } +} + +// 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. +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