From: Dan Fuhry Date: Fri, 13 Sep 2024 01:22:07 +0000 (-0400) Subject: machines/coredns_plugin: map interface name + hostname to host ID X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=f26130f5f99eef4d306b44445afc5564be91caa0;p=runtime.git machines/coredns_plugin: map interface name + hostname to host ID Will be used soon to support querying by interface name (not complete yet) --- diff --git a/machines/coredns_plugin/registry.go b/machines/coredns_plugin/registry.go index 6615023..26a0dfc 100644 --- a/machines/coredns_plugin/registry.go +++ b/machines/coredns_plugin/registry.go @@ -817,6 +817,7 @@ func (r *registry) monitorEvents() { for { select { case event := <-r.eventsCh: + r.log.Noticef("got event with thing=%s, action=%s, tags=<%+v>\n", event.ItemType, event.Event, event.Tags) r.stats.eventsReceived.WithLabelValues(mbclient.KV{"thing": event.ItemType, "action": event.Event}).Add(1) if event.ItemType == "host" && event.Event == "seen" { if via, ok := event.Tags["via"]; !ok || via != "dhcp" { diff --git a/machines/coredns_plugin/registry_store.go b/machines/coredns_plugin/registry_store.go index a8019aa..9ba83e8 100644 --- a/machines/coredns_plugin/registry_store.go +++ b/machines/coredns_plugin/registry_store.go @@ -17,13 +17,14 @@ type registryStore struct { mu sync.Mutex `json:"-"` log *log.Logger - Sites map[string]*machines.Site `json:"Sites"` - Domains map[string]*machines.Domain `json:"Domains"` - Hosts map[string]*machines.Host `json:"Hosts"` - Ifaces map[string]*machines.Iface `json:"Ifaces"` - Records map[string]map[recordKey][]*machines.DNSRecord `json:"Records"` - HostNames map[string]string `json:"-"` - Addrs *machines.RouterAddresses `json:"-"` + Sites map[string]*machines.Site `json:"Sites"` + Domains map[string]*machines.Domain `json:"Domains"` + Hosts map[string]*machines.Host `json:"Hosts"` + Ifaces map[string]*machines.Iface `json:"Ifaces"` + Records map[string]map[recordKey][]*machines.DNSRecord `json:"Records"` + HostNames map[string]string `json:"-"` + HostInterfaceNames map[string]string `json:"-"` + Addrs *machines.RouterAddresses `json:"-"` } type recordKey string @@ -165,6 +166,7 @@ func (rs *registryStore) fetch(client machines.MachinesClient) error { rs.Addrs = ras rs.HostNames = make(map[string]string) + rs.HostInterfaceNames = make(map[string]string) rs.Hosts = make(map[string]*machines.Host) for _, h := range hosts { h.Name = strings.ToLower(h.Name) @@ -175,6 +177,12 @@ func (rs *registryStore) fetch(client machines.MachinesClient) error { rs.Ifaces = make(map[string]*machines.Iface) for _, i := range ifaces { rs.Ifaces[i.ID()] = i + if i.Host.ID() != "" { + if host, ok := rs.Hosts[i.Host.ID()]; ok { + key := fmt.Sprintf("%s.%s", strings.ToLower(i.NameScrubbed), strings.ToLower(host.Name)) + rs.HostInterfaceNames[key] = i.ID() + } + } } // loop through again and populate LastSeenIface for _, i := range ifaces {