diff --git a/pkg/cloudprovider/providers/azure/azure_loadbalancer.go b/pkg/cloudprovider/providers/azure/azure_loadbalancer.go index 9a5f8398684..222d92d519e 100644 --- a/pkg/cloudprovider/providers/azure/azure_loadbalancer.go +++ b/pkg/cloudprovider/providers/azure/azure_loadbalancer.go @@ -1538,7 +1538,7 @@ func requiresInternalLoadBalancer(service *v1.Service) bool { func subnet(service *v1.Service) *string { if requiresInternalLoadBalancer(service) { - if l, found := service.Annotations[ServiceAnnotationLoadBalancerInternalSubnet]; found { + if l, found := service.Annotations[ServiceAnnotationLoadBalancerInternalSubnet]; found && strings.TrimSpace(l) != "" { return &l } } diff --git a/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go b/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go index f11742e5f39..7dda564e7d8 100644 --- a/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go +++ b/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go @@ -25,6 +25,7 @@ import ( "github.com/Azure/go-autorest/autorest/to" "github.com/stretchr/testify/assert" "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestFindProbe(t *testing.T) { @@ -243,3 +244,67 @@ func TestGetIdleTimeout(t *testing.T) { }) } } + +func TestSubnet(t *testing.T) { + for i, c := range []struct { + desc string + service *v1.Service + expected *string + }{ + { + desc: "No annotation should return nil", + service: &v1.Service{}, + expected: nil, + }, + { + desc: "annotation with subnet but no ILB should return nil", + service: &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + ServiceAnnotationLoadBalancerInternalSubnet: "subnet", + }, + }, + }, + expected: nil, + }, + { + desc: "annotation with subnet but ILB=false should return nil", + service: &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + ServiceAnnotationLoadBalancerInternalSubnet: "subnet", + ServiceAnnotationLoadBalancerInternal: "false", + }, + }, + }, + expected: nil, + }, + { + desc: "annotation with empty subnet should return nil", + service: &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + ServiceAnnotationLoadBalancerInternalSubnet: "", + ServiceAnnotationLoadBalancerInternal: "true", + }, + }, + }, + expected: nil, + }, + { + desc: "annotation with subnet and ILB should return subnet", + service: &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + ServiceAnnotationLoadBalancerInternalSubnet: "subnet", + ServiceAnnotationLoadBalancerInternal: "true", + }, + }, + }, + expected: to.StringPtr("subnet"), + }, + } { + real := subnet(c.service) + assert.Equal(t, c.expected, real, fmt.Sprintf("TestCase[%d]: %s", i, c.desc)) + } +}