-
Notifications
You must be signed in to change notification settings - Fork 326
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
Register Endpoint ports for ClusterIP services #133
Conversation
266c7fa
to
7af8881
Compare
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.
This looks good to me. I added a few comments/questions.
if ok { | ||
if v, err := strconv.ParseInt(target, 0, 0); err == nil { | ||
if v, err := strconv.ParseInt(portAnnotation, 0, 0); err == nil { | ||
port = int(v) |
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.
This is not the code you changed, but I think it'd be good to change the type of port
to uint16
rather than int
. It should probably be the case for all port variables.
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.
This is the datatype on the consul api struct so we can't change it: https://github.com/hashicorp/consul/blob/master/api/agent.go#L71
Sometimes ports in consul get set to -1
to state that they're disable so that's probably why they use int
. In the case of a service this wouldn't make any sense though.
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 see, thanks for clarifying.
catalog/from-k8s/resource.go
Outdated
@@ -277,52 +277,58 @@ func (t *ServiceResource) generateRegistrations(key string) { | |||
} | |||
|
|||
// Determine the default port and set port annotations | |||
var endpointPortName string |
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.
Would overridePortName
be a better name for this to match overridePortNumber
?
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.
Ahh good catch. At some point while coding this, I was using this variable if Service.targetPort was also a name so I didn't want to use override but I'm not doing this anymore. Will change.
require.Equal("foo", actual[1].Service.Service) | ||
require.Equal("2.3.4.5", actual[1].Service.Address) | ||
require.Equal(8500, actual[1].Service.Port) | ||
require.Equal(4141, actual[1].Service.Port) | ||
require.NotEqual(actual[0].Service.ID, actual[1].Service.ID) | ||
} |
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.
Is overriding the service port to something that isn't a registered endpoint with k8s a valid behavior?
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 think it's the best thing we can do in this situation. If you're setting the port to a specific number then we should use it, even if it's currently wrong. Maybe their next step is to add the containerPort.
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.
That makes sense.
Previously, we were registering the Service's port instead of the underlying endpoint port (as specified by targetPort). If the two were different, this would result in traffic being directed to the wrong port. Also handles edge cases around ports being overridden via annotations.
@ishustava I've addressed your comments, please take another look |
Previously, we were registering the Service's port instead of the
underlying endpoint port (as specified by targetPort).
If the two were different, this would result
in traffic being directed to the wrong port.
Also handles edge cases around ports being overridden via annotations.
Fixes #132
Some background:
ClusterIP
services can have atargetPort
that specifies which port on thePod
that traffic should be routed to. We weren't taking that into account and always usingport
.We are registering each
Endpoint
as a service instance. The endpoints are the Pod IPs so obviously we need to use the Pod's port, i.e.targetPort
.We support overriding which port we use via an annotation (https://www.consul.io/docs/platform/k8s/service-sync.html#service-ports). This can be set to a port name or a specific number. If set to a number, then we just use that number. If set to a name, we'd like to use that port name from the service:
Unfortunately,
targetPort
can also be set to the name of the Pod's port:In this case we need to instead use the named port from the
Endpoint
which will have an actual number since Kubernetes has resolvedtargetname
for us.