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

[NET-7534] v2: Make port names in consul-k8s compatible with NET-5586 #3528

Merged
merged 4 commits into from
Jan 31, 2024

Conversation

ndhanushkodi
Copy link
Contributor

@ndhanushkodi ndhanushkodi commented Jan 30, 2024

Changes proposed in this PR

  • This change in consul involves now interpreting whether xRoute/FailoverPolicy/DestinationPolicy resource service references use either the service port (virtualPort in consul) or service target port (targetPort in consul). To make this decision unambiguously:

This change updates our interpretation of these reference fields/keys (parent, backend, destination), s.t.:

  • A numeric value will be exclusively interpreted to indicate a ServicePort.virtual_port
  • A non-numeric value will be exclusively interpreted to indicate a ServicePort.target_port (this supports VMs/Nomad and other cases where network virtual ports are not used, and port names are expected to be in reference to workload ports, not service ports)
  • 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

@ndhanushkodi ndhanushkodi changed the title fix consul-k8s ports [NET-7534] v2: Make port names in consul-k8s compatible with NET-5586 Jan 30, 2024
@ndhanushkodi ndhanushkodi force-pushed the nd/net-7534-fix-consulk8s-ports branch from 9f7fb0b to eef95ee Compare January 30, 2024 22:13
@ndhanushkodi ndhanushkodi requested a review from rboyer January 30, 2024 22:45
@@ -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()
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yep will refactor

@ndhanushkodi ndhanushkodi added pr/no-changelog PR does not need a corresponding .changelog entry pr/no-backport signals that a PR will not contain a backport label labels Jan 31, 2024
@ndhanushkodi ndhanushkodi requested a review from rboyer January 31, 2024 03:17
isNum = true
}
if name == "" || isNum {
name = "cslport-" + strconv.Itoa(int(port.ContainerPort))
Copy link
Member

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?

Copy link
Contributor Author

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 {
Copy link
Member

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?

Copy link
Contributor Author

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) {
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

@jm96441n jm96441n left a 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 {
Copy link
Member

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.

Copy link
Contributor Author

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 🤞

@ndhanushkodi ndhanushkodi merged commit a054e33 into main Jan 31, 2024
48 checks passed
@ndhanushkodi ndhanushkodi deleted the nd/net-7534-fix-consulk8s-ports branch January 31, 2024 23:49
@zalimeni
Copy link
Member

zalimeni commented Feb 5, 2024

For posterity, ACK this change and thank you @ndhanushkodi for handling it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/no-backport signals that a PR will not contain a backport label pr/no-changelog PR does not need a corresponding .changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants