Skip to content

Commit

Permalink
Add label-based dns
Browse files Browse the repository at this point in the history
* Add ability to read swarmdns-names label to array

* Map name to node

* Answer nodes per name

* Add out for domain comp

* add dot to end of normDomain

* move dot to matchingDomain

* don't override nodes if no match in map
  • Loading branch information
WGOS authored Feb 5, 2024
1 parent b807f5d commit cde210d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
40 changes: 40 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var client swarm.Client
var logflag bool
var rateLimit int64
var nodes []swarm.SwarmNode
var nameToNodeMap map[string][]swarm.SwarmNode
var mutex = &sync.Mutex{}
var swarmDomains arrayFlags
var returnWorkers bool
Expand Down Expand Up @@ -142,9 +143,29 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
w.WriteMsg(m)
}

func replaceLast(s, search, replace string) string {
i := strings.LastIndex(s, search)
if i == -1 {
return s
}
return s[:i] + replace + s[i+len(search):]
}

func answerForNodes(domain string) []dns.RR {
mutex.Lock()
var rrs []dns.RR
var nodes = nodes

var swarmDomain = *matchingDomain(domain) + "."
var normDomain = strings.ToLower(domain)

if swarmDomain != normDomain {
var subDomain = replaceLast(normDomain, "."+swarmDomain, "")
if len(nameToNodeMap[subDomain]) > 0 {
nodes = nameToNodeMap[subDomain]
}
}

for _, node := range nodes {
if node.IsManager || returnWorkers {
rr := new(dns.A)
Expand Down Expand Up @@ -172,9 +193,28 @@ func refreshNodes() {

mutex.Lock()
nodes, err = client.ListActiveNodes()
mapNodesToNames(nodes)
logger.Printf("Refreshed nodes: %v\n", nodes)
mutex.Unlock()
if err != nil {
panic(err)
}
}

func mapNodesToNames(nodes []swarm.SwarmNode) {
var newMap = make(map[string][]swarm.SwarmNode)

for _, node := range nodes {
if len(node.DnsNames) <= 0 {
continue
}

for _, name := range node.DnsNames {
newMap[name] = append(newMap[name], node)

logger.Printf("Mapped: [%v] to [%v]\n", node.Hostname, name)
}
}

nameToNodeMap = newMap
}
26 changes: 25 additions & 1 deletion swarm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type SwarmNode struct {
Ip string
Hostname string
IsManager bool
DnsNames []string
}

type Client interface {
Expand Down Expand Up @@ -54,7 +55,13 @@ func (client swarmClient) ListActiveNodes() ([]SwarmNode, error) {
ip = node.Status.Addr
}

nodes = append(nodes, SwarmNode{Ip: ip, Hostname: getHostname(node), IsManager: node.ManagerStatus != nil})
nodes = append(nodes,
SwarmNode{
Ip: ip,
Hostname: getHostname(node),
IsManager: node.ManagerStatus != nil,
DnsNames: getDnsNames(node),
})
}
}

Expand All @@ -73,3 +80,20 @@ func getIPFromAddr(addr string) string {
ipAndPort := strings.Split(addr, ":")
return ipAndPort[0]
}

func getDnsNames(node swarm.Node) []string {
var namesStr = node.Spec.Annotations.Labels["swarmdns-names"]

if len(namesStr) <= 0 {
return []string{}
}

var names = strings.Split(namesStr, ",")

// Just to trim whitespaces
for i, name := range names {
names[i] = strings.Trim(name, " ")
}

return names
}

0 comments on commit cde210d

Please sign in to comment.