Files
firewall_containers/network-go/resolver/resolver.go
gyurix f9513cd98a
continuous-integration/drone/push Build is failing
fix(resolver): strip all dashes when matching container names
Instead of prefix matching on the part before the first dash, now strip
all dashes from both the lookup name and the stored container/selector
names and compare exactly. This improves matching accuracy for names
containing multiple dashes or dashes in varying positions.
2026-06-15 11:55:00 +02:00

67 lines
1.7 KiB
Go

package resolver
import (
"log"
"strings"
"firewall_containers/network-go/config"
)
// Resolver resolves names to IPs using the networks.json configuration
type Resolver struct {
cfg *config.NetworksConfig
retries int
}
// NewResolver creates a new name resolver backed by the networks.json config
func NewResolver(cfg *config.NetworksConfig) *Resolver {
return &Resolver{
cfg: cfg,
retries: 2,
}
}
// SetConfig updates the config used for name resolution
func (r *Resolver) SetConfig(cfg *config.NetworksConfig) {
r.cfg = cfg
}
// SetRetries sets the number of retries for resolution
func (r *Resolver) SetRetries(n int) {
r.retries = n
}
// Resolve resolves a name to one or more IP addresses
// It looks up the name in the networks.json config by container_name and selector fields
func (r *Resolver) Resolve(name string) []string {
if r.cfg == nil {
return nil
}
var ips []string
// Look up by container_name and selector in the IPs section
for _, ipCfg := range r.cfg.IPs {
if ipCfg.ContainerName == name || ipCfg.Selector == name {
ips = append(ips, ipCfg.IP)
}
}
// If no exact match, try stripping dashes from the lookup name
if len(ips) == 0 && strings.Contains(name, "-") {
strippedName := strings.ReplaceAll(name, "-", "")
for _, ipCfg := range r.cfg.IPs {
strippedContainer := strings.ReplaceAll(ipCfg.ContainerName, "-", "")
strippedSelector := strings.ReplaceAll(ipCfg.Selector, "-", "")
if strippedContainer == strippedName || strippedSelector == strippedName {
ips = append(ips, ipCfg.IP)
}
}
}
if len(ips) == 0 {
log.Printf("RESOLVER: no IP found for %s in networks.json config", name)
}
return ips
}