Merge pull request #16380 from justinsb/gce_better_ip_address_lookup

gce: match IP addresses including subnet where relevant
This commit is contained in:
Kubernetes Prow Robot 2024-02-24 11:06:21 -08:00 committed by GitHub
commit c5551a005d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 10 deletions

View File

@ -64,25 +64,43 @@ func (e *Address) Find(c *fi.CloudupContext) (*Address, error) {
return actual, err
}
func findAddressByIP(cloud gce.GCECloud, ip string) (*Address, error) {
// Technically this is a regex, but it doesn't matter...
func findAddressByIP(cloud gce.GCECloud, ip string, subnet string) (*Address, error) {
// Technically this is a regex, but it doesn't matter, it's a prefilter
addrs, err := cloud.Compute().Addresses().ListWithFilter(cloud.Project(), cloud.Region(), "address eq "+ip)
if err != nil {
return nil, fmt.Errorf("error listing IP Addresses: %v", err)
}
if len(addrs) == 0 {
var matches []*compute.Address
for _, addr := range addrs {
if subnet != "" && addr.Subnetwork != subnet {
continue
}
if addr.Address == ip {
matches = append(matches, addr)
}
}
if len(matches) == 0 {
return nil, nil
}
if len(addrs) > 1 {
if len(matches) > 1 {
return nil, fmt.Errorf("found multiple Addresses matching %q", ip)
}
addr := matches[0]
actual := &Address{}
actual.IPAddress = &addrs[0].Address
actual.IPAddressType = &addrs[0].AddressType
actual.Purpose = &addrs[0].Purpose
actual.Name = &addrs[0].Name
actual.IPAddress = &addr.Address
actual.IPAddressType = &addr.AddressType
actual.Purpose = &addr.Purpose
actual.Name = &addr.Name
if addr.Subnetwork != "" {
actual.Subnetwork = &Subnet{
Name: fi.PtrTo(lastComponent(addr.Subnetwork)),
}
}
return actual, nil
}

View File

@ -95,9 +95,9 @@ func (e *ForwardingRule) Find(c *fi.CloudupContext) (*ForwardingRule, error) {
}
}
if r.IPAddress != "" {
address, err := findAddressByIP(cloud, r.IPAddress)
address, err := findAddressByIP(cloud, r.IPAddress, r.Subnetwork)
if err != nil {
return nil, fmt.Errorf("error finding Address with IP=%q: %v", r.IPAddress, err)
return nil, fmt.Errorf("error finding Address with IP=%q: %w", r.IPAddress, err)
}
actual.IPAddress = address
}