Skip to content

Commit

Permalink
feat: allow to specify dial interface
Browse files Browse the repository at this point in the history
by default the tcp port status collector listen on all interfaces but
selects the first interface found to issue the request. this commit
makes the collector to accept an interface to which we want to call.

for example:

```yaml
  collectors:
  - tcpPortStatus:
      collectorName: test
      port: 10248
      dialInterface: lo
```

this definition will make the collector listen on all interface but call
127.0.0.1:10248 to test. other example could be:

```yaml
  collectors:
  - tcpPortStatus:
      collectorName: test
      port: 10248
      dialInterface: eth0
```

this again will make the collector listen in all interfaces but it will
use the ip found on eth0 to issue the call.
  • Loading branch information
ricardomaraschini committed Sep 24, 2024
1 parent e685cb9 commit f7b62c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type HTTPLoadBalancer struct {
type TCPPortStatus struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
Interface string `json:"interface,omitempty"`
DialInterface string `json:"dialInterface,omitempty"`
Port int `json:"port"`
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/collect/host_tcpportstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (c *CollectHostTCPPortStatus) Collect(progressChan chan<- interface{}) (map
dialAddress = listenAddress
}

if c.hostCollector.DialInterface != "" {
iface, err := net.InterfaceByName(c.hostCollector.DialInterface)
if err != nil {
return nil, errors.Wrapf(err, "lookup interface %s", c.hostCollector.Interface)
}
ip, err := getIPv4FromInterface(iface)
if err != nil {
return nil, errors.Wrapf(err, "get ipv4 address for interface %s", c.hostCollector.Interface)
}
dialAddress = fmt.Sprintf("%s:%d", ip, c.hostCollector.Port)
}

if dialAddress == "" {
ip, err := getLocalIPv4()
if err != nil {
Expand Down

0 comments on commit f7b62c6

Please sign in to comment.