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

Fix race condition in iptables partial sync handling #122204

Merged
merged 2 commits into from Dec 20, 2023

Conversation

danwinship
Copy link
Contributor

What type of PR is this?

/kind bug
/kind regression

What this PR does / why we need it:

Fixes a race condition in the iptables proxy partial sync handling; the methods that process apiserver events (proxier.OnServiceUpdated etc) rely on the change trackers' mutexes rather than the proxier's mutexes (since they don't use any mutable variables in the proxier), but syncProxyRules currently makes two separate calls into the change trackers, PendingUpdates() and Update(), so it's possible that an event could arrive between them, causing the two results to reflect inconsistent state. (#121362 (comment))

Which issue(s) this PR fixes:

Fixes #121362

Special notes for your reviewer:

This does not add any regression test since the bug is a race condition and I don't think there's any easy way we could force it?

Does this PR introduce a user-facing change?

Fixes a race condition in the iptables mode of kube-proxy in 1.27 and later
that could result in some updates getting lost (e.g., when a service gets a
new endpoint, the rules for the new endpoint might not be added until
much later).

/sig network
/priority important-soon
cc @aojea @juliantaylor

@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/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. labels Dec 6, 2023
@k8s-ci-robot
Copy link
Contributor

Please note that we're already in Test Freeze for the release-1.29 branch. This means every merged PR will be automatically fast-forwarded via the periodic ci-fast-forward job to the release branch of the upcoming v1.29.0 release.

Fast forwards are scheduled to happen every 6 hours, whereas the most recent run was: Wed Dec 6 10:11:59 UTC 2023.

@k8s-ci-robot k8s-ci-robot added sig/network Categorizes an issue or PR as relevant to SIG Network. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 6, 2023
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@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 approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kube-proxy labels Dec 6, 2023
func (cache *EndpointSliceCache) checkoutChanges() []*endpointsChange {
changes := []*endpointsChange{}
func (cache *EndpointSliceCache) checkoutChanges() map[types.NamespacedName]*endpointsChange {
changes := make(map[types.NamespacedName]*endpointsChange)
Copy link
Member

Choose a reason for hiding this comment

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

this is obtained from trackerByServiceMap so there is no risk of collapsing two different changes associated to the same service

for i, change := range changes {
expectedChange := tc.expectedChanges[i]
for _, change := range changes {
// The test only supports 0 or 1 changes, so if we're here,
Copy link
Member

Choose a reason for hiding this comment

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

I didn't check in detail , but is this because the test can not support more than 1 change or existing testcases only have 0 or 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the latter

@@ -1207,7 +1201,7 @@ func (proxier *Proxier) syncProxyRules() {
// them in activeNATChains, so they won't get deleted.) However, we have
// to still figure out how many chains we _would_ have written to make the
// metrics come out right, so we just compute them and throw them away.
if tryPartialSync && !serviceChanged.Has(svcName.NamespacedName.String()) && !endpointsChanged.Has(svcName.NamespacedName.String()) {
if tryPartialSync && !serviceUpdateResult.UpdatedServices.Has(svcName.NamespacedName) && !endpointUpdateResult.UpdatedServices.Has(svcName.NamespacedName) {
Copy link
Member

Choose a reason for hiding this comment

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

this seems much better and easier to think about

@aojea
Copy link
Member

aojea commented Dec 10, 2023

only one question about one test, but LGTM

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 14, 2023
@aojea
Copy link
Member

aojea commented Dec 18, 2023

/lgtm

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

LGTM label has been added.

Git tree hash: d37505ac6dbd12087479890dcc2c5a76946b3872

@aojea
Copy link
Member

aojea commented Dec 19, 2023

@danwinship the bot says it needs rebase

@danwinship
Copy link
Contributor Author

it doesn't; the tag is stale...
maybe if I rebase and repush it will notice this time...

ServicePortMap.Update() and EndpointsMap.Update() were just a tiny
wrappers around the corresponding apply() methods, which had no other
callers. So squash them together.

(Also fix the variable naming in ServicePortMap.Update() to match
other methods.)
…ults

This fixes a race condition where the tracker could be updated in
between us calling .PendingChanges() and .Update().
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 19, 2023
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 19, 2023
@danwinship
Copy link
Contributor Author

/retest-required

@aojea
Copy link
Member

aojea commented Dec 20, 2023

/lgtm

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

LGTM label has been added.

Git tree hash: b3c85bffa5dd1428437dc6809eece403275fd28f

@danwinship
Copy link
Contributor Author

/retest-required

@k8s-ci-robot k8s-ci-robot merged commit 247ea20 into kubernetes:master Dec 20, 2023
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.30 milestone Dec 20, 2023
@danwinship danwinship deleted the proxy-pending-changes branch December 20, 2023 20:29
@rikatz
Copy link
Contributor

rikatz commented Jan 13, 2024

/cherrypick release-1.28

k8s-ci-robot added a commit that referenced this pull request Feb 8, 2024
…04-upstream-release-1.27

Automated cherry pick of #122204: Fix race condition in iptables partial sync handling
k8s-ci-robot added a commit that referenced this pull request Feb 9, 2024
…04-upstream-release-1.28

Cherry pick of #122204: Fix race condition in iptables partial sync handling
k8s-ci-robot added a commit that referenced this pull request Feb 9, 2024
…04-upstream-release-1.29

Automated cherry pick of #122204: Fix race condition in iptables partial sync handling
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. area/kube-proxy cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. 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/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.

endpoints not configured with MinimizeIPTablesRestore
4 participants