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

controlapi: Allow multiple randomly assigned published ports on service #1657

Merged
merged 1 commit into from
Oct 18, 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
6 changes: 6 additions & 0 deletions manager/controlapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ func validateEndpointSpec(epSpec *api.EndpointSpec) error {

portSet := make(map[portSpec]struct{})
for _, port := range epSpec.Ports {
// If published port is not specified, it does not conflict
// with any others.
if port.PublishedPort == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a comment? (e.g., if no host port is assigned, ports can never conflict)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

continue
}

portSpec := portSpec{publishedPort: port.PublishedPort, protocol: port.Protocol}
if _, ok := portSet[portSpec]; ok {
return grpc.Errorf(codes.InvalidArgument, "EndpointSpec: duplicate published ports provided")
Expand Down
47 changes: 47 additions & 0 deletions manager/controlapi/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,47 @@ func TestValidateEndpointSpec(t *testing.T) {
},
}

// duplicated published port but different protocols, valid
endPointSpec4 := &api.EndpointSpec{
Mode: api.ResolutionModeVirtualIP,
Ports: []*api.PortConfig{
{
Name: "dns",
TargetPort: 53,
PublishedPort: 8002,
Protocol: api.ProtocolTCP,
},
{
Name: "dns",
TargetPort: 53,
PublishedPort: 8002,
Protocol: api.ProtocolUDP,
},
},
}

// multiple randomly assigned published ports
endPointSpec5 := &api.EndpointSpec{
Mode: api.ResolutionModeVirtualIP,
Ports: []*api.PortConfig{
{
Name: "http",
TargetPort: 80,
Protocol: api.ProtocolTCP,
},
{
Name: "dns",
TargetPort: 53,
Protocol: api.ProtocolUDP,
},
{
Name: "dns",
TargetPort: 53,
Protocol: api.ProtocolTCP,
},
},
}

err := validateEndpointSpec(endPointSpec1)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))
Expand All @@ -586,6 +627,12 @@ func TestValidateEndpointSpec(t *testing.T) {
err = validateEndpointSpec(endPointSpec3)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))

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

err = validateEndpointSpec(endPointSpec5)
assert.NoError(t, err)
}

func TestServiceEndpointSpecUpdate(t *testing.T) {
Expand Down