Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate duplicate published port in ServiceSpec #1495

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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