Skip to content

Commit

Permalink
incus-agent: Add timeout for DNS query
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
Sponsored-by: https://webdock.io
  • Loading branch information
stgraber committed Oct 17, 2024
1 parent e1e902e commit 14bc011
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/incus-agent/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"bufio"
"bytes"
"context"
"fmt"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/server/response"
Expand Down Expand Up @@ -269,8 +271,12 @@ func osState() *api.InstanceStateOSInfo {
}

// Get the FQDN. To avoid needing to run `hostname -f`, do a reverse host lookup for 127.0.1.1, and if found, return the first hostname as the FQDN.
fqdn, err := net.LookupAddr("127.0.1.1")
if err == nil {
ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
defer cancel()

var r net.Resolver
fqdn, err := r.LookupAddr(ctx, "127.0.0.1")
if err == nil && len(fqdn) > 0 {
// Take the first returned hostname and trim the trailing dot.
osInfo.FQDN = strings.TrimSuffix(fqdn[0], ".")
}
Expand Down

0 comments on commit 14bc011

Please sign in to comment.