Skip to content

Commit

Permalink
envoy proxy: Only reuse DNS proxy port when it's free
Browse files Browse the repository at this point in the history
When cilium-agent starts, it will allocate a free port for proxy to
use, if users don't speicify in config. It also tries to recover
previous allocation from iptables rules, but the recover doesn't check
if the port is already open by other processes on the host. This change
will check the recovered port is free before assign it to DNS proxy.

Fix cilium#22465

Signed-off-by: Yongkun Gui <ygui@google.com>
  • Loading branch information
anfernee committed May 15, 2023
1 parent a581786 commit 08555a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions daemon/cmd/fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,12 @@ func (d *Daemon) bootstrapFQDN(possibleEndpoints map[uint16]*endpoint.Endpoint,
if option.Config.ToFQDNsProxyPort != 0 {
port = uint16(option.Config.ToFQDNsProxyPort)
} else if port == 0 {
// Try locate old DNS proxy port number from the datapath
port = d.datapath.GetProxyPort(proxy.DNSProxyName)
// Try locate old DNS proxy port number from the datapath, and reuse it if it's not open
oldPort := d.datapath.GetProxyPort(proxy.DNSProxyName)
openLocalPorts := proxy.OpenLocalPorts()
if _, alreadyOpen := openLocalPorts[oldPort]; !alreadyOpen {
port = oldPort
}
}
if err := re.InitRegexCompileLRU(option.Config.FQDNRegexCompileLRUSize); err != nil {
return fmt.Errorf("could not initialize regex LRU cache: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/proxy/netstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ func readOpenLocalPorts(procNetFiles []string) map[uint16]struct{} {

return openLocalPorts
}

// OpenLocalPorts returns the set of L4 ports currently open locally.
func OpenLocalPorts() map[uint16]struct{} {
return readOpenLocalPorts(append(procNetTCPFiles, procNetUDPFiles...))
}

0 comments on commit 08555a5

Please sign in to comment.