Skip to content

Commit

Permalink
Merge pull request #1495 from allencloud/validate-dupicated-published…
Browse files Browse the repository at this point in the history
…-ports

validate duplicate published port in ServiceSpec
  • Loading branch information
dperny committed Sep 6, 2016
2 parents 27fbaef + 8dd4c69 commit 80b5796
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
8 changes: 4 additions & 4 deletions manager/controlapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ func validateEndpointSpec(epSpec *api.EndpointSpec) error {
return grpc.Errorf(codes.InvalidArgument, "EndpointSpec: ports can't be used with dnsrr mode")
}

portSet := make(map[api.PortConfig]struct{})
portSet := make(map[uint32]struct{})
for _, port := range epSpec.Ports {
if _, ok := portSet[*port]; ok {
return grpc.Errorf(codes.InvalidArgument, "EndpointSpec: duplicate ports provided")
if _, ok := portSet[port.PublishedPort]; ok {
return grpc.Errorf(codes.InvalidArgument, "EndpointSpec: duplicate published ports provided")
}

portSet[*port] = struct{}{}
portSet[port.PublishedPort] = struct{}{}
}

return nil
Expand Down
49 changes: 46 additions & 3 deletions manager/controlapi/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,57 @@ func TestRemoveService(t *testing.T) {
}

func TestValidateEndpointSpec(t *testing.T) {
err := validateEndpointSpec(&api.EndpointSpec{
endPointSpec1 := &api.EndpointSpec{
Mode: api.ResolutionModeDNSRoundRobin,
Ports: []*api.PortConfig{
{
Name: "http", TargetPort: 80,
Name: "http",
TargetPort: 80,
},
},
})
}

endPointSpec2 := &api.EndpointSpec{
Mode: api.ResolutionModeVirtualIP,
Ports: []*api.PortConfig{
{
Name: "http",
TargetPort: 81,
PublishedPort: 8001,
},
{
Name: "http",
TargetPort: 80,
PublishedPort: 8000,
},
},
}

// has duplicated published port, invalid
endPointSpec3 := &api.EndpointSpec{
Mode: api.ResolutionModeVirtualIP,
Ports: []*api.PortConfig{
{
Name: "http",
TargetPort: 81,
PublishedPort: 8001,
},
{
Name: "http",
TargetPort: 80,
PublishedPort: 8001,
},
},
}

err := validateEndpointSpec(endPointSpec1)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))

err = validateEndpointSpec(endPointSpec2)
assert.NoError(t, err)

err = validateEndpointSpec(endPointSpec3)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))
}
Expand Down

0 comments on commit 80b5796

Please sign in to comment.