Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ func handleContainer(
defer commit()

if err != nil {
return fmt.Errorf("cannot get entry group for container: %w", err)
// The error from egs.get() already includes containerID and is descriptive
return fmt.Errorf("cannot get Avahi entry group for container %s: %w", containerID, err)
}

log.Logf(log.PriDebug, "Checking if Avahi entry group for container %s is empty (before potential reset)", containerID)
empty, err := entryGroup.IsEmpty()
if err != nil {
return fmt.Errorf("checking whether Avahi entry group is empty: %w", err)
return fmt.Errorf("error checking whether Avahi entry group for container %s is empty: %w", containerID, err)
}
log.Logf(log.PriDebug, "Result of IsEmpty check for Avahi entry group for container %s: %t", containerID, empty)

if !empty {
log.Logf(log.PriDebug, "Attempting to reset Avahi entry group for container %s because it was not empty", containerID)
err := entryGroup.Reset()
if err != nil {
return fmt.Errorf("resetting Avahi entry group is empty: %w", err)
return fmt.Errorf("error resetting Avahi entry group for container %s: %w", containerID, err)
}
log.Logf(log.PriDebug, "Successfully reset Avahi entry group for container %s", containerID)
}

if status == "die" || status == "kill" || status == "pause" {
Expand Down
24 changes: 12 additions & 12 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func addAddress(entryGroup *avahi.EntryGroup, hostname string, ipNumbers []strin
continue
}

log.Logf(log.PriDebug, "Attempting to add address for hostname %s (IP: %s) to Avahi entry group", hostname, ipNumber)
err := entryGroup.AddAddress(iface, avahi.ProtoInet, uint32(net.FlagMulticast), hostname, ipNumber)
if err != nil {
log.Logf(log.PriErr, "addAddess() failed: %v", err)

log.Logf(log.PriErr, "Failed to add address for hostname %s (IP: %s): %v", hostname, ipNumber, err)
continue
}

log.Logf(log.PriDebug, "added address for %q pointing to %q", hostname, ipNumber)
log.Logf(log.PriDebug, "Successfully added address for hostname %s pointing to %s", hostname, ipNumber)
}
}

Expand All @@ -36,24 +36,24 @@ func addServices(entryGroup *avahi.EntryGroup, hostname string, ips []string, se
}

for service, portNumber := range services {
log.Logf(log.PriDebug, "Attempting to add service %s for %s on hostname %s (port: %d, IP: %s) to Avahi entry group", service, name, hostname, portNumber, ip)
err := entryGroup.AddService(
iface,
avahi.ProtoInet,
0,
name,
service,
tld,
hostname,
portNumber,
nil,
name, // Name of the service (e.g., container name)
service, // Type of the service (e.g., _http._tcp)
tld, // Domain (e.g., local)
hostname, // Hostname where the service is running
portNumber, // Port number of the service
nil, // TXT records
)
if err != nil {
log.Logf(log.PriErr, "AddService() failed: %v", err)

log.Logf(log.PriErr, "Failed to add service %s for %s on hostname %s (port: %d, IP: %s): %v", service, name, hostname, portNumber, ip, err)
continue
}

log.Logf(log.PriDebug, "added service %q pointing to %q", service, hostname)
log.Logf(log.PriDebug, "Successfully added service %s for %s pointing to hostname %s (port: %d, IP: %s)", service, name, hostname, portNumber, ip)
}
}
}
55 changes: 40 additions & 15 deletions entry_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,55 @@ func newEntryGroups(avahiServer *avahi.Server) *entryGroups {
}

func (e *entryGroups) get(containerID string) (*avahi.EntryGroup, func(), error) {
commit := func() {
empty, err := e.groups[containerID].IsEmpty()
e.mutex.Lock()

groupSuccessfullyRetrieved := true
if _, ok := e.groups[containerID]; !ok {
log.Logf(log.PriDebug, "Attempting to create new Avahi entry group for container %s", containerID)
eg, err := e.avahiServer.EntryGroupNew()
if err != nil {
log.Logf(log.PriErr, "checking whether Avahi entry group is empty: %v", err)
e.mutex.Unlock() // Unlock before returning due to error
groupSuccessfullyRetrieved = false
// Error is already descriptive, fmt.Errorf("error creating new entry group: %w", err)
return nil, func() { e.mutex.Unlock() }, fmt.Errorf("error creating new Avahi entry group for container %s: %w", containerID, err)
}
log.Logf(log.PriDebug, "Successfully created new Avahi entry group for container %s", containerID)
e.groups[containerID] = eg
}

if !empty {
err := e.groups[containerID].Commit()
if err != nil {
log.Logf(log.PriErr, "error committing: %v", err)
}
commit := func() {
defer e.mutex.Unlock()

if !groupSuccessfullyRetrieved {
return
}

e.mutex.Unlock()
}
group := e.groups[containerID]
if group == nil { // Should not happen if groupSuccessfullyRetrieved is true
log.Logf(log.PriCrit, "internal error: group for container %s is nil despite successful retrieval flag in commit()", containerID)
return
}

e.mutex.Lock()
if _, ok := e.groups[containerID]; !ok {
eg, err := e.avahiServer.EntryGroupNew()
log.Logf(log.PriDebug, "Checking if Avahi entry group for %s needs committing", containerID)
empty, err := group.IsEmpty()
if err != nil {
return nil, commit, fmt.Errorf("error creating new entry group: %w", err)
log.Logf(log.PriErr, "Error checking whether Avahi entry group for %s is empty: %v", containerID, err)
// If we can't check if it's empty, we probably shouldn't try to commit it.
return
}
log.Logf(log.PriDebug, "Avahi entry group for %s is empty: %t (before commit check)", containerID, empty)

e.groups[containerID] = eg
if !empty {
log.Logf(log.PriDebug, "Attempting to commit Avahi entry group for %s", containerID)
err := group.Commit()
if err != nil {
log.Logf(log.PriErr, "Error committing Avahi entry group for %s: %v", containerID, err)
} else {
log.Logf(log.PriDebug, "Successfully committed Avahi entry group for %s", containerID)
}
} else {
log.Logf(log.PriDebug, "Avahi entry group for %s is empty, no commit needed", containerID)
}
}

return e.groups[containerID], commit, nil
Expand Down
Loading
Loading