aboutsummaryrefslogtreecommitdiff
path: root/utils/networks/networks.go
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-10-29 21:17:53 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-10-29 21:17:53 +0100
commitaf7c5d500b76a6f2db790a1a8e0480f370da81ce (patch)
treee9bb68f8d4eec1f0fa44ba2fb64826d0da913010 /utils/networks/networks.go
parent0f51c7d542227f1f4c9895db36007b9d95963d99 (diff)
downloadinfra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.tar.gz
infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.tar.bz2
infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.tar.xz
infra-af7c5d500b76a6f2db790a1a8e0480f370da81ce.zip
utils: networks
Diffstat (limited to 'utils/networks/networks.go')
-rw-r--r--utils/networks/networks.go132
1 files changed, 9 insertions, 123 deletions
diff --git a/utils/networks/networks.go b/utils/networks/networks.go
index 328f006..f667f05 100644
--- a/utils/networks/networks.go
+++ b/utils/networks/networks.go
@@ -1,13 +1,10 @@
package networks
import (
- "cmp"
"fmt"
- "log"
- "net/netip"
"os"
"slices"
- "utils/ipam/yaml_model"
+ Ipam "utils/ipam"
)
func RunNetworks() error {
@@ -17,13 +14,13 @@ func RunNetworks() error {
return err
}
- ipam, err := loadIpam(bs)
+ ipam, err := Ipam.LoadIpam(bs)
if err != nil {
return err
}
- networks := slices.SortedFunc(slices.Values(ipam.networks), CompareNetwork)
- roots := slices.SortedFunc(slices.Values(ipam.FindRoots()), CompareNetwork)
+ networks := slices.SortedFunc(slices.Values(ipam.Networks), Ipam.CompareNetwork)
+ roots := slices.SortedFunc(slices.Values(ipam.FindRoots()), Ipam.CompareNetwork)
fmt.Printf("digraph {\n")
//fmt.Printf(" layout=dot\n")
@@ -45,21 +42,21 @@ func RunNetworks() error {
fmt.Printf("\n")
fmt.Printf(" # Nodes\n")
for _, n := range networks {
- fmt.Printf(" %s [ label = \"%s\\n%s\"];\n", n.name, n.name, n.prefix)
+ fmt.Printf(" %s [ label = \"%s\\n%s\"];\n", n.Name, n.Name, n.Prefix)
}
fmt.Printf("\n")
fmt.Printf(" # Roots\n")
for _, n := range roots {
- fmt.Printf(" %s [ root = true ];\n", n.name)
+ fmt.Printf(" %s [ root = true ];\n", n.Name)
}
fmt.Printf("\n")
fmt.Printf(" # Relationships\n")
for _, n := range networks {
- for _, c := range ipam.networks {
- if c.parent == n {
- fmt.Printf(" %s -> %s", n.name, c.name)
+ for _, c := range ipam.Networks {
+ if c.Parent == n {
+ fmt.Printf(" %s -> %s", n.Name, c.Name)
fmt.Printf(";\n")
}
}
@@ -68,114 +65,3 @@ func RunNetworks() error {
return nil
}
-
-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
-}