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

Support AdminNetworkPolicy named ports #9254

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

mazdakn
Copy link
Member

@mazdakn mazdakn commented Sep 19, 2024

Description

Add support for AdminNetworkPolicy named ports.

We default to tcp protocol for named ports:

namedPortProto := labelindex.ProtocolTCP

and we match it differently for named ports here:
if protocol.Type == numorstring.NumOrStringNum {

Related issues/PRs

AdminNetworkPolicy core features: #9206

Todos

  • Tests
  • Documentation
  • Release note

Release Note

Add support for AdminNetworkPolicy named ports.

Reminder for the reviewer

Make sure that this PR has the correct labels and milestone set.

Every PR needs one docs-* label.

  • docs-pr-required: This change requires a change to the documentation that has not been completed yet.
  • docs-completed: This change has all necessary documentation completed.
  • docs-not-required: This change has no user-facing impact and requires no docs.

Every PR needs one release-note-* label.

  • release-note-required: This PR has user-facing changes. Most PRs should have this label.
  • release-note-not-required: This PR has no user-facing changes.

Other optional labels:

  • cherry-pick-candidate: This PR should be cherry-picked to an earlier release. For bug fixes only.
  • needs-operator-pr: This PR is related to install and requires a corresponding change to the operator.

@mazdakn mazdakn requested a review from a team as a code owner September 19, 2024 00:42
@marvin-tigera marvin-tigera added this to the Calico v3.30.0 milestone Sep 19, 2024
@marvin-tigera marvin-tigera added release-note-required Change has user-facing impact (no matter how small) docs-pr-required Change is not yet documented labels Sep 19, 2024
@mazdakn mazdakn mentioned this pull request Sep 19, 2024
3 tasks
@mazdakn mazdakn requested a review from fasaxc September 20, 2024 01:11
if err != nil {
return
}
proto := kapiv1.ProtocolTCP
Copy link
Member Author

Choose a reason for hiding this comment

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

I think we only need to set this to make our parsing work, otherwise the protocol value does not matter. @fasaxc is this correct? There is no protocol field in the named port, and AFAIK, this is just name that will be matched with a named port.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should need a protocol for a named port. In NP there's an implicit default of TCP but there's no field in ANP so I think the idea is to match the named port, whatever protocol it uses. so, you could match "dns" and that will match pods that use TCP or UDP in their named port. so, I think you should set proto to nil and handle that further up the stack if needed.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if the dataplane can handle named ports with no protocol match. Is an IP set port match allowed when there's no protocol specified?

We could work around that by generating a rule per protocol for UDP, TCP, SCTP but obviously that wouldn't be ideal.

libcalico-go/lib/backend/k8s/conversion/conversion.go Outdated Show resolved Hide resolved
if err != nil {
return
}
proto := kapiv1.ProtocolTCP
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should need a protocol for a named port. In NP there's an implicit default of TCP but there's no field in ANP so I think the idea is to match the named port, whatever protocol it uses. so, you could match "dns" and that will match pods that use TCP or UDP in their named port. so, I think you should set proto to nil and handle that further up the stack if needed.

if err != nil {
return
}
proto := kapiv1.ProtocolTCP
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if the dataplane can handle named ports with no protocol match. Is an IP set port match allowed when there's no protocol specified?

We could work around that by generating a rule per protocol for UDP, TCP, SCTP but obviously that wouldn't be ideal.

protocolPorts[""] = []numorstring.Port{*calicoPort}
continue
}

Copy link
Member Author

@mazdakn mazdakn Sep 20, 2024

Choose a reason for hiding this comment

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

@fasaxc Originally I handled it like this, but if a rule with both named ports and other types, this would generate a separate rule for named ports. Is this fine?

Copy link
Member

Choose a reason for hiding this comment

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

I think that's the correct interpretation. Question is: does the dataplane handle that correctly (I'm not sure if you can match on ports without specifying a protocol so possible we fail or default it to TCP or something)?

Copy link
Member Author

Choose a reason for hiding this comment

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

The conformance tests are passing, and those tests include named ports with both udp and tcp. From what I've seen, it seems the protocol does not matter for named ports. The underlying struct does not include a protocol value.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks we default to TCP here:

namedPortProto := labelindex.ProtocolTCP

Also the conformance tests only include tcp protocols case. I think we should default to tcp here, and explicitly state that in our docs. WDYT?

@mazdakn mazdakn requested a review from fasaxc September 20, 2024 16:06
@mazdakn mazdakn changed the title Support AdminNetworkPolicy extensions Support AdminNetworkPolicy named ports Oct 2, 2024
// TODO: Add support for NamedPorts
if port.NamedPort != nil {
dstPort, err = k8sAdminPolicyNamedPortToCalico(*port.NamedPort)
proto := numorstring.ProtocolFromString(numorstring.ProtocolTCP)
Copy link
Member Author

Choose a reason for hiding this comment

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

We default to tcp in Felix for named ports.

@mazdakn mazdakn added docs-not-required Docs not required for this change and removed docs-pr-required Change is not yet documented labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required Docs not required for this change release-note-required Change has user-facing impact (no matter how small)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants