From 31e638e1da28f592dae3b35843d7161d77518535 Mon Sep 17 00:00:00 2001 From: Qi Ni Date: Wed, 24 May 2023 11:36:35 +0800 Subject: [PATCH] fix: make sure the pip dns tag will not be removed when systemTags is set --- pkg/provider/azure_loadbalancer.go | 8 +++- pkg/provider/azure_loadbalancer_test.go | 58 +++++++++++++------------ pkg/provider/azure_utils.go | 1 + 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/pkg/provider/azure_loadbalancer.go b/pkg/provider/azure_loadbalancer.go index bf5fd60ba7..6c51a1c3df 100644 --- a/pkg/provider/azure_loadbalancer.go +++ b/pkg/provider/azure_loadbalancer.go @@ -3016,19 +3016,25 @@ func (az *Cloud) ensurePIPTagged(service *v1.Service, pip *network.PublicIPAddre } // include the cluster name and service names tags when comparing - var clusterName, serviceNames *string + var clusterName, serviceNames, serviceNameUsingDNS *string if v := getClusterFromPIPClusterTags(pip.Tags); v != "" { clusterName = &v } if v := getServiceFromPIPServiceTags(pip.Tags); v != "" { serviceNames = &v } + if v := getServiceFromPIPDNSTags(pip.Tags); v != "" { + serviceNameUsingDNS = &v + } if clusterName != nil { configTags[consts.ClusterNameKey] = clusterName } if serviceNames != nil { configTags[consts.ServiceTagKey] = serviceNames } + if serviceNameUsingDNS != nil { + configTags[consts.ServiceUsingDNSKey] = serviceNameUsingDNS + } tags, changed := az.reconcileTags(pip.Tags, configTags) pip.Tags = tags diff --git a/pkg/provider/azure_loadbalancer_test.go b/pkg/provider/azure_loadbalancer_test.go index 682748dc22..23c74dac9f 100644 --- a/pkg/provider/azure_loadbalancer_test.go +++ b/pkg/provider/azure_loadbalancer_test.go @@ -5478,25 +5478,27 @@ func TestEnsurePIPTagged(t *testing.T) { } pip := network.PublicIPAddress{ Tags: map[string]*string{ - consts.ClusterNameKey: pointer.String("testCluster"), - consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), - "foo": pointer.String("bar"), - "a": pointer.String("j"), - "m": pointer.String("n"), + consts.ClusterNameKey: pointer.String("testCluster"), + consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), + consts.ServiceUsingDNSKey: pointer.String("default/svc1"), + "foo": pointer.String("bar"), + "a": pointer.String("j"), + "m": pointer.String("n"), }, } t.Run("ensurePIPTagged should ensure the pip is tagged as configured", func(t *testing.T) { expectedPIP := network.PublicIPAddress{ Tags: map[string]*string{ - consts.ClusterNameKey: pointer.String("testCluster"), - consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), - "foo": pointer.String("bar"), - "a": pointer.String("b"), - "c": pointer.String("d"), - "y": pointer.String("z"), - "m": pointer.String("n"), - "e": pointer.String(""), + consts.ClusterNameKey: pointer.String("testCluster"), + consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), + consts.ServiceUsingDNSKey: pointer.String("default/svc1"), + "foo": pointer.String("bar"), + "a": pointer.String("b"), + "c": pointer.String("d"), + "y": pointer.String("z"), + "m": pointer.String("n"), + "e": pointer.String(""), }, } changed := cloud.ensurePIPTagged(&service, &pip) @@ -5508,13 +5510,14 @@ func TestEnsurePIPTagged(t *testing.T) { cloud.SystemTags = "a,foo" expectedPIP := network.PublicIPAddress{ Tags: map[string]*string{ - consts.ClusterNameKey: pointer.String("testCluster"), - consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), - "foo": pointer.String("bar"), - "a": pointer.String("b"), - "c": pointer.String("d"), - "y": pointer.String("z"), - "e": pointer.String(""), + consts.ClusterNameKey: pointer.String("testCluster"), + consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), + consts.ServiceUsingDNSKey: pointer.String("default/svc1"), + "foo": pointer.String("bar"), + "a": pointer.String("b"), + "c": pointer.String("d"), + "y": pointer.String("z"), + "e": pointer.String(""), }, } changed := cloud.ensurePIPTagged(&service, &pip) @@ -5527,13 +5530,14 @@ func TestEnsurePIPTagged(t *testing.T) { cloud.TagsMap = map[string]string{"a": "c", "a=b": "c=d", "Y": "zz"} expectedPIP := network.PublicIPAddress{ Tags: map[string]*string{ - consts.ClusterNameKey: pointer.String("testCluster"), - consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), - "foo": pointer.String("bar"), - "a": pointer.String("b"), - "c": pointer.String("d"), - "a=b": pointer.String("c=d"), - "e": pointer.String(""), + consts.ClusterNameKey: pointer.String("testCluster"), + consts.ServiceTagKey: pointer.String("default/svc1,default/svc2"), + consts.ServiceUsingDNSKey: pointer.String("default/svc1"), + "foo": pointer.String("bar"), + "a": pointer.String("b"), + "c": pointer.String("d"), + "a=b": pointer.String("c=d"), + "e": pointer.String(""), }, } changed := cloud.ensurePIPTagged(&service, &pip) diff --git a/pkg/provider/azure_utils.go b/pkg/provider/azure_utils.go index d1d45b749f..7fd34a08c0 100644 --- a/pkg/provider/azure_utils.go +++ b/pkg/provider/azure_utils.go @@ -181,6 +181,7 @@ func (az *Cloud) reconcileTags(currentTagsOnResource, newTags map[string]*string for k := range currentTagsOnResource { if _, ok := newTags[k]; !ok { if found, _ := findKeyInMapCaseInsensitive(systemTagsMap, k); !found { + klog.V(2).Infof("reconcileTags: delete tag %s: %s", k, pointer.StringDeref(currentTagsOnResource[k], "")) delete(currentTagsOnResource, k) changed = true }