Skip to content

Commit

Permalink
Simplify netmasks field
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Jul 17, 2024
1 parent 1a2d0b8 commit 5ecd810
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 102 deletions.
31 changes: 10 additions & 21 deletions internal/core/hosts/discovered_host.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package hosts

type NetworkInterface struct {
Index int `json:"index"`
Name string `json:"name"`
Addresses []Address `json:"addresses"`
}

type Address struct {
Address string `json:"address"`
Netmask int `json:"netmask"`
}

type DiscoveredHost struct {
OSVersion string `json:"os_version"`
HostIPAddresses []string `json:"ip_addresses"` // deprecated
NetworkInterfaces []NetworkInterface `json:"network_interfaces"`
HostName string `json:"hostname"`
CPUCount int `json:"cpu_count"`
SocketCount int `json:"socket_count"`
TotalMemoryMB int `json:"total_memory_mb"`
AgentVersion string `json:"agent_version"`
InstallationSource string `json:"installation_source"`
FullyQualifiedDomainName *string `json:"fully_qualified_domain_name,omitempty"`
OSVersion string `json:"os_version"`
HostIPAddresses []string `json:"ip_addresses"`
Netmasks []int `json:"netmasks"`
HostName string `json:"hostname"`
CPUCount int `json:"cpu_count"`
SocketCount int `json:"socket_count"`
TotalMemoryMB int `json:"total_memory_mb"`
AgentVersion string `json:"agent_version"`
InstallationSource string `json:"installation_source"`
FullyQualifiedDomainName *string `json:"fully_qualified_domain_name,omitempty"`
}
26 changes: 6 additions & 20 deletions internal/discovery/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func (d HostDiscovery) GetInterval() time.Duration {

// Execute one iteration of a discovery and publish to the collector
func (d HostDiscovery) Discover(ctx context.Context) (string, error) {
networkInterfaces, ipAddresses, err := getNetworks()
ipAddresses, netmasks, err := getNetworksData()
if err != nil {
return "", err
}

host := hosts.DiscoveredHost{
OSVersion: getOSVersion(),
HostIPAddresses: ipAddresses,
NetworkInterfaces: networkInterfaces,
Netmasks: netmasks,
HostName: d.host,
CPUCount: getLogicalCPUs(),
SocketCount: getCPUSocketCount(),
Expand All @@ -77,29 +77,21 @@ func (d HostDiscovery) Discover(ctx context.Context) (string, error) {
return fmt.Sprintf("Host with name: %s successfully discovered", d.host), nil
}

// getNetworks still returns ip addresses list in 2nd return value
// for backward compatibility
func getNetworks() ([]hosts.NetworkInterface, []string, error) {
func getNetworksData() ([]string, []int, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, nil, err
}

ipAddrList := make([]string, 0)
networkInterfaces := make([]hosts.NetworkInterface, 0)
netmasks := make([]int, 0)

for _, inter := range interfaces {
addrs, err := inter.Addrs()
if err != nil {
continue
}

newInterface := hosts.NetworkInterface{
Index: inter.Index,
Name: inter.Name,
Addresses: make([]hosts.Address, 0),
}

for _, addr := range addrs {
cidrAddr, ip, err := net.ParseCIDR(addr.String())
if err != nil {
Expand All @@ -108,17 +100,11 @@ func getNetworks() ([]hosts.NetworkInterface, []string, error) {

ipAddrList = append(ipAddrList, cidrAddr.String())
mask, _ := ip.Mask.Size()
newInterface.Addresses = append(newInterface.Addresses, hosts.Address{
Address: cidrAddr.String(),
Netmask: mask,
})

netmasks = append(netmasks, mask)
}

networkInterfaces = append(networkInterfaces, newInterface)
}

return networkInterfaces, ipAddrList, nil
return ipAddrList, netmasks, nil
}

func getHostFQDN() *string {
Expand Down
35 changes: 3 additions & 32 deletions internal/discovery/mocks/discovered_host_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,9 @@ func NewDiscoveredHostMock() hosts.DiscoveredHost {
fqdn := "com.example.trento.host"

return hosts.DiscoveredHost{
OSVersion: "15-SP2",
HostIPAddresses: []string{"10.1.1.4", "10.1.1.5", "10.1.1.6"},
NetworkInterfaces: []hosts.NetworkInterface{
{
Index: 0,
Name: "eth0",
Addresses: []hosts.Address{
{
Address: "10.1.1.4",
Netmask: 24,
},
{
Address: "10.1.1.5",
Netmask: 16,
},
{
Address: "10.1.1.6",
Netmask: 32,
},
},
},
{
Index: 1,
Name: "eth1",
Addresses: []hosts.Address{
{
Address: "10.1.2.4",
Netmask: 24,
},
},
},
},
OSVersion: "15-SP2",
HostIPAddresses: []string{"10.1.1.4", "10.1.1.5", "10.1.1.6"},
Netmasks: []int{24, 16, 32},
HostName: "thehostnamewherethediscoveryhappened",
CPUCount: 2,
SocketCount: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,10 @@
"10.1.1.5",
"10.1.1.6"
],
"network_interfaces": [
{
"index": 0,
"name": "eth0",
"addresses": [
{
"address": "10.1.1.4",
"netmask": 24
},
{
"address": "10.1.1.5",
"netmask": 16
},
{
"address": "10.1.1.6",
"netmask": 32
}
]
},
{
"index": 1,
"name": "eth1",
"addresses": [
{
"address": "10.1.2.4",
"netmask": 24
}
]
}
"netmasks": [
24,
16,
32
],
"hostname": "thehostnamewherethediscoveryhappened",
"cpu_count": 2,
Expand Down

0 comments on commit 5ecd810

Please sign in to comment.