Skip to content

Commit

Permalink
Add trimming spaces per range value
Browse files Browse the repository at this point in the history
  • Loading branch information
panslava committed Jan 17, 2023
1 parent d18f8a9 commit 3ddbfec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pkg/utils/sourceranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func getAllSourceRanges(service *v1.Service) (net.IPNetSet, error) {
// if SourceRange field is specified, ignore sourceRange annotation
if len(service.Spec.LoadBalancerSourceRanges) > 0 {
specs := service.Spec.LoadBalancerSourceRanges
for i, spec := range specs {
specs[i] = strings.TrimSpace(spec)
}
ipnets, err := net.ParseIPNets(specs...)

if err != nil {
return nil, NewInvalidSpecLoadBalancerSourceRangesError(specs, err)
}
Expand All @@ -40,8 +42,10 @@ func getAllSourceRanges(service *v1.Service) (net.IPNetSet, error) {
if !ok {
return nil, nil
}
val = strings.TrimSpace(val)
specs := strings.Split(val, ",")
for i, spec := range specs {
specs[i] = strings.TrimSpace(spec)
}
ipnets, err := net.ParseIPNets(specs...)
if err != nil {
return nil, NewInvalidLoadBalancerSourceRangesAnnotationError(val, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/sourceranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func TestServiceSourceRanges(t *testing.T) {
},
{
desc: "Should parse ranges from spec",
specRanges: []string{"192.168.0.1/10", "132.8.0.1/8"},
expectedSourceRanges: []string{"192.128.0.0/10", "132.0.0.0/8"}, // only significant bits are left
specRanges: []string{" 192.168.0.1/10", " 132.8.0.1/8 "},
expectedSourceRanges: []string{"192.128.0.0/10", "132.0.0.0/8"}, // only significant bits are left, spaces are trimmed
},
{
desc: "Should parse ranges from annotations, if no spec value",
annotations: map[string]string{
v1.AnnotationLoadBalancerSourceRangesKey: "192.168.0.1/10,132.8.0.1/8",
v1.AnnotationLoadBalancerSourceRangesKey: " 192.168.0.1/10 , 132.8.0.1/8 ",
},
expectedSourceRanges: []string{"192.128.0.0/10", "132.0.0.0/8"}, // only significant bits are left
expectedSourceRanges: []string{"192.128.0.0/10", "132.0.0.0/8"}, // only significant bits are left, spaces are trimmed
},
{
desc: "Should ignore annotation if spec is present",
Expand Down

0 comments on commit 3ddbfec

Please sign in to comment.