External IPAM authority: NetBox driver, ownership, reconcile
CI / agent (push) Successful in 2s
CI / site (push) Successful in 2s
CI / go (push) Successful in 35s

Every address Rollcall hands out is registered in the configured authority
first; addresses others hold there are skipped. Rollcall's records carry the
tag `rollcall` and the machine ID in the `rollcall_machine` custom field;
only those are changed or deleted. Releases trigger a reconcile that also
runs periodically. dns_name per address feeds the site's DNS sync. Delegated
prefixes can be listed and imported as subnets. `rollcall ipam`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-28 09:35:21 +01:00
1 parent c8465c235a
commit 2706fd65e7
14 files changed
+1068 -6

No files matched your search

+5
View File
@@ -48,6 +48,11 @@ publish a tag that has no section here.
web UI and console. Lab Redfish emulator with iDRAC/iLO/generic profiles.
- IPv6: subnets, IPAM, dual-stack default layout, per-family default routes, dnsmasq RA/DHCPv6,
AAAA host records, IPv6 listen addresses and URLs, IPv6 in the commissioning inventory.
- External IPAM authority (NetBox driver): allocation registers each address in NetBox first and
skips addresses others hold there; ownership by tag + `rollcall_machine` custom field; releases
and a periodic reconcile create, fix and delete only Rollcall-owned records; `dns_name` per address
for DNS sync; delegated prefixes (`rollcall ipam prefixes|import`); `rollcall ipam [sync -dry]`,
`/api/v1/ipam`.
- `rollcall version`; CD-built binary carries `git describe`.
- Release notes from `CHANGELOG.md`.
+48
View File
@@ -480,6 +480,54 @@ Verified in the lab: Debian 13 (with a bond and VLAN), Rocky 9 and AlmaLinux 9
dual-stack on two segments, AAAA lookups through dnsmasq, the admin CLI over
`https://[fdc3:...]`.
## External IPAM (NetBox; Infoblox-style)
Rollcall works on its own, but at a site with an established network plan the
address authority is somewhere else. With `ipam` in `config.json` every
address Rollcall hands out is registered there first:
```json
"ipam": {
"provider": "netbox",
"url": "https://netbox.int.example.com",
"token_file": "/etc/rollcall/netbox.token",
"ca_file": "/etc/rollcall/netbox-ca.pem",
"dns_zone": "int.example.com",
"sync_interval": "5m"
}
```
- **Allocation asks the authority.** An address that exists in NetBox and is
not Rollcall's is skipped and the next candidate is tried, so Rollcall never
collides with a printer, a VIP or another team's reservation. If NetBox is
unreachable, allocation fails instead of guessing.
- **Ownership is explicit.** Rollcall's records carry the tag `rollcall` and the
machine ID in the custom field `rollcall_machine` (create both in NetBox;
on Infoblox these are an extensible attribute). Records without both are
never changed or deleted, only reported.
- **Releases reconcile.** Releasing, unassigning or deleting a machine frees
addresses locally and triggers a reconcile, which also runs every
`sync_interval`: missing records are created, names and owners fixed, and
Rollcall-owned records that are no longer in use deleted.
- **DNS follows the record.** `dns_name` is `<host>.<zone>` for a machine's
provisioning/mgmt address and `<host>-<iface>.<zone>` elsewhere (a subnet's
`domain` overrides the zone). A NetBox-to-DNS sync (PowerDNS here, a host
record on Infoblox) publishes it; Rollcall writes one object per address.
- **Prefixes are delegated in NetBox.** Tag a prefix `rollcall`;
`rollcall ipam prefixes` lists them and `rollcall ipam import` creates the
missing subnets (VLAN and rack are then set as usual).
```
rollcall ipam # provider, last reconcile
rollcall ipam sync -dry # what would change
rollcall ipam sync
rollcall ipam prefixes | import
```
The provider is an interface (reserve, owned records, update, delete,
delegated prefixes); the NetBox driver is the first. An Infoblox WAPI driver
maps them to `record:host`/`fixedaddress` with extensible attributes.
## Lab
`lab/` documents a Proxmox SDN test setup: an isolated vnet, a PXE server VM
+1 -1
View File
@@ -269,7 +269,7 @@ func runClient(cmd string, args []string) error {
return c.locationCmd(args)
case "bmc":
return c.bmcCmd(args)
case "fabric", "vlan", "subnet", "range", "ip", "layout", "net", "controllers", "controller", "dhcp":
case "fabric", "vlan", "subnet", "range", "ip", "layout", "net", "controllers", "controller", "dhcp", "ipam":
return c.netCmd(cmd, args)
default:
return c.statelessCmd(cmd, args)
+3
View File
@@ -44,6 +44,9 @@ type Config struct {
TLSListen string `json:"tls_listen"` // HTTPS listener, default ":443"; "off" disables
TLSNames []string `json:"tls_names"` // extra names/addresses for the region certificate
AdminHTTP string `json:"admin_http"` // admin API over plain HTTP: allow (default) | loopback | deny
// external IPAM authority (NetBox; Infoblox at sites that run it)
IPAM IPAMConfig `json:"ipam"`
}
// BMCHint maps a machine MAC to a known BMC (virtual lab nodes).
+1
View File
@@ -603,6 +603,7 @@ func (a *App) afterJob(ctx context.Context, m *Machine, j *Job, state, errMsg st
_ = a.store.AddEvent(ctx, m.ID, "deployed", fmt.Sprintf("job=%s os=%s/%s disk=%s", j.ID, sp.OS, sp.Version, sp.Disk))
case state == "done" && j.Kind == "release":
_ = a.store.SetReleased(ctx, m.ID)
a.kickIPAM()
m, _ = a.store.Get(ctx, m.ID)
_ = a.writePXE(m)
_ = a.store.AddEvent(ctx, m.ID, "released", fmt.Sprintf("job=%s disk=%s wipe=%s", j.ID, sp.Disk, sp.Wipe))
+640
View File
@@ -0,0 +1,640 @@
package main
// External IPAM: an authority outside Rollcall (NetBox, or Infoblox where a
// site runs it) in which every address Rollcall hands out is registered.
//
// - Allocation asks the authority first: an address someone else already
// has there is skipped, so Rollcall never collides with the site's plan.
// - Releases are local; the reconcile loop then makes the authority match
// Rollcall's table: it creates what is missing, fixes names, and deletes
// what Rollcall owns there but no longer uses.
// - Ownership is explicit: a tag plus the machine ID in a custom field
// (an extensible attribute on Infoblox). Records without both are never
// changed or deleted, only reported as conflicts.
// - DNS follows from the record's dns_name (NetBox + PowerDNS sync, or an
// Infoblox host record), so Rollcall writes one object per address.
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/netip"
"net/url"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
)
type IPAMConfig struct {
Provider string `json:"provider"` // "" = built-in only | netbox
URL string `json:"url"` // https://netbox.example
TokenFile string `json:"token_file"` // API token (NetBox v2 "nbt_..." or v1)
CAFile string `json:"ca_file"` // CA that signed the server certificate ("" = system roots)
DNSZone string `json:"dns_zone"` // dns_name = <host>.<zone> unless the subnet has a domain
Tag string `json:"tag"` // ownership tag, default "rollcall"
MachineField string `json:"machine_field"` // custom field holding the machine ID, default "rollcall_machine"
SyncInterval string `json:"sync_interval"` // reconcile period, default 5m
}
// extAddr is one address as the authority holds it.
type extAddr struct {
ID string `json:"id,omitempty"`
IP string `json:"ip"`
Bits int `json:"bits"`
DNSName string `json:"dns_name,omitempty"`
Machine string `json:"machine,omitempty"` // owner mark; "-" = Rollcall reservation without a machine
Status string `json:"status"` // active | reserved
Desc string `json:"description,omitempty"`
}
type extPrefix struct {
Prefix string `json:"prefix"`
Desc string `json:"description,omitempty"`
VID int `json:"vid,omitempty"`
Site string `json:"site,omitempty"`
Local bool `json:"local"` // a Rollcall subnet exists for it
Comment string `json:"comment,omitempty"`
}
type ipamProvider interface {
Name() string
// Reserve registers a; taken is true when the address exists there and
// is not Rollcall's. An existing Rollcall-owned record is updated.
Reserve(ctx context.Context, a extAddr) (taken bool, err error)
Owned(ctx context.Context) ([]extAddr, error)
Update(ctx context.Context, a extAddr) error
Delete(ctx context.Context, a extAddr) error
Prefixes(ctx context.Context) ([]extPrefix, error)
}
type ipamReport struct {
Provider string `json:"provider"`
At time.Time `json:"at"`
DryRun bool `json:"dry_run,omitempty"`
Created []string `json:"created"`
Updated []string `json:"updated"`
Deleted []string `json:"deleted"`
Conflicts []string `json:"conflicts"`
Error string `json:"error,omitempty"`
}
type ipamState struct {
mu sync.Mutex // one reconcile at a time
kick chan struct{}
last ipamReport
}
func newIPAMProvider(c IPAMConfig) (ipamProvider, error) {
switch c.Provider {
case "":
return nil, nil
case "netbox":
return newNetBox(c)
default:
return nil, fmt.Errorf("ipam provider %q: unknown (netbox)", c.Provider)
}
}
func (c IPAMConfig) tag() string { return orDefault(c.Tag, "rollcall") }
func (c IPAMConfig) field() string { return orDefault(c.MachineField, "rollcall_machine") }
func orDefault(s, d string) string {
if s == "" {
return d
}
return s
}
// ---------------------------------------------------------------- App side
var dnsLabel = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$`)
// extFor maps a Rollcall address to the record the authority should hold.
func (a *App) extFor(x IPAddr, sn *Subnet) extAddr {
bits := 32
if p, err := netip.ParsePrefix(x.Subnet); err == nil {
bits = p.Bits()
} else if ad, ok := parseAddr(x.IP); ok && ad.Is6() {
bits = 128
}
e := extAddr{IP: x.IP, Bits: bits, Status: "active", Machine: x.MachineID}
if x.Kind == "reserved" {
e.Status = "reserved"
}
if e.Machine == "" {
e.Machine = "-"
}
desc := "rollcall"
if x.MachineID != "" {
desc += " " + x.MachineID
}
if x.Iface != "" {
desc += " " + x.Iface
}
if x.Kind == "reserved" && x.Hostname != "" {
desc += ": " + x.Hostname
}
e.Desc = desc
e.DNSName = a.extDNSName(x, sn)
return e
}
// extDNSName: the machine's main address gets <host>.<zone>; addresses on
// other subnets (storage, IB, a tagged VLAN) get <host>-<iface>.<zone>.
func (a *App) extDNSName(x IPAddr, sn *Subnet) string {
zone := a.cfg.IPAM.DNSZone
if sn != nil && sn.Domain != "" {
zone = sn.Domain
}
host := strings.ToLower(strings.TrimSuffix(x.Hostname, "."))
if zone == "" || host == "" {
return ""
}
zone = strings.TrimSuffix(strings.ToLower(zone), ".")
if strings.HasSuffix(host, "."+zone) {
return host
}
if !dnsLabel.MatchString(host) {
return "" // a free-text reservation name ("core switch") stays in the description
}
main := sn == nil || x.Iface == "" || sn.Purpose == "" || sn.Purpose == "provisioning" || sn.Purpose == "mgmt"
if !main {
suffix := strings.Trim(regexp.MustCompile(`[^a-z0-9]+`).ReplaceAllString(strings.ToLower(x.Iface), "-"), "-")
if suffix != "" {
host += "-" + suffix
}
}
return host + "." + zone
}
// claimIP records an address in the authority (when one is configured) and
// then locally; false when it is taken in either.
func (a *App) claimIP(ctx context.Context, x IPAddr) (bool, error) {
if a.ipam != nil {
sn := subnetByRef(a.subnetsSnapshot(), x.Subnet)
taken, err := a.ipam.Reserve(ctx, a.extFor(x, sn))
if err != nil {
return false, fmt.Errorf("%s: %w", a.ipam.Name(), err)
}
if taken {
return false, nil
}
}
ok, err := a.store.ClaimIP(ctx, x)
if a.ipam != nil && (err != nil || !ok) {
a.kickIPAM() // the record we just made is reconciled away or re-owned
}
return ok, err
}
func (a *App) kickIPAM() {
if a.ipamSt == nil {
return
}
select {
case a.ipamSt.kick <- struct{}{}:
default:
}
}
func (a *App) startIPAM() error {
p, err := newIPAMProvider(a.cfg.IPAM)
if err != nil || p == nil {
return err
}
a.ipam = p
a.ipamSt = &ipamState{kick: make(chan struct{}, 1)}
every := 5 * time.Minute
if d, err := time.ParseDuration(a.cfg.IPAM.SyncInterval); err == nil && d >= 10*time.Second {
every = d
}
go func() {
a.kickIPAM() // first pass right after start
for {
select {
case <-a.ipamSt.kick:
case <-time.After(every):
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
rep := a.reconcileIPAM(ctx, false)
cancel()
if rep.Error != "" {
log.Printf("ipam %s: %s", p.Name(), rep.Error)
} else if n := len(rep.Created) + len(rep.Updated) + len(rep.Deleted) + len(rep.Conflicts); n > 0 {
log.Printf("ipam %s: created %d updated %d deleted %d conflicts %d %v", p.Name(),
len(rep.Created), len(rep.Updated), len(rep.Deleted), len(rep.Conflicts), rep.Conflicts)
}
}
}()
log.Printf("ipam: %s at %s (zone %q, owner tag %q)", p.Name(), a.cfg.IPAM.URL, a.cfg.IPAM.DNSZone, a.cfg.IPAM.tag())
return nil
}
// reconcileIPAM makes the authority match Rollcall's address table.
func (a *App) reconcileIPAM(ctx context.Context, dry bool) ipamReport {
rep := ipamReport{At: time.Now().UTC(), DryRun: dry, Created: []string{}, Updated: []string{}, Deleted: []string{}, Conflicts: []string{}}
if a.ipam == nil {
rep.Error = "no external IPAM configured (ipam.provider in config.json)"
return rep
}
rep.Provider = a.ipam.Name()
a.ipamSt.mu.Lock()
defer a.ipamSt.mu.Unlock()
defer func() {
if !dry {
a.ipamSt.last = rep
}
}()
local, err := a.store.IPs(ctx, "", "")
if err != nil {
rep.Error = err.Error()
return rep
}
a.reconcileAddrs(ctx, local, &rep)
return rep
}
// reconcileAddrs is the reconcile proper, over a given address table.
func (a *App) reconcileAddrs(ctx context.Context, local []IPAddr, rep *ipamReport) {
dry := rep.DryRun
owned, err := a.ipam.Owned(ctx)
if err != nil {
rep.Error = err.Error()
return
}
have := map[string]extAddr{}
for _, o := range owned {
have[o.IP] = o
}
subnets := a.subnetsSnapshot()
for _, x := range local {
want := a.extFor(x, subnetByRef(subnets, x.Subnet))
if o, ok := have[x.IP]; ok {
delete(have, x.IP)
if o.DNSName != want.DNSName || o.Machine != want.Machine || o.Status != want.Status || o.Bits != want.Bits || o.Desc != want.Desc {
want.ID = o.ID
rep.Updated = append(rep.Updated, x.IP)
if !dry {
if err := a.ipam.Update(ctx, want); err != nil {
rep.Error = fmt.Sprintf("update %s: %v", x.IP, err)
return
}
}
}
continue
}
if dry {
rep.Created = append(rep.Created, x.IP)
continue
}
taken, err := a.ipam.Reserve(ctx, want)
if err != nil {
rep.Error = fmt.Sprintf("create %s: %v", x.IP, err)
return
}
if taken {
rep.Conflicts = append(rep.Conflicts, x.IP+" exists there and is not Rollcall's")
} else {
rep.Created = append(rep.Created, x.IP)
}
}
rest := make([]extAddr, 0, len(have))
for _, o := range have {
rest = append(rest, o)
}
sort.Slice(rest, func(i, j int) bool { return ipLess(rest[i].IP, rest[j].IP) })
for _, o := range rest {
rep.Deleted = append(rep.Deleted, o.IP)
if !dry {
if err := a.ipam.Delete(ctx, o); err != nil {
rep.Error = fmt.Sprintf("delete %s: %v", o.IP, err)
return
}
}
}
}
func (a *App) handleIPAM(w http.ResponseWriter, r *http.Request) {
if a.ipam == nil {
writeJSON(w, 200, obj{"provider": "", "comment": "built-in IPAM only"})
return
}
a.ipamSt.mu.Lock()
last := a.ipamSt.last
a.ipamSt.mu.Unlock()
writeJSON(w, 200, obj{"provider": a.ipam.Name(), "url": a.cfg.IPAM.URL, "dns_zone": a.cfg.IPAM.DNSZone,
"tag": a.cfg.IPAM.tag(), "machine_field": a.cfg.IPAM.field(), "last_sync": last})
}
func (a *App) handleIPAMSync(w http.ResponseWriter, r *http.Request) {
dry := r.URL.Query().Get("dry") != ""
rep := a.reconcileIPAM(r.Context(), dry)
code := 200
if rep.Error != "" {
code = 502
if a.ipam == nil {
code = 400
}
}
writeJSON(w, code, rep)
}
// handleIPAMPrefixes lists the prefixes the authority delegates to Rollcall
// (tagged); POST imports the missing ones as subnets.
func (a *App) handleIPAMPrefixes(w http.ResponseWriter, r *http.Request) {
if a.ipam == nil {
fail(w, 400, fmt.Errorf("no external IPAM configured"))
return
}
ps, err := a.ipam.Prefixes(r.Context())
if err != nil {
fail(w, 502, err)
return
}
subnets := a.subnetsSnapshot()
imported := []string{}
for i := range ps {
p, err := netip.ParsePrefix(ps[i].Prefix)
if err != nil {
continue
}
for _, sn := range subnets {
if q, err := netip.ParsePrefix(sn.CIDR); err == nil && q.Masked() == p.Masked() {
ps[i].Local = true
}
}
if r.Method == http.MethodPost && !ps[i].Local {
sn := Subnet{CIDR: p.Masked().String(), Name: ps[i].Desc, Purpose: "provisioning",
Comment: "imported from " + a.ipam.Name(), DNS: []string{}}
if len(sn.Name) > 60 {
sn.Name = sn.Name[:60]
}
if err := a.store.PutSubnet(r.Context(), sn); err != nil {
fail(w, 500, err)
return
}
ps[i].Local = true
ps[i].Comment = "imported"
imported = append(imported, sn.CIDR)
}
}
if len(imported) > 0 {
a.netChanged(r)
}
writeJSON(w, 200, obj{"prefixes": ps, "imported": imported})
}
// ---------------------------------------------------------------- NetBox
type netBox struct {
base string
token string
tag string
field string
client *http.Client
}
func newNetBox(c IPAMConfig) (*netBox, error) {
if c.URL == "" || c.TokenFile == "" {
return nil, fmt.Errorf("netbox: url and token_file are required")
}
tok, err := os.ReadFile(c.TokenFile)
if err != nil {
return nil, fmt.Errorf("netbox token: %w", err)
}
tr := http.DefaultTransport.(*http.Transport).Clone()
if c.CAFile != "" {
pem, err := os.ReadFile(c.CAFile)
if err != nil {
return nil, fmt.Errorf("netbox ca_file: %w", err)
}
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(pem) {
return nil, fmt.Errorf("netbox ca_file %s: no certificates", c.CAFile)
}
tr.TLSClientConfig = &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}
}
return &netBox{base: strings.TrimRight(c.URL, "/"), token: strings.TrimSpace(string(tok)),
tag: c.tag(), field: c.field(), client: &http.Client{Transport: tr, Timeout: 30 * time.Second}}, nil
}
func (n *netBox) Name() string { return "netbox" }
type nbError struct {
Code int
Body string
}
func (e *nbError) Error() string { return fmt.Sprintf("HTTP %d: %s", e.Code, trunc(e.Body, 300)) }
func (n *netBox) do(ctx context.Context, method, path string, body, out any) error {
var rd io.Reader
if body != nil {
b, _ := json.Marshal(body)
rd = bytes.NewReader(b)
}
u := n.base + "/api/" + strings.TrimPrefix(path, "/")
if strings.HasPrefix(path, "http") { // a "next" link: keep our base, the server may name itself differently
if p, err := url.Parse(path); err == nil {
u = n.base + p.Path
if p.RawQuery != "" {
u += "?" + p.RawQuery
}
}
}
req, err := http.NewRequestWithContext(ctx, method, u, rd)
if err != nil {
return err
}
if strings.HasPrefix(n.token, "nbt_") {
req.Header.Set("Authorization", "Bearer "+n.token)
} else {
req.Header.Set("Authorization", "Token "+n.token)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := n.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
b, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
if resp.StatusCode >= 300 {
return &nbError{resp.StatusCode, string(b)}
}
if out != nil && len(b) > 0 {
return json.Unmarshal(b, out)
}
return nil
}
type nbIP struct {
ID int64 `json:"id"`
Address string `json:"address"`
DNSName string `json:"dns_name"`
Description string `json:"description"`
Status struct{ Value string } `json:"status"`
Tags []struct{ Name string } `json:"tags"`
CustomFields map[string]any `json:"custom_fields"`
}
type nbPage[T any] struct {
Next *string `json:"next"`
Results []T `json:"results"`
}
func (n *netBox) list(ctx context.Context, path string, q url.Values) ([]nbIP, error) {
q.Set("limit", "500")
next := path + "?" + q.Encode()
var out []nbIP
for next != "" {
var p nbPage[nbIP]
if err := n.do(ctx, "GET", next, nil, &p); err != nil {
return nil, err
}
out = append(out, p.Results...)
next = ""
if p.Next != nil {
next = *p.Next
}
}
return out, nil
}
func (n *netBox) owner(x nbIP) string {
tagged := false
for _, t := range x.Tags {
tagged = tagged || t.Name == n.tag
}
if !tagged {
return ""
}
if v, ok := x.CustomFields[n.field].(string); ok {
return v
}
return ""
}
func (n *netBox) toExt(x nbIP) extAddr {
ip, bits := x.Address, 0
if p, err := netip.ParsePrefix(x.Address); err == nil {
ip, bits = p.Addr().String(), p.Bits()
}
return extAddr{ID: strconv.FormatInt(x.ID, 10), IP: ip, Bits: bits, DNSName: x.DNSName, Machine: n.owner(x),
Status: x.Status.Value, Desc: x.Description}
}
func (n *netBox) body(a extAddr) obj {
return obj{"address": fmt.Sprintf("%s/%d", a.IP, a.Bits), "status": a.Status, "dns_name": a.DNSName,
"description": a.Desc, "tags": []obj{{"name": n.tag}}, "custom_fields": obj{n.field: a.Machine}}
}
func (n *netBox) find(ctx context.Context, ip string) ([]nbIP, error) {
return n.list(ctx, "ipam/ip-addresses/", url.Values{"address": {ip}})
}
func (n *netBox) Reserve(ctx context.Context, a extAddr) (bool, error) {
cur, err := n.find(ctx, a.IP)
if err != nil {
return false, err
}
for _, x := range cur {
if n.owner(x) == "" {
return true, nil // someone else's
}
}
if len(cur) > 0 {
a.ID = strconv.FormatInt(cur[0].ID, 10)
return false, n.Update(ctx, a)
}
err = n.do(ctx, "POST", "ipam/ip-addresses/", n.body(a), nil)
var ne *nbError
if err != nil && errors.As(err, &ne) && ne.Code == 400 && strings.Contains(strings.ToLower(ne.Body), "duplicate") {
return true, nil // created by someone else between our look and our write
}
return false, err
}
func (n *netBox) Owned(ctx context.Context) ([]extAddr, error) {
xs, err := n.list(ctx, "ipam/ip-addresses/", url.Values{"tag": {n.tag}})
if err != nil {
return nil, err
}
out := []extAddr{}
for _, x := range xs {
if n.owner(x) != "" {
out = append(out, n.toExt(x))
}
}
return out, nil
}
func (n *netBox) Update(ctx context.Context, a extAddr) error {
return n.do(ctx, "PATCH", "ipam/ip-addresses/"+a.ID+"/", n.body(a), nil)
}
func (n *netBox) Delete(ctx context.Context, a extAddr) error {
var cur nbIP // re-read: only delete what is still ours
if err := n.do(ctx, "GET", "ipam/ip-addresses/"+a.ID+"/", nil, &cur); err != nil {
var ne *nbError
if errors.As(err, &ne) && ne.Code == 404 {
return nil
}
return err
}
if n.owner(cur) == "" {
return nil
}
err := n.do(ctx, "DELETE", "ipam/ip-addresses/"+a.ID+"/", nil, nil)
var ne *nbError
if errors.As(err, &ne) && ne.Code == 404 {
return nil
}
return err
}
func (n *netBox) Prefixes(ctx context.Context) ([]extPrefix, error) {
type nbPrefix struct {
Prefix string `json:"prefix"`
Description string `json:"description"`
VLAN *struct {
VID int `json:"vid"`
} `json:"vlan"`
Scope *struct {
Name string `json:"name"`
} `json:"scope"`
}
next := "ipam/prefixes/?" + url.Values{"tag": {n.tag}, "limit": {"500"}}.Encode()
out := []extPrefix{}
for next != "" {
var p nbPage[nbPrefix]
if err := n.do(ctx, "GET", next, nil, &p); err != nil {
return nil, err
}
for _, x := range p.Results {
e := extPrefix{Prefix: x.Prefix, Desc: x.Description}
if x.VLAN != nil {
e.VID = x.VLAN.VID
}
if x.Scope != nil {
e.Site = x.Scope.Name
}
out = append(out, e)
}
next = ""
if p.Next != nil {
next = *p.Next
}
}
return out, nil
}
+253
View File
@@ -0,0 +1,253 @@
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"testing"
)
// fakeNetBox implements the slice of the NetBox REST API the driver uses.
type fakeNetBox struct {
mu sync.Mutex
next int64
ips map[int64]map[string]any
reqs []string
}
func newFakeNetBox() *fakeNetBox { return &fakeNetBox{next: 1, ips: map[int64]map[string]any{}} }
func (f *fakeNetBox) add(addr, dns string, tags []string, machine string) {
f.mu.Lock()
defer f.mu.Unlock()
var ts []any
for _, t := range tags {
ts = append(ts, map[string]any{"name": t})
}
f.ips[f.next] = map[string]any{"id": f.next, "address": addr, "dns_name": dns, "description": "",
"status": map[string]any{"value": "active"}, "tags": ts, "custom_fields": map[string]any{"rollcall_machine": machine}}
f.next++
}
func (f *fakeNetBox) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
f.reqs = append(f.reqs, r.Method+" "+r.URL.Path)
if r.Header.Get("Authorization") != "Bearer nbt_k.secret" {
http.Error(w, "auth", 403)
return
}
host := func(a string) string { return strings.SplitN(a, "/", 2)[0] }
write := func(code int, v any) { w.WriteHeader(code); _ = json.NewEncoder(w).Encode(v) }
in := func() map[string]any {
var b map[string]any
_ = json.NewDecoder(r.Body).Decode(&b)
if s, ok := b["status"].(string); ok {
b["status"] = map[string]any{"value": s}
}
return b
}
var id int64
if _, err := fmt.Sscanf(r.URL.Path, "/api/ipam/ip-addresses/%d/", &id); err == nil {
x, ok := f.ips[id]
if !ok {
write(404, map[string]any{"detail": "not found"})
return
}
switch r.Method {
case "GET":
write(200, x)
case "PATCH":
for k, v := range in() {
x[k] = v
}
write(200, x)
case "DELETE":
delete(f.ips, id)
w.WriteHeader(204)
}
return
}
switch {
case r.URL.Path == "/api/ipam/ip-addresses/" && r.Method == "GET":
res := []any{}
ids := []int64{}
for id := range f.ips {
ids = append(ids, id)
}
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
for _, id := range ids {
x := f.ips[id]
if a := r.URL.Query().Get("address"); a != "" && host(x["address"].(string)) != a {
continue
}
if t := r.URL.Query().Get("tag"); t != "" {
found := false
for _, tt := range x["tags"].([]any) {
found = found || tt.(map[string]any)["name"] == t
}
if !found {
continue
}
}
res = append(res, x)
}
write(200, map[string]any{"count": len(res), "next": nil, "results": res})
case r.URL.Path == "/api/ipam/ip-addresses/" && r.Method == "POST":
b := in()
for _, x := range f.ips {
if host(x["address"].(string)) == host(b["address"].(string)) {
write(400, map[string]any{"address": []string{"Duplicate IP address found in global table"}})
return
}
}
b["id"] = f.next
f.ips[f.next] = b
f.next++
write(201, b)
case r.URL.Path == "/api/ipam/prefixes/":
write(200, map[string]any{"next": nil, "results": []any{
map[string]any{"prefix": "10.213.77.0/24", "description": "lab", "vlan": nil, "scope": map[string]any{"name": "Ev"}},
map[string]any{"prefix": "10.213.80.0/24", "description": "new hall", "vlan": map[string]any{"vid": 300}, "scope": nil},
}})
default:
http.NotFound(w, r)
}
}
func testIPAM(t *testing.T) (*App, *fakeNetBox) {
t.Helper()
f := newFakeNetBox()
srv := httptest.NewServer(f)
t.Cleanup(srv.Close)
tok := filepath.Join(t.TempDir(), "token")
if err := os.WriteFile(tok, []byte("nbt_k.secret\n"), 0o600); err != nil {
t.Fatal(err)
}
cfg := &Config{IPAM: IPAMConfig{Provider: "netbox", URL: srv.URL, TokenFile: tok, DNSZone: "int.example"}}
p, err := newIPAMProvider(cfg.IPAM)
if err != nil {
t.Fatal(err)
}
a := &App{cfg: cfg, ipam: p, ipamSt: &ipamState{kick: make(chan struct{}, 1)}}
a.subnets = []Subnet{
{CIDR: "10.213.77.0/24", Purpose: "provisioning"},
{CIDR: "10.213.79.0/24", Purpose: "storage"},
{CIDR: "10.213.78.0/24", Purpose: "provisioning", Domain: "hall-b.example"},
}
return a, f
}
func TestIPAMDNSNames(t *testing.T) {
a, _ := testIPAM(t)
sn := func(c string) *Subnet { return subnetByRef(a.subnets, c) }
cases := []struct {
x IPAddr
want string
}{
{IPAddr{IP: "10.213.77.50", Subnet: "10.213.77.0/24", Hostname: "node001", Iface: "bond0"}, "node001.int.example"},
{IPAddr{IP: "10.213.79.50", Subnet: "10.213.79.0/24", Hostname: "node001", Iface: "bond0.200"}, "node001-bond0-200.int.example"},
{IPAddr{IP: "10.213.78.50", Subnet: "10.213.78.0/24", Hostname: "Node002"}, "node002.hall-b.example"},
{IPAddr{IP: "10.213.77.5", Subnet: "10.213.77.0/24", Hostname: "core switch", Kind: "reserved"}, ""},
{IPAddr{IP: "10.213.77.6", Subnet: "10.213.77.0/24", Kind: "reserved"}, ""},
}
for _, c := range cases {
if got := a.extDNSName(c.x, sn(c.x.Subnet)); got != c.want {
t.Errorf("%s %q/%s: got %q want %q", c.x.IP, c.x.Hostname, c.x.Iface, got, c.want)
}
}
e := a.extFor(IPAddr{IP: "10.213.77.5", Subnet: "10.213.77.0/24", Hostname: "core switch", Kind: "reserved"}, sn("10.213.77.0/24"))
if e.Status != "reserved" || e.Machine != "-" || e.Bits != 24 || !strings.Contains(e.Desc, "core switch") {
t.Errorf("reserved mapping: %+v", e)
}
}
func TestIPAMReserve(t *testing.T) {
a, f := testIPAM(t)
ctx := context.Background()
f.add("10.213.77.20/24", "printer.int.example", nil, "") // someone else's
f.add("10.213.77.21/24", "", []string{"rollcall"}, "") // tagged but no owner mark: still not ours
f.add("10.213.77.22/24", "old.int.example", []string{"rollcall"}, "m-1")
for ip, wantTaken := range map[string]bool{"10.213.77.20": true, "10.213.77.21": true, "10.213.77.22": false, "10.213.77.23": false} {
taken, err := a.ipam.Reserve(ctx, a.extFor(IPAddr{IP: ip, Subnet: "10.213.77.0/24", MachineID: "m-2", Hostname: "node002", Iface: "eth0"}, &a.subnets[0]))
if err != nil {
t.Fatal(ip, err)
}
if taken != wantTaken {
t.Errorf("%s: taken=%v want %v", ip, taken, wantTaken)
}
}
got := map[string]string{}
for _, x := range f.ips {
got[x["address"].(string)] = x["dns_name"].(string) + " " + fmt.Sprint(x["custom_fields"].(map[string]any)["rollcall_machine"])
}
if got["10.213.77.20/24"] != "printer.int.example " {
t.Errorf("foreign record touched: %q", got["10.213.77.20/24"])
}
if got["10.213.77.22/24"] != "node002.int.example m-2" || got["10.213.77.23/24"] != "node002.int.example m-2" {
t.Errorf("owned/new records: %v", got)
}
}
func TestIPAMReconcile(t *testing.T) {
a, f := testIPAM(t)
ctx := context.Background()
f.add("10.213.77.20/24", "printer.int.example", nil, "") // foreign, collides with a local address
f.add("10.213.77.30/24", "gone.int.example", []string{"rollcall"}, "m-9") // ours, released locally
f.add("10.213.77.50/24", "wrong.int.example", []string{"rollcall"}, "m-1")
f.add("10.213.77.99/24", "keep.int.example", []string{"other"}, "m-3") // not tagged: never deleted
local := []IPAddr{
{IP: "10.213.77.20", Subnet: "10.213.77.0/24", MachineID: "m-2", Hostname: "node002", Kind: "auto"},
{IP: "10.213.77.50", Subnet: "10.213.77.0/24", MachineID: "m-1", Hostname: "node001", Kind: "auto", Iface: "bond0"},
{IP: "10.213.77.51", Subnet: "10.213.77.0/24", MachineID: "m-4", Hostname: "node004", Kind: "static", Iface: "eth0"},
}
dry := ipamReport{DryRun: true}
a.reconcileAddrs(ctx, local, &dry)
if len(f.ips) != 4 {
t.Fatalf("dry run changed records: %d", len(f.ips))
}
if fmt.Sprint(dry.Created, dry.Updated, dry.Deleted) != "[10.213.77.20 10.213.77.51] [10.213.77.50] [10.213.77.30]" {
t.Errorf("dry plan: %+v", dry)
}
rep := ipamReport{}
a.reconcileAddrs(ctx, local, &rep)
if rep.Error != "" {
t.Fatal(rep.Error)
}
if fmt.Sprint(rep.Created, rep.Updated, rep.Deleted, len(rep.Conflicts)) != "[10.213.77.51] [10.213.77.50] [10.213.77.30] 1" {
t.Errorf("report: %+v", rep)
}
addrs := map[string]string{}
for _, x := range f.ips {
addrs[x["address"].(string)] = x["dns_name"].(string)
}
want := map[string]string{"10.213.77.20/24": "printer.int.example", "10.213.77.50/24": "node001.int.example",
"10.213.77.51/24": "node004.int.example", "10.213.77.99/24": "keep.int.example"}
if fmt.Sprint(addrs) != fmt.Sprint(want) {
t.Errorf("records after reconcile:\n got %v\nwant %v", addrs, want)
}
again := ipamReport{}
a.reconcileAddrs(ctx, local, &again)
if n := len(again.Created) + len(again.Updated) + len(again.Deleted); n != 0 {
t.Errorf("second pass not idempotent: %+v", again)
}
}
func TestIPAMPrefixes(t *testing.T) {
a, _ := testIPAM(t)
ps, err := a.ipam.Prefixes(context.Background())
if err != nil {
t.Fatal(err)
}
if len(ps) != 2 || ps[0].Site != "Ev" || ps[1].VID != 300 {
t.Errorf("prefixes: %+v", ps)
}
}
+2 -1
View File
@@ -61,6 +61,7 @@ func usage() {
rollcall subnet list | show CIDR | add CIDR -fabric F [-vid N] [-purpose P] [-gateway G] [-dns a,b] [-domain D] | rm CIDR [-force]
rollcall range add CIDR dynamic|reserved|static START END | rm ID
rollcall ip list [-subnet CIDR] | reserve IP [-name N] | release IP
rollcall ipam [status] | sync [-dry] | prefixes | import external IPAM (NetBox): state, reconcile, delegated prefixes
rollcall layout list | show NAME | add NAME FILE.json | rm NAME
rollcall net show ID | set ID [-layout L] [-rack R] [-static IFACE=IP] [-unstatic IP]
rollcall controllers | controller rm NAME | controller join-code [-name N] [-ttl 60] [-addr HOST:443] | dhcp CONTROLLER
@@ -123,7 +124,7 @@ func main() {
case "list", "show", "events", "power", "bootdev", "recommission", "delete",
"images", "profile", "assign", "unassign", "overlay", "render",
"deploy", "release", "jobs", "job", "tag", "tags", "location",
"bmc", "fabric", "vlan", "subnet", "range", "ip", "layout", "net", "controllers", "controller", "dhcp":
"bmc", "fabric", "vlan", "subnet", "range", "ip", "layout", "net", "controllers", "controller", "dhcp", "ipam":
if err := runClient(cmd, args); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
+100 -2
View File
@@ -264,7 +264,7 @@ func (a *App) handleIPs(w http.ResponseWriter, r *http.Request) {
fail(w, 404, fmt.Errorf("%s is in no subnet", x.IP))
return
}
ok, err := a.store.ClaimIP(r.Context(), IPAddr{IP: x.IP, Subnet: sn.CIDR, Kind: "reserved", Hostname: x.Hostname})
ok, err := a.claimIP(r.Context(), IPAddr{IP: x.IP, Subnet: sn.CIDR, Kind: "reserved", Hostname: x.Hostname})
if err != nil {
fail(w, 500, err)
return
@@ -283,6 +283,7 @@ func (a *App) handleIPDelete(w http.ResponseWriter, r *http.Request) {
fail(w, 500, err)
return
}
a.kickIPAM()
a.netChanged(r)
writeJSON(w, 200, obj{"released": r.PathValue("ip")})
}
@@ -420,6 +421,7 @@ func (a *App) handleMachineNetwork(w http.ResponseWriter, r *http.Request) {
for _, x := range have {
if x.IP == ip && x.Kind == "static" {
_ = a.store.ReleaseIP(r.Context(), ip)
a.kickIPAM()
}
}
}
@@ -435,7 +437,7 @@ func (a *App) handleMachineNetwork(w http.ResponseWriter, r *http.Request) {
_ = a.store.ReleaseIP(r.Context(), x.IP)
}
}
if ok, err := a.store.ClaimIP(r.Context(), IPAddr{IP: st.IP, Subnet: sn.CIDR, MachineID: m.ID, Iface: st.Iface, Kind: "static", Hostname: deref(m.NodeName)}); err != nil {
if ok, err := a.claimIP(r.Context(), IPAddr{IP: st.IP, Subnet: sn.CIDR, MachineID: m.ID, Iface: st.Iface, Kind: "static", Hostname: deref(m.NodeName)}); err != nil {
fail(w, 500, err)
return
} else if !ok {
@@ -788,6 +790,102 @@ func (c *client) netCmd(cmd string, args []string) error {
return err
}
os.Stdout.Write(data)
case "ipam":
return c.ipamCmd(sub, args)
}
return nil
}
func (c *client) ipamCmd(sub string, args []string) error {
printRep := func(rep ipamReport) {
for _, x := range []struct {
k string
v []string
}{{"created", rep.Created}, {"updated", rep.Updated}, {"deleted", rep.Deleted}, {"conflict", rep.Conflicts}} {
for _, ip := range x.v {
fmt.Printf("%-9s %s\n", x.k, ip)
}
}
dry := ""
if rep.DryRun {
dry = " (dry run, nothing changed)"
}
fmt.Printf("%s: %d created, %d updated, %d deleted, %d conflicts%s\n", orDash(rep.Provider),
len(rep.Created), len(rep.Updated), len(rep.Deleted), len(rep.Conflicts), dry)
if rep.Error != "" {
fmt.Println("error:", rep.Error)
}
}
switch sub {
case "", "status":
var st struct {
Provider string `json:"provider"`
URL string `json:"url"`
Zone string `json:"dns_zone"`
Tag string `json:"tag"`
Field string `json:"machine_field"`
Last ipamReport `json:"last_sync"`
}
if err := c.do("GET", "/api/v1/ipam", nil, &st); err != nil {
return err
}
if st.Provider == "" {
fmt.Println("built-in IPAM only (set ipam.provider in config.json for NetBox)")
return nil
}
fmt.Printf("provider %s %s\nzone %s\nowner tag %q, custom field %q\n", st.Provider, st.URL, orDash(st.Zone), st.Tag, st.Field)
if st.Last.At.IsZero() {
fmt.Println("last sync never")
return nil
}
fmt.Printf("last sync %s\n", st.Last.At.Local().Format("2006-01-02 15:04:05"))
printRep(st.Last)
case "sync":
fs := newFlags("ipam sync")
dry := fs.Bool("dry", false, "show what would change")
_ = fs.Parse(args[min(1, len(args)):])
q := ""
if *dry {
q = "?dry=1"
}
var rep ipamReport
err := c.do("POST", "/api/v1/ipam/sync"+q, nil, &rep)
if rep.Provider != "" || rep.Error != "" {
printRep(rep)
}
return err
case "prefixes", "import":
var res struct {
Prefixes []extPrefix `json:"prefixes"`
Imported []string `json:"imported"`
}
method := "GET"
if sub == "import" {
method = "POST"
}
if err := c.do(method, "/api/v1/ipam/prefixes", nil, &res); err != nil {
return err
}
fmt.Printf("%-22s %-6s %-5s %-10s %s\n", "PREFIX", "LOCAL", "VLAN", "SITE", "DESCRIPTION")
for _, p := range res.Prefixes {
vid := "-"
if p.VID > 0 {
vid = strconv.Itoa(p.VID)
}
local := "no"
if p.Local {
local = "yes"
if p.Comment != "" {
local = p.Comment
}
}
fmt.Printf("%-22s %-6s %-5s %-10s %s\n", p.Prefix, local, vid, orDash(p.Site), p.Desc)
}
if sub == "prefixes" {
fmt.Println("prefixes tagged for Rollcall; `rollcall ipam import` creates the missing subnets")
}
default:
usage()
}
return nil
}
+1 -1
View File
@@ -544,7 +544,7 @@ func (a *App) allocIP(ctx context.Context, sn Subnet, machineID, iface, hostname
used[x.IP] = true
}
for _, ip := range freeCandidates(sn, used, a.rackAddresses(), 256) {
ok, err := a.store.ClaimIP(ctx, IPAddr{IP: ip, Subnet: sn.CIDR, MachineID: machineID, Iface: iface, Kind: "auto", Hostname: hostname})
ok, err := a.claimIP(ctx, IPAddr{IP: ip, Subnet: sn.CIDR, MachineID: machineID, Iface: iface, Kind: "auto", Hostname: hostname})
if err != nil {
return "", err
}
+1 -1
View File
@@ -705,7 +705,7 @@ func (a *App) adoptIPs(ctx context.Context) {
if b := bootNIC(machineNICs(m), m.MACs); b != nil && ip == deref(m.MgmtIP) {
iface = b.Name
}
_, _ = a.store.ClaimIP(ctx, IPAddr{IP: ip, Subnet: sn.CIDR, MachineID: m.ID, Iface: iface, Kind: "auto", Hostname: deref(m.NodeName)})
_, _ = a.claimIP(ctx, IPAddr{IP: ip, Subnet: sn.CIDR, MachineID: m.ID, Iface: iface, Kind: "auto", Hostname: deref(m.NodeName)})
}
}
}
+11
View File
@@ -34,6 +34,9 @@ type App struct {
netMu sync.RWMutex
racks map[string]RackController
subnets []Subnet
ipam ipamProvider // nil = built-in IPAM only
ipamSt *ipamState
}
type apiError struct {
@@ -194,6 +197,10 @@ func (a *App) routes() http.Handler {
mux.HandleFunc("GET /api/v1/ips", a.adminAuth(a.handleIPs))
mux.HandleFunc("POST /api/v1/ips", a.adminAuth(a.handleIPs))
mux.HandleFunc("DELETE /api/v1/ips/{ip}", a.adminAuth(a.handleIPDelete))
mux.HandleFunc("GET /api/v1/ipam", a.adminAuth(a.handleIPAM))
mux.HandleFunc("POST /api/v1/ipam/sync", a.adminAuth(a.handleIPAMSync))
mux.HandleFunc("GET /api/v1/ipam/prefixes", a.adminAuth(a.handleIPAMPrefixes))
mux.HandleFunc("POST /api/v1/ipam/prefixes", a.adminAuth(a.handleIPAMPrefixes))
mux.HandleFunc("GET /api/v1/layouts", a.adminAuth(a.handleLayouts))
mux.HandleFunc("POST /api/v1/layouts", a.adminAuth(a.handleLayouts))
mux.HandleFunc("DELETE /api/v1/layouts/{name}", a.adminAuth(a.handleLayoutDelete))
@@ -571,6 +578,7 @@ func (a *App) handleDelete(w http.ResponseWriter, r *http.Request) {
return
}
a.removePXE(m)
defer a.kickIPAM()
if err := a.store.Delete(r.Context(), m.ID); err != nil {
fail(w, 500, err)
return
@@ -675,6 +683,9 @@ func serve(cfgPath string) error {
if err != nil {
return fmt.Errorf("rack controllers: %w", err)
}
if err := app.startIPAM(); err != nil {
return fmt.Errorf("ipam: %w", err)
}
handler := app.routes()
if cfg.TLSListen != "" {
names := append([]string{}, cfg.TLSNames...)
+1
View File
@@ -814,6 +814,7 @@ func (a *App) handleUnassign(w http.ResponseWriter, r *http.Request) {
return
}
_ = a.store.FreeMachineIPs(r.Context(), m.ID, "auto")
a.kickIPAM()
if err := a.store.Unassign(r.Context(), m.ID); err != nil {
fail(w, 500, err)
return
+1
View File
@@ -16,6 +16,7 @@
"tls_listen": ":443",
"tls_names": ["rollcall.example.com"],
"admin_http": "loopback",
"ipam": {"provider": "", "url": "https://netbox.int.example.com", "token_file": "/etc/rollcall/netbox.token", "ca_file": "/etc/rollcall/netbox-ca.pem", "dns_zone": "int.example.com", "sync_interval": "5m"},
"os_dir": "/srv/rollcall/os",
"os_sources": "/etc/rollcall/os-sources.json",
"node_pools": {"mgmt": "10.213.77.50-10.213.77.99"},