Skip to content

Commit bf79c7c

Browse files
authored
Merge pull request kubernetes#77719 from feiskyer/fix-service-tag
Fix some service tags not supported issues for Azure LoadBalancer service
2 parents 8fecbd8 + 5b39671 commit bf79c7c

File tree

2 files changed

+70
-21
lines changed

2 files changed

+70
-21
lines changed

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const (
7474

7575
// ServiceAnnotationAllowedServiceTag is the annotation used on the service
7676
// to specify a list of allowed service tags separated by comma
77+
// Refer https://docs.microsoft.com/en-us/azure/virtual-network/security-overview#service-tags for all supported service tags.
7778
ServiceAnnotationAllowedServiceTag = "service.beta.kubernetes.io/azure-allowed-service-tags"
7879

7980
// ServiceAnnotationLoadBalancerIdleTimeout is the annotation used on the service
@@ -90,13 +91,6 @@ const (
9091
clusterNameKey = "kubernetes-cluster-name"
9192
)
9293

93-
var (
94-
// supportedServiceTags holds a list of supported service tags on Azure.
95-
// Refer https://docs.microsoft.com/en-us/azure/virtual-network/security-overview#service-tags for more information.
96-
supportedServiceTags = sets.NewString("VirtualNetwork", "VIRTUAL_NETWORK", "AzureLoadBalancer", "AZURE_LOADBALANCER",
97-
"Internet", "INTERNET", "AzureTrafficManager", "Storage", "Sql")
98-
)
99-
10094
// GetLoadBalancer returns whether the specified load balancer exists, and
10195
// if so, what its status is.
10296
func (az *Cloud) GetLoadBalancer(ctx context.Context, clusterName string, service *v1.Service) (status *v1.LoadBalancerStatus, exists bool, err error) {
@@ -1028,10 +1022,7 @@ func (az *Cloud) reconcileSecurityGroup(clusterName string, service *v1.Service,
10281022
if err != nil {
10291023
return nil, err
10301024
}
1031-
serviceTags, err := getServiceTags(service)
1032-
if err != nil {
1033-
return nil, err
1034-
}
1025+
serviceTags := getServiceTags(service)
10351026
var sourceAddressPrefixes []string
10361027
if (sourceRanges == nil || servicehelpers.IsAllowAll(sourceRanges)) && len(serviceTags) == 0 {
10371028
if !requiresInternalLoadBalancer(service) {
@@ -1609,24 +1600,25 @@ func useSharedSecurityRule(service *v1.Service) bool {
16091600
return false
16101601
}
16111602

1612-
func getServiceTags(service *v1.Service) ([]string, error) {
1603+
func getServiceTags(service *v1.Service) []string {
1604+
if service == nil {
1605+
return nil
1606+
}
1607+
16131608
if serviceTags, found := service.Annotations[ServiceAnnotationAllowedServiceTag]; found {
1609+
result := []string{}
16141610
tags := strings.Split(strings.TrimSpace(serviceTags), ",")
16151611
for _, tag := range tags {
1616-
// Storage and Sql service tags support setting regions with suffix ".Region"
1617-
if strings.HasPrefix(tag, "Storage.") || strings.HasPrefix(tag, "Sql.") {
1618-
continue
1619-
}
1620-
1621-
if !supportedServiceTags.Has(tag) {
1622-
return nil, fmt.Errorf("only %q are allowed in service tags", supportedServiceTags.List())
1612+
serviceTag := strings.TrimSpace(tag)
1613+
if serviceTag != "" {
1614+
result = append(result, serviceTag)
16231615
}
16241616
}
16251617

1626-
return tags, nil
1618+
return result
16271619
}
16281620

1629-
return nil, nil
1621+
return nil
16301622
}
16311623

16321624
func serviceOwnsPublicIP(pip *network.PublicIPAddress, clusterName, serviceName string) bool {

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,60 @@ func TestServiceOwnsPublicIP(t *testing.T) {
447447
assert.Equal(t, owns, c.expected, "TestCase[%d]: %s", i, c.desc)
448448
}
449449
}
450+
451+
func TestGetServiceTags(t *testing.T) {
452+
tests := []struct {
453+
desc string
454+
service *v1.Service
455+
expected []string
456+
}{
457+
{
458+
desc: "nil should be returned when service is nil",
459+
service: nil,
460+
expected: nil,
461+
},
462+
{
463+
desc: "nil should be returned when service has no annotations",
464+
service: &v1.Service{},
465+
expected: nil,
466+
},
467+
{
468+
desc: "single tag should be returned when service has set one annotations",
469+
service: &v1.Service{
470+
ObjectMeta: metav1.ObjectMeta{
471+
Annotations: map[string]string{
472+
ServiceAnnotationAllowedServiceTag: "tag1",
473+
},
474+
},
475+
},
476+
expected: []string{"tag1"},
477+
},
478+
{
479+
desc: "multiple tags should be returned when service has set multi-annotations",
480+
service: &v1.Service{
481+
ObjectMeta: metav1.ObjectMeta{
482+
Annotations: map[string]string{
483+
ServiceAnnotationAllowedServiceTag: "tag1, tag2",
484+
},
485+
},
486+
},
487+
expected: []string{"tag1", "tag2"},
488+
},
489+
{
490+
desc: "correct tags should be returned when comma or spaces are included in the annotations",
491+
service: &v1.Service{
492+
ObjectMeta: metav1.ObjectMeta{
493+
Annotations: map[string]string{
494+
ServiceAnnotationAllowedServiceTag: ", tag1, ",
495+
},
496+
},
497+
},
498+
expected: []string{"tag1"},
499+
},
500+
}
501+
502+
for i, c := range tests {
503+
tags := getServiceTags(c.service)
504+
assert.Equal(t, tags, c.expected, "TestCase[%d]: %s", i, c.desc)
505+
}
506+
}

0 commit comments

Comments
 (0)