-
Notifications
You must be signed in to change notification settings - Fork 607
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
Allow different services to use the same host-mode port #2177
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package controlapi | |
import ( | ||
"errors" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -489,18 +488,32 @@ func (s *Server) checkPortConflicts(spec *api.ServiceSpec, serviceID string) err | |
return nil | ||
} | ||
|
||
pcToString := func(pc *api.PortConfig) string { | ||
port := strconv.FormatUint(uint64(pc.PublishedPort), 10) | ||
return port + "/" + pc.Protocol.String() | ||
type portSpec struct { | ||
protocol api.PortConfig_Protocol | ||
publishedPort uint32 | ||
} | ||
|
||
reqPorts := make(map[string]bool) | ||
pcToStruct := func(pc *api.PortConfig) portSpec { | ||
return portSpec{ | ||
protocol: pc.Protocol, | ||
publishedPort: pc.PublishedPort, | ||
} | ||
} | ||
|
||
ingressPorts := make(map[portSpec]struct{}) | ||
hostModePorts := make(map[portSpec]struct{}) | ||
for _, pc := range spec.Endpoint.Ports { | ||
if pc.PublishedPort > 0 { | ||
reqPorts[pcToString(pc)] = true | ||
if pc.PublishedPort == 0 { | ||
continue | ||
} | ||
switch pc.PublishMode { | ||
case api.PublishModeIngress: | ||
ingressPorts[pcToStruct(pc)] = struct{}{} | ||
case api.PublishModeHost: | ||
hostModePorts[pcToStruct(pc)] = struct{}{} | ||
} | ||
} | ||
if len(reqPorts) == 0 { | ||
if len(ingressPorts) == 0 && len(hostModePorts) == 0 { | ||
return nil | ||
} | ||
|
||
|
@@ -516,22 +529,47 @@ func (s *Server) checkPortConflicts(spec *api.ServiceSpec, serviceID string) err | |
return err | ||
} | ||
|
||
isPortInUse := func(pc *api.PortConfig, service *api.Service) error { | ||
if pc.PublishedPort == 0 { | ||
return nil | ||
} | ||
|
||
switch pc.PublishMode { | ||
case api.PublishModeHost: | ||
if _, ok := ingressPorts[pcToStruct(pc)]; ok { | ||
return grpc.Errorf(codes.InvalidArgument, "port '%d' is already in use by service '%s' (%s) as a host-published port", pc.PublishedPort, service.Spec.Annotations.Name, service.ID) | ||
} | ||
|
||
// Multiple services with same port in host publish mode can | ||
// coexist - this is handled by the scheduler. | ||
return nil | ||
case api.PublishModeIngress: | ||
_, ingressConflict := ingressPorts[pcToStruct(pc)] | ||
_, hostModeConflict := hostModePorts[pcToStruct(pc)] | ||
if ingressConflict || hostModeConflict { | ||
return grpc.Errorf(codes.InvalidArgument, "port '%d' is already in use by service '%s' (%s) as an ingress port", pc.PublishedPort, service.Spec.Annotations.Name, service.ID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should remove this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are confusing the new service (which the maps reflect) with the existing services we iterate over. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. |
||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
for _, service := range services { | ||
// If service ID is the same (and not "") then this is an update | ||
if serviceID != "" && serviceID == service.ID { | ||
continue | ||
} | ||
if service.Spec.Endpoint != nil { | ||
for _, pc := range service.Spec.Endpoint.Ports { | ||
if reqPorts[pcToString(pc)] { | ||
return grpc.Errorf(codes.InvalidArgument, "port '%d' is already in use by service '%s' (%s)", pc.PublishedPort, service.Spec.Annotations.Name, service.ID) | ||
if err := isPortInUse(pc, service); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
if service.Endpoint != nil { | ||
for _, pc := range service.Endpoint.Ports { | ||
if reqPorts[pcToString(pc)] { | ||
return grpc.Errorf(codes.InvalidArgument, "port '%d' is already in use by service '%s' (%s)", pc.PublishedPort, service.Spec.Annotations.Name, service.ID) | ||
if err := isPortInUse(pc, service); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ var ( | |
&PluginFilter{}, | ||
&ConstraintFilter{}, | ||
&PlatformFilter{}, | ||
&HostPortFilter{}, | ||
} | ||
) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
as an ingress port
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is correct as is. Since we are in the
api.PublishModeHost
case, this is a newly-added ingress port conflicting with an existing host-published port.