Skip to content

🐛 fix: set correct IP address type for machine #3379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/services/vimmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,13 @@ func (v *VimMachineService) reconcileNetwork(ctx context.Context, vimMachineCtx
addresses := vm.Status.Addresses
machineAddresses := make([]clusterv1.MachineAddress, 0, len(addresses))
for _, addr := range addresses {
var addrType = clusterv1.MachineInternalIP
if !infrautilv1.IsInternalIP(addr) {
addrType = clusterv1.MachineExternalIP
}

machineAddresses = append(machineAddresses, clusterv1.MachineAddress{
Type: clusterv1.MachineExternalIP,
Type: addrType,
Address: addr,
})
}
Expand Down
41 changes: 38 additions & 3 deletions pkg/util/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ func GetMachinePreferredIPAddress(machine *infrav1.VSphereMachine) (string, erro
}

for _, machineAddr := range machine.Status.Addresses {
if machineAddr.Type != clusterv1.MachineExternalIP {
continue
}
if cidr == nil {
return machineAddr.Address, nil
}
Expand Down Expand Up @@ -258,3 +255,41 @@ func MachinesAsString(machines []*clusterv1.Machine) string {
}
return message
}

// IsInternalIP checks if the IP is private/internal.
func IsInternalIP(ip string) bool {
netIP := net.ParseIP(ip)
if netIP == nil {
return false
}

privateBlocks := []*net.IPNet{
{IP: net.IPv4(10, 0, 0, 0), Mask: net.CIDRMask(8, 32)}, // 10.0.0.0/8
{IP: net.IPv4(172, 16, 0, 0), Mask: net.CIDRMask(12, 32)}, // 172.16.0.0/12
{IP: net.IPv4(192, 168, 0, 0), Mask: net.CIDRMask(16, 32)}, // 192.168.0.0/16
{IP: net.IPv4(127, 0, 0, 1), Mask: net.CIDRMask(8, 32)}, // 127.0.0.0/8 (loopback)
{IP: net.IPv4(169, 254, 0, 0), Mask: net.CIDRMask(16, 32)}, // 169.254.0.0/16 (link-local)
}
Comment on lines +266 to +272
Copy link
Member

@neolit123 neolit123 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that cluster local subnets are not may not be the same as local network subnets.

Copy link
Member

@neolit123 neolit123 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think that would be the correct way to determine if the ip is internal. it would be an internal network ip, but not necessary the internal cluster ip allocated to a node. to check if the ip is internal as per k8s' definition, i think one would have to check how the controller manager cidr is configured.

e.g. kcm is started with --cluster-cidr=10.244.0.0/16


// Check IPv4 private ranges
for _, block := range privateBlocks {
if block.Contains(netIP) {
return true
}
}

// Check IPv6 private ranges
privateIPv6Blocks := []*net.IPNet{
{IP: net.ParseIP("::1"), Mask: net.CIDRMask(128, 128)}, // Loopback
{IP: net.ParseIP("fc00::"), Mask: net.CIDRMask(7, 128)}, // Unique Local Addresses (ULA)
{IP: net.ParseIP("fe80::"), Mask: net.CIDRMask(10, 128)}, // Link-local
}

for _, block := range privateIPv6Blocks {
if block.Contains(netIP) {
return true
}
}

return false
}