Skip to content

Commit

Permalink
MGMT-19588: Also use bond interface for hostname workaround (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
pastequo authored Feb 20, 2025
1 parent 3e2184b commit 93697bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
65 changes: 40 additions & 25 deletions src/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inventory
import (
"encoding/json"
"net"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -80,12 +81,7 @@ var forbiddenHostnames = []string{
// isForbiddenHostname checks if the given string is a forbidden host name that needs to be replaced
// with an automatically generated one.
func isForbiddenHostname(hostname string) bool {
for _, forbiddenHostname := range forbiddenHostnames {
if hostname == forbiddenHostname {
return true
}
}
return false
return slices.Contains(forbiddenHostnames, hostname)
}

// calculateHostname calculates a hostname from the MAC address of one of the network interfaces of
Expand Down Expand Up @@ -120,35 +116,54 @@ func findUsableNIC(inventory *models.Inventory) (result *models.Interface, err e
for _, nic := range nics {
isPhysical := nic.Type == "physical"
isVLAN := nic.Type == "vlan"
isBond := nic.Type == "bond"
if !(isPhysical || isVLAN || isBond) {
continue
}

hasMAC := nic.MacAddress != ""
if !hasMAC {
continue
}

hasV4 := false
for _, ip := range nic.IPV4Addresses {
hasV4, err = isGlobalCIDR(ip)
if err != nil {
return
}
if hasV4 {
break
}
hasV4, err = hasGlobalCIDR(nic.IPV4Addresses)
if err != nil {
return
}

hasV6 := false
for _, ip := range nic.IPV6Addresses {
hasV6, err = isGlobalCIDR(ip)
if err != nil {
return
}
if hasV6 {
break
}
}
if (isPhysical || isVLAN) && hasMAC && (hasV4 || hasV6) {
result = nic
hasV6, err = hasGlobalCIDR(nic.IPV6Addresses)
if err != nil {
return
}

if !(hasV4 || hasV6) {
continue
}

result = nic
return
}

return
}

func hasGlobalCIDR(ips []string) (bool, error) {
for _, ip := range ips {
ret, err := isGlobalCIDR(ip)
if err != nil {
return false, err
}

if ret {
return true, nil
}
}

return false, nil
}

// isGlobalCIDR returns a boolean flag indicating if the IP address in the given CIDR is a global
// one and not a local one like 127.0.0.1 or ::1. Returns an error if the given string can't be
// parsed as a CIDR.
Expand Down
20 changes: 19 additions & 1 deletion src/inventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ var _ = Describe("Hostname processing", func() {
{
Name: "eth0",
Type: "physical",
MacAddress: "71:A0:A4:6F:BE:C8",
MacAddress: "71:A0:A4:6F:BE:C7",
},
{
Name: "eth0.42",
Expand All @@ -216,5 +216,23 @@ var _ = Describe("Hostname processing", func() {
},
"71-a0-a4-6f-be-c8",
),
Entry(
"Renames with bond",
"localhost.localdomain",
[]*models.Interface{
{
Name: "eth0",
Type: "physical",
MacAddress: "71:A0:A4:6F:BE:C8",
},
{
Name: "bond0",
Type: "bond",
MacAddress: "52:54:00:aa:3d:c4",
IPV4Addresses: []string{"192.168.124.33/24"},
},
},
"52-54-00-aa-3d-c4",
),
)
})

0 comments on commit 93697bb

Please sign in to comment.