@@ -83,6 +83,11 @@ const (
83
83
// ServiceAnnotationLoadBalancerMixedProtocols is the annotation used on the service
84
84
// to create both TCP and UDP protocols when creating load balancer rules.
85
85
ServiceAnnotationLoadBalancerMixedProtocols = "service.beta.kubernetes.io/azure-load-balancer-mixed-protocols"
86
+
87
+ // serviceTagKey is the service key applied for public IP tags.
88
+ serviceTagKey = "service"
89
+ // clusterNameKey is the cluster name key applied for public IP tags.
90
+ clusterNameKey = "kubernetes-cluster-name"
86
91
)
87
92
88
93
var (
@@ -465,7 +470,7 @@ func (az *Cloud) findServiceIPAddress(ctx context.Context, clusterName string, s
465
470
return lbStatus .Ingress [0 ].IP , nil
466
471
}
467
472
468
- func (az * Cloud ) ensurePublicIPExists (service * v1.Service , pipName string , domainNameLabel string ) (* network.PublicIPAddress , error ) {
473
+ func (az * Cloud ) ensurePublicIPExists (service * v1.Service , pipName string , domainNameLabel , clusterName string ) (* network.PublicIPAddress , error ) {
469
474
pipResourceGroup := az .getPublicIPAddressResourceGroup (service )
470
475
pip , existsPip , err := az .getPublicIPAddress (pipResourceGroup , pipName )
471
476
if err != nil {
@@ -486,7 +491,10 @@ func (az *Cloud) ensurePublicIPExists(service *v1.Service, pipName string, domai
486
491
DomainNameLabel : & domainNameLabel ,
487
492
}
488
493
}
489
- pip .Tags = map [string ]* string {"service" : & serviceName }
494
+ pip .Tags = map [string ]* string {
495
+ serviceTagKey : & serviceName ,
496
+ clusterNameKey : & clusterName ,
497
+ }
490
498
if az .useStandardLoadBalancer () {
491
499
pip .Sku = & network.PublicIPAddressSku {
492
500
Name : network .PublicIPAddressSkuNameStandard ,
@@ -711,7 +719,7 @@ func (az *Cloud) reconcileLoadBalancer(clusterName string, service *v1.Service,
711
719
return nil , err
712
720
}
713
721
domainNameLabel := getPublicIPDomainNameLabel (service )
714
- pip , err := az .ensurePublicIPExists (service , pipName , domainNameLabel )
722
+ pip , err := az .ensurePublicIPExists (service , pipName , domainNameLabel , clusterName )
715
723
if err != nil {
716
724
return nil , err
717
725
}
@@ -1344,9 +1352,7 @@ func (az *Cloud) reconcilePublicIP(clusterName string, service *v1.Service, lb *
1344
1352
1345
1353
for i := range pips {
1346
1354
pip := pips [i ]
1347
- if pip .Tags != nil &&
1348
- (pip .Tags )["service" ] != nil &&
1349
- * (pip .Tags )["service" ] == serviceName {
1355
+ if serviceOwnsPublicIP (& pip , clusterName , serviceName ) {
1350
1356
// We need to process for pips belong to this service
1351
1357
pipName := * pip .Name
1352
1358
if wantLb && ! isInternal && pipName == desiredPipName {
@@ -1369,7 +1375,7 @@ func (az *Cloud) reconcilePublicIP(clusterName string, service *v1.Service, lb *
1369
1375
// Confirm desired public ip resource exists
1370
1376
var pip * network.PublicIPAddress
1371
1377
domainNameLabel := getPublicIPDomainNameLabel (service )
1372
- if pip , err = az .ensurePublicIPExists (service , desiredPipName , domainNameLabel ); err != nil {
1378
+ if pip , err = az .ensurePublicIPExists (service , desiredPipName , domainNameLabel , clusterName ); err != nil {
1373
1379
return nil , err
1374
1380
}
1375
1381
return pip , nil
@@ -1612,3 +1618,24 @@ func getServiceTags(service *v1.Service) ([]string, error) {
1612
1618
1613
1619
return nil , nil
1614
1620
}
1621
+
1622
+ func serviceOwnsPublicIP (pip * network.PublicIPAddress , clusterName , serviceName string ) bool {
1623
+ if pip != nil && pip .Tags != nil {
1624
+ serviceTag := pip .Tags [serviceTagKey ]
1625
+ clusterTag := pip .Tags [clusterNameKey ]
1626
+
1627
+ if serviceTag != nil && * serviceTag == serviceName {
1628
+ // Backward compatible for clusters upgraded from old releases.
1629
+ // In such case, only "service" tag is set.
1630
+ if clusterTag == nil {
1631
+ return true
1632
+ }
1633
+
1634
+ // If cluster name tag is set, then return true if it matches.
1635
+ if * clusterTag == clusterName {
1636
+ return true
1637
+ }
1638
+ }
1639
+ }
1640
+ return false
1641
+ }
0 commit comments