Skip to content

Commit 3c92a6d

Browse files
authored
Merge pull request kubernetes#74498 from feiskyer/fix-subnet
Fix subnet annotation checking for Azure internal loadbalancer
2 parents 44d13d3 + 8d0c5d9 commit 3c92a6d

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

pkg/cloudprovider/providers/azure/azure_loadbalancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ func requiresInternalLoadBalancer(service *v1.Service) bool {
15381538

15391539
func subnet(service *v1.Service) *string {
15401540
if requiresInternalLoadBalancer(service) {
1541-
if l, found := service.Annotations[ServiceAnnotationLoadBalancerInternalSubnet]; found {
1541+
if l, found := service.Annotations[ServiceAnnotationLoadBalancerInternalSubnet]; found && strings.TrimSpace(l) != "" {
15421542
return &l
15431543
}
15441544
}

pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/Azure/go-autorest/autorest/to"
2626
"github.com/stretchr/testify/assert"
2727
"k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
)
2930

3031
func TestFindProbe(t *testing.T) {
@@ -243,3 +244,67 @@ func TestGetIdleTimeout(t *testing.T) {
243244
})
244245
}
245246
}
247+
248+
func TestSubnet(t *testing.T) {
249+
for i, c := range []struct {
250+
desc string
251+
service *v1.Service
252+
expected *string
253+
}{
254+
{
255+
desc: "No annotation should return nil",
256+
service: &v1.Service{},
257+
expected: nil,
258+
},
259+
{
260+
desc: "annotation with subnet but no ILB should return nil",
261+
service: &v1.Service{
262+
ObjectMeta: metav1.ObjectMeta{
263+
Annotations: map[string]string{
264+
ServiceAnnotationLoadBalancerInternalSubnet: "subnet",
265+
},
266+
},
267+
},
268+
expected: nil,
269+
},
270+
{
271+
desc: "annotation with subnet but ILB=false should return nil",
272+
service: &v1.Service{
273+
ObjectMeta: metav1.ObjectMeta{
274+
Annotations: map[string]string{
275+
ServiceAnnotationLoadBalancerInternalSubnet: "subnet",
276+
ServiceAnnotationLoadBalancerInternal: "false",
277+
},
278+
},
279+
},
280+
expected: nil,
281+
},
282+
{
283+
desc: "annotation with empty subnet should return nil",
284+
service: &v1.Service{
285+
ObjectMeta: metav1.ObjectMeta{
286+
Annotations: map[string]string{
287+
ServiceAnnotationLoadBalancerInternalSubnet: "",
288+
ServiceAnnotationLoadBalancerInternal: "true",
289+
},
290+
},
291+
},
292+
expected: nil,
293+
},
294+
{
295+
desc: "annotation with subnet and ILB should return subnet",
296+
service: &v1.Service{
297+
ObjectMeta: metav1.ObjectMeta{
298+
Annotations: map[string]string{
299+
ServiceAnnotationLoadBalancerInternalSubnet: "subnet",
300+
ServiceAnnotationLoadBalancerInternal: "true",
301+
},
302+
},
303+
},
304+
expected: to.StringPtr("subnet"),
305+
},
306+
} {
307+
real := subnet(c.service)
308+
assert.Equal(t, c.expected, real, fmt.Sprintf("TestCase[%d]: %s", i, c.desc))
309+
}
310+
}

0 commit comments

Comments
 (0)