diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-10-29 21:17:53 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-10-29 21:17:53 +0100 |
commit | af7c5d500b76a6f2db790a1a8e0480f370da81ce (patch) | |
tree | e9bb68f8d4eec1f0fa44ba2fb64826d0da913010 /utils/ipam | |
parent | 0f51c7d542227f1f4c9895db36007b9d95963d99 (diff) | |
download | infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.tar.gz infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.tar.bz2 infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.tar.xz infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.zip |
utils: networks
Diffstat (limited to 'utils/ipam')
-rw-r--r-- | utils/ipam/ipam.go | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/utils/ipam/ipam.go b/utils/ipam/ipam.go new file mode 100644 index 0000000..faec6e7 --- /dev/null +++ b/utils/ipam/ipam.go @@ -0,0 +1,120 @@ +package ipam + +import ( + "cmp" + "fmt" + "log" + "net/netip" + "utils/ipam/yaml_model" +) + +func LoadIpam(bs []byte) (*Ipam, error) { + parsed, err := yaml_model.Parse(bs) + if err != nil { + return nil, err + } + + ipam_, err := processNetworks(parsed.Ipam6.Networks) + if err != nil { + return nil, err + } + + ipam_.ResolveParents() + + return ipam_, nil +} + +type Ipam struct { + Networks []*Network +} + +func contains(a, b netip.Prefix) bool { + return b.Bits() > a.Bits() && b.Overlaps(a) +} + +func (ipam *Ipam) ResolveParents() { + for _, n := range ipam.Networks { + log.Printf("network %s/%s", n.Name, n.Prefix) + for _, p := range ipam.Networks { + if n == p { + continue + } + log.Printf(" candidate %s/%s", p.Name, p.Prefix) + if contains(p.Prefix, n.Prefix) { + if n.Parent == nil { + log.Printf(" found parent %s/%s", p.Name, p.Prefix) + n.Parent = p + } else { + if n.Parent.Prefix.Bits() < p.Prefix.Bits() { + log.Printf(" found better parent %s/%s", p.Name, p.Prefix) + n.Parent = p + } + } + } + } + } + + return +} + +func (ipam *Ipam) FindRoots() []*Network { + var ns []*Network + + for _, n := range ipam.Networks { + if n.Parent == nil { + ns = append(ns, n) + } + } + + return ns +} + +type Network struct { + Name string + Parent *Network + Prefix netip.Prefix + hosts []networkHost +} + +func CompareNetwork(a, b *Network) int { + return cmp.Compare(a.Name, b.Name) +} + +func (n *Network) Compare(other *Network) int { + return cmp.Compare(n.Name, other.Name) +} + +type networkHost struct { + name string + address netip.Addr +} + +func processNetworks(networks map[string]yaml_model.Network6Yaml) (*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) + } + log.Printf("prefix: %s", prefix.String()) + + n := Network{Name: name, Prefix: prefix} + 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) + } + + n.hosts = append(n.hosts, networkHost{ + name: hostname, + address: addr, + }) + } + + ns = append(ns, &n) + } + + return &Ipam{Networks: ns}, nil +} |