aboutsummaryrefslogtreecommitdiff
path: root/utils/ipam
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-10-29 21:58:47 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-10-29 21:58:47 +0100
commitab2409752fc8992a172e8caffa64fa19beafbcb7 (patch)
treebec8f72367bba8f584893f76219b034943039247 /utils/ipam
parentaf7c5d500b76a6f2db790a1a8e0480f370da81ce (diff)
downloadinfra-ab2409752fc8992a172e8caffa64fa19beafbcb7.tar.gz
infra-ab2409752fc8992a172e8caffa64fa19beafbcb7.tar.bz2
infra-ab2409752fc8992a172e8caffa64fa19beafbcb7.tar.xz
infra-ab2409752fc8992a172e8caffa64fa19beafbcb7.zip
utils: routers
Diffstat (limited to 'utils/ipam')
-rw-r--r--utils/ipam/ipam.go77
-rw-r--r--utils/ipam/yaml_model/yaml_model.go10
2 files changed, 75 insertions, 12 deletions
diff --git a/utils/ipam/ipam.go b/utils/ipam/ipam.go
index faec6e7..d61fa3e 100644
--- a/utils/ipam/ipam.go
+++ b/utils/ipam/ipam.go
@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/netip"
+ "slices"
"utils/ipam/yaml_model"
)
@@ -14,18 +15,25 @@ func LoadIpam(bs []byte) (*Ipam, error) {
return nil, err
}
- ipam_, err := processNetworks(parsed.Ipam6.Networks)
+ ipam := Ipam{}
+ err = processNetworks(parsed.Ipam6.Networks, &ipam)
if err != nil {
return nil, err
}
- ipam_.ResolveParents()
+ err = processRouters(parsed.Routers, &ipam)
+ if err != nil {
+ return nil, err
+ }
+
+ ipam.ResolveParents()
- return ipam_, nil
+ return &ipam, nil
}
type Ipam struct {
Networks []*Network
+ Routers []*Router
}
func contains(a, b netip.Prefix) bool {
@@ -89,14 +97,14 @@ type networkHost struct {
address netip.Addr
}
-func processNetworks(networks map[string]yaml_model.Network6Yaml) (*Ipam, error) {
+func processNetworks(networks map[string]yaml_model.Network6Yaml, ipam *Ipam) error {
var ns []*Network
for name, net := range networks {
log.Printf("Processing net %v\n", name)
prefix, err := netip.ParsePrefix(net.Range)
if err != nil {
- return nil, fmt.Errorf("error parsing net range: %v", err)
+ return fmt.Errorf("error parsing net range: %v", err)
}
log.Printf("prefix: %s", prefix.String())
@@ -104,7 +112,7 @@ func processNetworks(networks map[string]yaml_model.Network6Yaml) (*Ipam, error)
for hostname, a := range net.Hosts {
addr, err := netip.ParseAddr(a)
if err != nil {
- return nil, fmt.Errorf("network: %s, unable to parse host address %s", n.Name, a)
+ return fmt.Errorf("network: %s, unable to parse host address %s", n.Name, a)
}
n.hosts = append(n.hosts, networkHost{
@@ -116,5 +124,60 @@ func processNetworks(networks map[string]yaml_model.Network6Yaml) (*Ipam, error)
ns = append(ns, &n)
}
- return &Ipam{Networks: ns}, nil
+ ipam.Networks = ns
+ return nil
+}
+
+type Router struct {
+ Name string
+ As string
+ Peers []Peer
+}
+
+func (r *Router) Compare(other *Router) int {
+ return cmp.Compare(r.Name, other.Name)
+}
+
+type Peer struct {
+ Name string
+ /// Router is set if this is a known peer
+ Router *Router
+
+ As string
+}
+
+func processRouters(routers map[string]yaml_model.Router, ipam *Ipam) error {
+ var rs []*Router
+ rsIndex := make(map[string]*Router)
+ for name, router := range routers {
+ r := &Router{
+ Name: name,
+ As: router.As,
+ }
+
+ rs = append(rs, r)
+ rsIndex[name] = r
+ }
+
+ for _, r := range rs {
+ router := routers[r.Name]
+
+ for name, peer := range router.Peers {
+ remote := rsIndex[name]
+
+ r.Peers = append(r.Peers, Peer{
+ Name: name,
+ Router: remote,
+ As: peer.As,
+ })
+ }
+ }
+
+ compare := func(a *Router, b *Router) int {
+ return a.Compare(b)
+ }
+
+ ipam.Routers = slices.SortedStableFunc(slices.Values(rs), compare)
+
+ return nil
}
diff --git a/utils/ipam/yaml_model/yaml_model.go b/utils/ipam/yaml_model/yaml_model.go
index 6e5b0f8..4109e12 100644
--- a/utils/ipam/yaml_model/yaml_model.go
+++ b/utils/ipam/yaml_model/yaml_model.go
@@ -3,8 +3,8 @@ package yaml_model
import "gopkg.in/yaml.v3"
type Ipam struct {
- Ipam6 Ipam6 `yaml:"ipam6,omitempty"`
- Routers []Router `yaml:"routers,omitempty"`
+ Ipam6 Ipam6 `yaml:"ipam6,omitempty"`
+ Routers map[string]Router `yaml:"routers,omitempty"`
}
type Ipam6 struct {
@@ -17,12 +17,12 @@ type Network6Yaml struct {
}
type Router struct {
- As string `yaml:"as,omitempty"`
- Peer []Peer `yaml:"peer,omitempty"`
+ As string `yaml:"as,omitempty"`
+ Peers map[string]Peer `yaml:"peers,omitempty"`
}
type Peer struct {
- Name string `yaml:"name"`
+ As string `yaml:"as"`
}
func Parse(bs []byte) (Ipam, error) {