-
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
[NET-7534] v2: Make port names in consul-k8s compatible with NET-5586 #3528
Conversation
9f7fb0b
to
eef95ee
Compare
@@ -493,7 +498,7 @@ func getEffectiveTargetPort(targetPort intstr.IntOrString, prefixedPods selector | |||
|
|||
// If still no match for the target port, fall back to string-ifying the target port name, which | |||
// is what the PodController will do when converting unnamed ContainerPorts to Workload ports. | |||
return targetPort.String() | |||
return "cslport-" + targetPort.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.
This feels like it belongs in some constants package.
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.
Ah yep will refactor
isNum = true | ||
} | ||
if name == "" || isNum { | ||
name = "cslport-" + strconv.Itoa(int(port.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.
should we use the constant UnnamedWorkloadPortNamePrefix
here?
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.
yup updated thanks
@@ -73,6 +73,20 @@ func PortValue(pod corev1.Pod, value string) (int32, error) { | |||
return int32(raw), err | |||
} | |||
|
|||
// WorkloadPortName returns the container port's name if it has one, and if not, constructs a name from the port number | |||
// and adds a constant prefix. The port name must be 1-15 characters and must have at least 1 alpha character. | |||
func WorkloadPortName(port *corev1.ContainerPort) 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.
can we add some unit tests for this function?
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.
ah yep i forgot to add unit tests on this side after refactoring
// created by the pod controller when creating workloads. | ||
for _, c := range pod.Spec.Containers { | ||
for _, p := range c.Ports { | ||
if strings.HasPrefix(p.Name, constants.UnnamedWorkloadPortNamePrefix) { |
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.
not necessarily for this PR but are we tracking anywhere restrictions like this that we should document for end users?
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.
For this particular case I'd not expect users to run into this and would expect if they do that it's easily discoverable through the pod not being accepted for injection. It definitely feels like a thing that'd be glazed over in any docs. Any other restrictions (so far) are enforced through kube validations
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.
LGTM! Thanks for jumping on this!
func WorkloadPortName(port *corev1.ContainerPort) string { | ||
name := port.Name | ||
var isNum bool | ||
if _, err := strconv.Atoi(name); err == nil { |
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.
codegolf, nonblocking, unnecessary optimization:
You don't have to try to Atoi
the empty string, since you know it won't work.
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.
Ah right, makes sense, I may skip for now because just trying to get the tests green to merge before code freeze 🤞
For posterity, ACK this change and thank you @ndhanushkodi for handling it! |
Changes proposed in this PR
If a K8s service targetport is allowed to be the stringified version of a number, it will be ambiguous in consul what to interpret the string "portID" as.
This change makes it such that the string port can never be a number, and will always also have alpha characters by prefixing "cslport-" to the workload port if the workload port name is unspecified.
How I've tested this PR
How I expect reviewers to test this PR
Checklist