Skip to content

Commit 1ecf143

Browse files
committed
fix: Remove managed pip if switched to another pip in a different resource group
1 parent debe0f5 commit 1ecf143

File tree

5 files changed

+288
-58
lines changed

5 files changed

+288
-58
lines changed

pkg/provider/azure_loadbalancer.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,13 +3267,29 @@ func (az *Cloud) ensurePIPTagged(service *v1.Service, pip *armnetwork.PublicIPAd
32673267

32683268
// reconcilePublicIPs reconciles the PublicIP resources similar to how the LB is reconciled.
32693269
func (az *Cloud) reconcilePublicIPs(ctx context.Context, clusterName string, service *v1.Service, lbName string, wantLb bool) ([]*armnetwork.PublicIPAddress, error) {
3270+
logger := klog.FromContext(ctx).WithName("reconcilePublicIPs").
3271+
WithValues("loadBalancer", lbName)
3272+
32703273
pipResourceGroup := az.getPublicIPAddressResourceGroup(service)
32713274

3272-
reconciledPIPs := []*armnetwork.PublicIPAddress{}
3273-
pips, err := az.listPIP(ctx, pipResourceGroup, azcache.CacheReadTypeDefault)
3275+
reconciledPIPs := make([]*armnetwork.PublicIPAddress, 0)
3276+
3277+
var (
3278+
pips []*armnetwork.PublicIPAddress
3279+
err error
3280+
)
3281+
pips, err = az.listPIP(ctx, pipResourceGroup, azcache.CacheReadTypeDefault)
32743282
if err != nil {
32753283
return nil, err
32763284
}
3285+
if !strings.EqualFold(az.ResourceGroup, pipResourceGroup) {
3286+
pipsFromClusterRG, err := az.listPIP(ctx, az.ResourceGroup, azcache.CacheReadTypeDefault)
3287+
if err != nil {
3288+
logger.Error(err, "Failed to list public IPs from cluster resource group", "resourceGroup", az.ResourceGroup)
3289+
return nil, err
3290+
}
3291+
pips = append(pips, pipsFromClusterRG...)
3292+
}
32773293

32783294
pipsV4, pipsV6 := []*armnetwork.PublicIPAddress{}, []*armnetwork.PublicIPAddress{}
32793295
for _, pip := range pips {
@@ -3309,6 +3325,7 @@ func (az *Cloud) reconcilePublicIPs(ctx context.Context, clusterName string, ser
33093325

33103326
// reconcilePublicIP reconciles the PublicIP resources similar to how the LB is reconciled with the specified IP family.
33113327
func (az *Cloud) reconcilePublicIP(ctx context.Context, pips []*armnetwork.PublicIPAddress, clusterName string, service *v1.Service, lbName string, wantLb, isIPv6 bool) (*armnetwork.PublicIPAddress, error) {
3328+
logger := klog.FromContext(ctx).WithName("reconcilePublicIP")
33123329
isInternal := requiresInternalLoadBalancer(service)
33133330
serviceName := getServiceName(service)
33143331
serviceIPTagRequest := getServiceIPTagRequestForPublicIP(service)
@@ -3345,7 +3362,7 @@ func (az *Cloud) reconcilePublicIP(ctx context.Context, pips []*armnetwork.Publi
33453362
for _, pip := range pipsToBeUpdated {
33463363
pipCopy := *pip
33473364
updateFuncs = append(updateFuncs, func() error {
3348-
klog.V(2).Infof("reconcilePublicIP for service(%s): pip(%s), isIPv6(%v) - updating", serviceName, *pip.Name, isIPv6)
3365+
logger.V(2).Info("reconcilePublicIP for service", "service", serviceName, "pip", *pip.Name, "isIPv6", isIPv6, "action", "updating")
33493366
return az.CreateOrUpdatePIP(service, pipResourceGroup, &pipCopy)
33503367
})
33513368
}
@@ -3357,8 +3374,16 @@ func (az *Cloud) reconcilePublicIP(ctx context.Context, pips []*armnetwork.Publi
33573374
for _, pip := range pipsToBeDeleted {
33583375
pipCopy := *pip
33593376
deleteFuncs = append(deleteFuncs, func() error {
3360-
klog.V(2).Infof("reconcilePublicIP for service(%s): pip(%s), isIPv6(%v) - deleting", serviceName, *pip.Name, isIPv6)
3361-
return az.safeDeletePublicIP(ctx, service, pipResourceGroup, &pipCopy, lb)
3377+
pipID := strings.ToLower((ptr.Deref(pipCopy.ID, "")))
3378+
rg, err := getPIPRGFromID(pipID)
3379+
if err != nil {
3380+
logger.Error(err, "Failed to get resource group from PIP ID", "pip-id", pipID)
3381+
return err
3382+
}
3383+
logger.V(2).Info("reconcilePublicIP for service",
3384+
"service", serviceName, "pip", *pip.Name, "rg", rg, "isIPv6", isIPv6, "action", "deleting",
3385+
)
3386+
return az.safeDeletePublicIP(ctx, service, rg, &pipCopy, lb)
33623387
})
33633388
}
33643389
errs = utilerrors.AggregateGoroutines(deleteFuncs...)

0 commit comments

Comments
 (0)