aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-10-29 21:13:43 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-10-29 21:13:43 +0100
commit0f51c7d542227f1f4c9895db36007b9d95963d99 (patch)
treea7d57e15a783b5dd3180e8923f88cfb0a8bc8bab /utils
parent19822dde9f01d54776233fe153b6ea5d5b21c454 (diff)
downloadinfra-0f51c7d542227f1f4c9895db36007b9d95963d99.tar.gz
infra-0f51c7d542227f1f4c9895db36007b9d95963d99.tar.bz2
infra-0f51c7d542227f1f4c9895db36007b9d95963d99.tar.xz
infra-0f51c7d542227f1f4c9895db36007b9d95963d99.zip
utils: networks
Diffstat (limited to 'utils')
-rw-r--r--utils/Makefile5
-rw-r--r--utils/ipam/yaml_model/yaml_model.go (renamed from utils/yaml_ipam/yaml_ipam.go)14
-rw-r--r--utils/main.go178
-rw-r--r--utils/networks/networks.go181
-rw-r--r--utils/routers/routers.go4
5 files changed, 204 insertions, 178 deletions
diff --git a/utils/Makefile b/utils/Makefile
new file mode 100644
index 0000000..77e67a3
--- /dev/null
+++ b/utils/Makefile
@@ -0,0 +1,5 @@
+all: utils
+ go run main.go > ../ansible/group_vars/all/ipam.tmp.dot && dot -Tpng ../ansible/group_vars/all/ipam.tmp.dot > ../ansible/group_vars/all/ipam.tmp.png
+
+utils:
+ go build
diff --git a/utils/yaml_ipam/yaml_ipam.go b/utils/ipam/yaml_model/yaml_model.go
index ff65155..6e5b0f8 100644
--- a/utils/yaml_ipam/yaml_ipam.go
+++ b/utils/ipam/yaml_model/yaml_model.go
@@ -1,9 +1,10 @@
-package yaml_ipam
+package yaml_model
import "gopkg.in/yaml.v3"
type Ipam struct {
- Ipam6 Ipam6 `yaml:"ipam6"`
+ Ipam6 Ipam6 `yaml:"ipam6,omitempty"`
+ Routers []Router `yaml:"routers,omitempty"`
}
type Ipam6 struct {
@@ -15,6 +16,15 @@ type Network6Yaml struct {
Hosts map[string]string `yaml:"hosts"`
}
+type Router struct {
+ As string `yaml:"as,omitempty"`
+ Peer []Peer `yaml:"peer,omitempty"`
+}
+
+type Peer struct {
+ Name string `yaml:"name"`
+}
+
func Parse(bs []byte) (Ipam, error) {
var ipam Ipam
err := yaml.Unmarshal(bs, &ipam)
diff --git a/utils/main.go b/utils/main.go
index 42adbd6..af3a4a7 100644
--- a/utils/main.go
+++ b/utils/main.go
@@ -1,189 +1,15 @@
package main
import (
- "cmp"
"fmt"
- "log"
- "net/netip"
"os"
- "slices"
- "utils/yaml_ipam"
+ "utils/networks"
)
func main() {
- err := work()
+ err := networks.RunNetworks()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
-
-func work() error {
- ipamYaml := "../ansible/group_vars/all/ipam.yml"
- bs, err := os.ReadFile(ipamYaml)
- if err != nil {
- return err
- }
-
- ipam, err := loadIpam(bs)
- if err != nil {
- return err
- }
-
- networks := slices.SortedFunc(slices.Values(ipam.networks), CompareNetwork)
- roots := slices.SortedFunc(slices.Values(ipam.FindRoots()), CompareNetwork)
-
- fmt.Printf("digraph {\n")
- //fmt.Printf(" layout=dot\n")
- //fmt.Printf(" layout=twopi\n")
- fmt.Printf(" layout=fdp\n")
- //fmt.Printf(" layout=circo\n")
- //fmt.Printf(" layout=neato\n")
- //fmt.Printf(" ranksep=3\n")
- //fmt.Printf(" ratio=auto\n")
- //fmt.Printf(" rankdir=LR\n")
- //fmt.Printf(" rankdir=RL\n")
- fmt.Printf(" rankdir=TB\n")
- //fmt.Printf(" sep=3\n")
- //fmt.Printf(" overlap_scaling=-10\n")
- fmt.Printf("node [len=10];")
- fmt.Printf("node [shape=box];")
- fmt.Printf("overlap=false")
-
- 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("\n")
- fmt.Printf(" # Roots\n")
- for _, n := range roots {
- 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)
- fmt.Printf(";\n")
- }
- }
- }
- fmt.Printf("}\n")
-
- return nil
-}
-
-func loadIpam(bs []byte) (*Ipam, error) {
- parsed, err := yaml_ipam.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_ipam.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
-}
diff --git a/utils/networks/networks.go b/utils/networks/networks.go
new file mode 100644
index 0000000..328f006
--- /dev/null
+++ b/utils/networks/networks.go
@@ -0,0 +1,181 @@
+package networks
+
+import (
+ "cmp"
+ "fmt"
+ "log"
+ "net/netip"
+ "os"
+ "slices"
+ "utils/ipam/yaml_model"
+)
+
+func RunNetworks() error {
+ ipamYaml := "../ansible/group_vars/all/ipam.yml"
+ bs, err := os.ReadFile(ipamYaml)
+ if err != nil {
+ return err
+ }
+
+ ipam, err := loadIpam(bs)
+ if err != nil {
+ return err
+ }
+
+ networks := slices.SortedFunc(slices.Values(ipam.networks), CompareNetwork)
+ roots := slices.SortedFunc(slices.Values(ipam.FindRoots()), CompareNetwork)
+
+ fmt.Printf("digraph {\n")
+ //fmt.Printf(" layout=dot\n")
+ //fmt.Printf(" layout=twopi\n")
+ fmt.Printf(" layout=fdp\n")
+ //fmt.Printf(" layout=circo\n")
+ //fmt.Printf(" layout=neato\n")
+ //fmt.Printf(" ranksep=3\n")
+ //fmt.Printf(" ratio=auto\n")
+ //fmt.Printf(" rankdir=LR\n")
+ //fmt.Printf(" rankdir=RL\n")
+ fmt.Printf(" rankdir=TB\n")
+ //fmt.Printf(" sep=3\n")
+ //fmt.Printf(" overlap_scaling=-10\n")
+ fmt.Printf("node [len=10];")
+ fmt.Printf("node [shape=box];")
+ fmt.Printf("overlap=false")
+
+ 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("\n")
+ fmt.Printf(" # Roots\n")
+ for _, n := range roots {
+ 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)
+ fmt.Printf(";\n")
+ }
+ }
+ }
+ fmt.Printf("}\n")
+
+ 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
+}
diff --git a/utils/routers/routers.go b/utils/routers/routers.go
new file mode 100644
index 0000000..0afb355
--- /dev/null
+++ b/utils/routers/routers.go
@@ -0,0 +1,4 @@
+package routers
+
+type Routers struct {
+}