Skip to content

Commit b5d33fa

Browse files
authored
Merge pull request kubernetes#81262 from levimm/fixUpdateServiceDNSLabel
fix azure load balancer update dns label issue
2 parents 6d4e6d7 + d95296e commit b5d33fa

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -492,41 +492,52 @@ func (az *Cloud) ensurePublicIPExists(service *v1.Service, pipName string, domai
492492
if err != nil {
493493
return nil, err
494494
}
495-
if existsPip {
496-
return &pip, nil
497-
}
498495

499496
serviceName := getServiceName(service)
500497

501-
if shouldPIPExisted {
502-
return nil, fmt.Errorf("PublicIP from annotation azure-pip-name=%s for service %s doesn't exist", pipName, serviceName)
503-
}
504-
505-
pip.Name = to.StringPtr(pipName)
506-
pip.Location = to.StringPtr(az.Location)
507-
pip.PublicIPAddressPropertiesFormat = &network.PublicIPAddressPropertiesFormat{
508-
PublicIPAllocationMethod: network.Static,
498+
if existsPip {
499+
// return if pip exist and dns label is the same
500+
if getDomainNameLabel(&pip) == domainNameLabel {
501+
return &pip, nil
502+
}
503+
klog.V(2).Infof("ensurePublicIPExists for service(%s): pip(%s) - updating", serviceName, *pip.Name)
504+
if pip.PublicIPAddressPropertiesFormat == nil {
505+
pip.PublicIPAddressPropertiesFormat = &network.PublicIPAddressPropertiesFormat{
506+
PublicIPAllocationMethod: network.Static,
507+
}
508+
}
509+
} else {
510+
if shouldPIPExisted {
511+
return nil, fmt.Errorf("PublicIP from annotation azure-pip-name=%s for service %s doesn't exist", pipName, serviceName)
512+
}
513+
pip.Name = to.StringPtr(pipName)
514+
pip.Location = to.StringPtr(az.Location)
515+
pip.PublicIPAddressPropertiesFormat = &network.PublicIPAddressPropertiesFormat{
516+
PublicIPAllocationMethod: network.Static,
517+
}
518+
pip.Tags = map[string]*string{
519+
serviceTagKey: &serviceName,
520+
clusterNameKey: &clusterName,
521+
}
522+
if az.useStandardLoadBalancer() {
523+
pip.Sku = &network.PublicIPAddressSku{
524+
Name: network.PublicIPAddressSkuNameStandard,
525+
}
526+
}
527+
klog.V(2).Infof("ensurePublicIPExists for service(%s): pip(%s) - creating", serviceName, *pip.Name)
509528
}
510-
if len(domainNameLabel) > 0 {
529+
if len(domainNameLabel) == 0 {
530+
pip.PublicIPAddressPropertiesFormat.DNSSettings = nil
531+
} else {
511532
pip.PublicIPAddressPropertiesFormat.DNSSettings = &network.PublicIPAddressDNSSettings{
512533
DomainNameLabel: &domainNameLabel,
513534
}
514535
}
515-
pip.Tags = map[string]*string{
516-
serviceTagKey: &serviceName,
517-
clusterNameKey: &clusterName,
518-
}
519-
if az.useStandardLoadBalancer() {
520-
pip.Sku = &network.PublicIPAddressSku{
521-
Name: network.PublicIPAddressSkuNameStandard,
522-
}
523-
}
524536

525-
klog.V(2).Infof("ensurePublicIPExists for service(%s): pip(%s) - creating", serviceName, *pip.Name)
526537
klog.V(10).Infof("CreateOrUpdatePIP(%s, %q): start", pipResourceGroup, *pip.Name)
527538
err = az.CreateOrUpdatePIP(service, pipResourceGroup, pip)
528539
if err != nil {
529-
klog.V(2).Infof("ensure(%s) abort backoff: pip(%s) - creating", serviceName, *pip.Name)
540+
klog.V(2).Infof("ensure(%s) abort backoff: pip(%s)", serviceName, *pip.Name)
530541
return nil, err
531542
}
532543
klog.V(10).Infof("CreateOrUpdatePIP(%s, %q): end", pipResourceGroup, *pip.Name)
@@ -540,6 +551,13 @@ func (az *Cloud) ensurePublicIPExists(service *v1.Service, pipName string, domai
540551
return &pip, nil
541552
}
542553

554+
func getDomainNameLabel(pip *network.PublicIPAddress) string {
555+
if pip == nil || pip.PublicIPAddressPropertiesFormat == nil || pip.PublicIPAddressPropertiesFormat.DNSSettings == nil {
556+
return ""
557+
}
558+
return to.String(pip.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel)
559+
}
560+
543561
func getIdleTimeout(s *v1.Service) (*int32, error) {
544562
const (
545563
min = 4

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,7 @@ func TestEnsurePublicIPExists(t *testing.T) {
18331833
existingPIPs []network.PublicIPAddress
18341834
expectedPIP *network.PublicIPAddress
18351835
expectedID string
1836+
expectedDNS string
18361837
expectedError bool
18371838
}{
18381839
{
@@ -1849,6 +1850,28 @@ func TestEnsurePublicIPExists(t *testing.T) {
18491850
expectedID: "/subscriptions/subscription/resourceGroups/rg/providers/" +
18501851
"Microsoft.Network/publicIPAddresses/pip1",
18511852
},
1853+
{
1854+
desc: "ensurePublicIPExists shall update existed PIP's dns label",
1855+
existingPIPs: []network.PublicIPAddress{{
1856+
Name: to.StringPtr("pip1"),
1857+
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
1858+
DNSSettings: &network.PublicIPAddressDNSSettings{
1859+
DomainNameLabel: to.StringPtr("previousdns"),
1860+
},
1861+
},
1862+
}},
1863+
expectedPIP: &network.PublicIPAddress{
1864+
Name: to.StringPtr("pip1"),
1865+
ID: to.StringPtr("/subscriptions/subscription/resourceGroups/rg" +
1866+
"/providers/Microsoft.Network/publicIPAddresses/pip1"),
1867+
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
1868+
DNSSettings: &network.PublicIPAddressDNSSettings{
1869+
DomainNameLabel: to.StringPtr("newdns"),
1870+
},
1871+
},
1872+
},
1873+
expectedDNS: "newdns",
1874+
},
18521875
}
18531876

18541877
for i, test := range testCases {
@@ -1860,7 +1883,7 @@ func TestEnsurePublicIPExists(t *testing.T) {
18601883
t.Fatalf("TestCase[%d] meets unexpected error: %v", i, err)
18611884
}
18621885
}
1863-
pip, err := az.ensurePublicIPExists(&service, "pip1", "", "", false)
1886+
pip, err := az.ensurePublicIPExists(&service, "pip1", test.expectedDNS, "", false)
18641887
if test.expectedID != "" {
18651888
assert.Equal(t, test.expectedID, to.String(pip.ID), "TestCase[%d]: %s", i, test.desc)
18661889
} else {

0 commit comments

Comments
 (0)