f-42: api: ressource /nat-gateways, état, métrique et watchdog #42

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-29 20:25:52 +01:00
commit 2737b61bc4
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
16 changed files with 841 additions and 11 deletions

View file

@ -232,6 +232,20 @@ func expectedExports(db *badger.DB, vpcName string) (expected, tolerated map[str
classify(st, ip.String()+"/32")
}
}
gateways, err := kv.ListByPrefix(db, prefixNATGW)
if err != nil {
return nil, nil, fmt.Errorf("watchdog: listing nat gateways: %w", err)
}
for key, value := range gateways {
name, ok := strings.CutSuffix(strings.TrimPrefix(key, prefixNATGW), "/vpc")
if !ok || strings.Contains(name, "/") || value != vpcName {
continue
}
if st, err := state.Get(db, prefixNATGW+name); err == nil {
classify(st, "0.0.0.0/0")
}
}
return expected, tolerated, nil
}

View file

@ -278,3 +278,41 @@ func TestCheckL3VPCsReportsAVRFWithoutUnreachableDefault(t *testing.T) {
t.Errorf("notifications %q", notified(r))
}
}
func TestCheckL3VPCsExpectsTheDefaultRouteOfARunningNATGateway(t *testing.T) {
cases := []struct {
label string
gateway state.State
vpc string
exported bool
want []string
}{
{"passerelle running, route exportée", state.Running, "vp-s7", true, nil},
{"passerelle running, route absente", state.Running, "vp-s7", false,
[]string{"vpc vp-s7: route 0.0.0.0/0 not exported in vrf-s7"}},
{"passerelle en création, route exportée", state.Creating, "vp-s7", true, nil},
{"passerelle en erreur, route absente", state.Error, "vp-s7", false, nil},
{"passerelle d'un autre VPC, route exportée", state.Running, "vp-s70", true,
[]string{"vpc vp-s7: route 0.0.0.0/0 exported in vrf-s7 without a running owner: its traffic is black-holed"}},
}
for _, c := range cases {
db := newTestDB(t)
seedResource(t, db, prefixVPC, "vp-s7", state.Running)
seedKV(t, db, "vpc/vp-s7/cidr", "10.241.0.0/16")
seedKV(t, db, "vpc/vp-s7/l3vni", "2700")
seedResource(t, db, prefixNATGW, "ng-1", c.gateway)
seedKV(t, db, "nat-gateway/ng-1/vpc", c.vpc)
p := seedRoutedVPC(t)
p.routes = map[uint32][]string{2700: nil}
if c.exported {
p.routes[2700] = []string{"0.0.0.0/0"}
}
r := &recorder{}
if err := CheckL3VPCs(db, bgpCfg(), p, r); err != nil {
t.Fatal(err)
}
if got := notified(r); !reflect.DeepEqual(got, c.want) {
t.Errorf("%s :\n%q\nattendu :\n%q", c.label, got, c.want)
}
}
}

View file

@ -14,6 +14,7 @@ const (
prefixVPC = "vpc/"
prefixSubnet = "subnet/"
prefixVM = "vm/"
prefixNATGW = "nat-gateway/"
kindVPC = "vpc"
kindSubnet = "subnet"