Skip to content

Conversation

danwinship
Copy link
Contributor

Proof-of-concept, since I keep mentioning we could do this if we wanted. I'm not sure we do want to, but...

Note that we only use the regexps if the initial attempt at parsing fails, so we only take the regexp hit on invalid or legacy inputs.

/kind cleanup
/cc @aojea @thockin

@k8s-ci-robot k8s-ci-robot requested review from aojea and thockin August 20, 2025 23:58
@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 20, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: danwinship

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 20, 2025
@danwinship
Copy link
Contributor Author

/hold

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Aug 20, 2025
"regexp"
)

var matchZeros = regexp.MustCompile(`(^|:)0*(0|[1-9][0-9]*)\.0*(0|[1-9][0-9]*)\.0*(0|[1-9][0-9]*)\.0*(0|[1-9][0-9]*)((/)0*(0|[1-9][0-9]*))?$`)
Copy link
Member

Choose a reason for hiding this comment

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

I like the idea of being able to not just tolerate an IP using old style parsing, but actually transform it into the acceptable version, so that we could conceivably scrub existing data and stop accepting leading 0s via the API at some point in the future.

as nits, if we were going to do this for real, I'd want to split the IP and CIDR cases so we match exactly what we intend to for each scenario, and probably try to factor out the matcher for the leading-zero number to make the overall structure easier to see:

var (
  num = `0*(0|[1-9][0-9]*)`
  dot = `\.`
  
  leadingZeroIP = regexp.MustCompile(`(^|:)` + num + dot + num + dot + num + dot + num + `$`)
  leadingZeroIPReplace = `$1$2.$3.$4.$5`

  leadingZeroCIDR = regexp.MustCompile(`(^|:)` + num + dot + num + dot + num + dot + num + `/` + num + `$`)
  leadingZeroCIDRReplace = `$1$2.$3.$4.$5/$6`
)

I'd also consider whether we wanted to use these cleaners externally to detect a field was relying on this (so we could warn) or to do the transform (so we could normalize or clean up existing or incoming data)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to detect a field was relying on this (so we could warn)

KEP-4858 already took care of all of this (for non-CRD types). kubernetes/kubernetes#122550 split IP/CIDR validation into IsValid{IP,CIDR} for new fields vs IsValid{IP,CIDR}ForLegacyField, for older fields, and kubernetes/kubernetes#128786 added warnings when you're using a value with 0s in a legacy field.

As for transform/clean-up, that's been an ongoing discussion, most recently in #328 (comment) (which is the proximate cause of me filing this PR).

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 2, 2025
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants