Skip to content

Commit 0995ef5

Browse files
author
Elena Morozova
committed
Fix ensureStaticIP if name for existed address was changed
It might happend when we change external IP address from ephemeral to static and give it a custom name. In this case GetRegionAddress with given name will return Not Found
1 parent 0387ee4 commit 0995ef5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

staging/src/k8s.io/legacy-cloud-providers/gce/gce_loadbalancer_external.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,18 @@ func ensureStaticIP(s CloudAddressService, name, serviceName, region, existingIP
10601060
existed = true
10611061
}
10621062

1063+
// If address exists, get it by IP, because name might be different.
1064+
// This can specifically happen if the IP was changed from ephemeral to static,
1065+
// which results in a new name for the IP.
1066+
if existingIP != "" {
1067+
addr, err := s.GetRegionAddressByIP(region, existingIP)
1068+
if err != nil {
1069+
return "", false, fmt.Errorf("error getting static IP address: %v", err)
1070+
}
1071+
return addr.Address, existed, nil
1072+
}
1073+
1074+
// Otherwise, get address by name
10631075
addr, err := s.GetRegionAddress(name, region)
10641076
if err != nil {
10651077
return "", false, fmt.Errorf("error getting static IP address: %v", err)

staging/src/k8s.io/legacy-cloud-providers/gce/gce_loadbalancer_external_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func TestEnsureStaticIP(t *testing.T) {
6363
if err != nil || !existed || ip != ipPrime {
6464
t.Fatalf(`ensureStaticIP(%v, %v, %v, %v, %v) = %v, %v, %v; want %v, true, nil`, gce, ipName, serviceName, gce.region, ip, ipPrime, existed, err, ip)
6565
}
66+
67+
// Ensure call with different name
68+
ipName = "another-name-for-static-ip"
69+
ipPrime, existed, err = ensureStaticIP(gce, ipName, serviceName, gce.region, ip, cloud.NetworkTierDefault)
70+
if err != nil || !existed || ip != ipPrime {
71+
t.Fatalf(`ensureStaticIP(%v, %v, %v, %v, %v) = %v, %v, %v; want %v, true, nil`, gce, ipName, serviceName, gce.region, ip, ipPrime, existed, err, ip)
72+
}
6673
}
6774

6875
func TestEnsureStaticIPWithTier(t *testing.T) {

0 commit comments

Comments
 (0)