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

Updating kube-proxy to trim space from loadBalancerSourceRanges #94107

Merged

Conversation

robscott
Copy link
Member

@robscott robscott commented Aug 19, 2020

What type of PR is this?
/kind bug

What this PR does / why we need it:
This updates kube-proxy to trim any space from loadBalancerSourceRanges to match validation. A better long term fix will require updating Service validation for future Kubernetes versions to not trim spaces before validation.

Special notes for your reviewer:
This feels like the kind of fix that should be part of 1.19 and potentially backported to older Kubernetes versions.

Does this PR introduce a user-facing change?:

kube-proxy now trims extra spaces found in loadBalancerSourceRanges to match Service validation.

/sig network
/priority critical-urgent
/assign @bowei

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. sig/network Categorizes an issue or PR as relevant to SIG Network. priority/critical-urgent Highest priority. Must be actively worked on as someone's top priority right now. labels Aug 19, 2020
Before this fix, a Service with a loadBalancerSourceRange value that
included a space would cause kube-proxy to crashloop. This updates
kube-proxy to trim any space from that field.
@robscott robscott force-pushed the kube-proxy-source-ranges-fix branch from 95f4348 to c382c79 Compare August 21, 2020 01:21
@robscott
Copy link
Member Author

@bowei I've added another check and test now for better coverage here. Now if syncProxyRules ever runs into an invalid CIDR it simply drops it - this happens after TrimSpace is called so extra spaces do not invalidate a CIDR. I don't think this should ever get hit though. The validation for this field actually trims spaces before running validation. Whether or not that's a good idea I'm not sure, but we were not doing that in kube-proxy as a consumer of the API. Changing the core validation would require a much longer release cycle and would likely be relatively difficult since the actual code that should change is in the utils repo.

If you're curious, here's the validation flow:

  1. Core Service validation calls GetLoadBalancerSourceRanges
  2. GetLoadBalancerSourceRanges calls utilnet.ParseIPNets
  3. utilnet.ParseIPNets call strings.TrimSpace before validating.

@bowei
Copy link
Member

bowei commented Aug 25, 2020

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 25, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bowei, robscott

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 25, 2020
@aojea
Copy link
Member

aojea commented Aug 25, 2020

shouldn't we caught this in validation?

// Validate SourceRange field and annotation
_, ok := service.Annotations[core.AnnotationLoadBalancerSourceRangesKey]
if len(service.Spec.LoadBalancerSourceRanges) > 0 || ok {
var fieldPath *field.Path
var val string
if len(service.Spec.LoadBalancerSourceRanges) > 0 {
fieldPath = specPath.Child("LoadBalancerSourceRanges")
val = fmt.Sprintf("%v", service.Spec.LoadBalancerSourceRanges)
} else {
fieldPath = field.NewPath("metadata", "annotations").Key(core.AnnotationLoadBalancerSourceRangesKey)
val = service.Annotations[core.AnnotationLoadBalancerSourceRangesKey]
}
if service.Spec.Type != core.ServiceTypeLoadBalancer {
allErrs = append(allErrs, field.Forbidden(fieldPath, "may only be used when `type` is 'LoadBalancer'"))
}
_, err := apiservice.GetLoadBalancerSourceRanges(service)
if err != nil {
allErrs = append(allErrs, field.Invalid(fieldPath, val, "must be a list of IP ranges. For example, 10.240.0.0/24,10.250.0.0/24 "))
}
}

@robscott
Copy link
Member Author

@aojea you would think so, but unfortunately not. Outlined the flow in #94107 (comment). Unfortunately strings.TrimSpace is called before any meaningful validation is run but not before kube-proxy used the same field.

@bowei
Copy link
Member

bowei commented Aug 25, 2020

The validation function is technically wrong -- validation should not be mutating the input before validating. An IP address with extra whitespace is not a valid IP address.
@robscott will file an issue to track potentially fixing that behavior. It's likely complicated to fix due to how old it is in the codebase.

@aojea
Copy link
Member

aojea commented Aug 25, 2020

yeah, didn't mean to ask for changes. I was curious about what will be the "ideal" solution

An IP address with extra whitespace is not a valid IP address.

... , and I was thinking if validation should reject the IP as invalid ...

It's likely complicated to fix due to how old it is in the codebase.

absolutely agree on this

@justaugustus justaugustus modified the milestones: v1.20-phase-bug, v1.20 Aug 26, 2020
@robscott
Copy link
Member Author

robscott commented Sep 1, 2020

/retest

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

2 similar comments
@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@robscott
Copy link
Member Author

robscott commented Sep 1, 2020

/retest

@liggitt
Copy link
Member

liggitt commented Sep 1, 2020

@cpanato
Copy link
Member

cpanato commented Oct 7, 2020

@robscott @bowei do we need to backport this to 1.18 and 1.17 as well?

@robscott
Copy link
Member Author

robscott commented Oct 7, 2020

@cpanato @bowei Created PRs to backport this to 1.18 and 1.17 as well.

k8s-ci-robot added a commit that referenced this pull request Nov 3, 2020
…07-upstream-release-1.19

Automated cherry pick of #94107: Updating kube-proxy to trim space from
k8s-ci-robot added a commit that referenced this pull request Nov 3, 2020
…07-upstream-release-1.18

Automated cherry pick of #94107: Updating kube-proxy to trim space from
k8s-ci-robot added a commit that referenced this pull request Nov 3, 2020
…07-upstream-release-1.17

Automated cherry pick of #94107: Updating kube-proxy to trim space from
aanm added a commit to aanm/cilium that referenced this pull request Nov 13, 2020
Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")
Signed-off-by: André Martins <andre@cilium.io>
christarazi pushed a commit to cilium/cilium that referenced this pull request Nov 14, 2020
Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")
Signed-off-by: André Martins <andre@cilium.io>
kaworu pushed a commit to cilium/cilium that referenced this pull request Nov 17, 2020
[ upstream commit ada413f ]

Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")

v1.8 backport: fixed a trivial conflict at pkg/k8s/service.go:117

Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Alexandre Perrin <alex@kaworu.ch>
kaworu pushed a commit to cilium/cilium that referenced this pull request Nov 17, 2020
[ upstream commit ada413f ]

Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")
Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Alexandre Perrin <alex@kaworu.ch>
borkmann pushed a commit to cilium/cilium that referenced this pull request Nov 18, 2020
[ upstream commit ada413f ]

Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")
Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Alexandre Perrin <alex@kaworu.ch>
kaworu pushed a commit to cilium/cilium that referenced this pull request Nov 19, 2020
[ upstream commit ada413f ]

Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")

v1.8 backport: fixed a trivial conflict at pkg/k8s/service.go:117

Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Alexandre Perrin <alex@kaworu.ch>
aanm added a commit to cilium/cilium that referenced this pull request Nov 26, 2020
[ upstream commit ada413f ]

Similarly to what is being done in upstream kube-proxy [1], but
unfortunately without explaining why, loadBalancerSourceRanges might
contain spaces which prevents the CIDR from being parsed correctly.

[1] kubernetes/kubernetes#94107

Fixes: 3195681 ("k8s: Add and parse LoadBalancerSourceRanges field")

v1.8 backport: fixed a trivial conflict at pkg/k8s/service.go:117

Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Alexandre Perrin <alex@kaworu.ch>
@robscott robscott deleted the kube-proxy-source-ranges-fix branch March 11, 2021 04:55
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. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/critical-urgent Highest priority. Must be actively worked on as someone's top priority right now. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/network Categorizes an issue or PR as relevant to SIG Network. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants