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

Bug 1841260: Make resolv.conf prepend operation atomic #1763

Merged
merged 3 commits into from May 29, 2020

Conversation

cybertron
Copy link
Member

This PR includes two changes:

  1. It makes resolv.conf prepending atomic. This prevents a potential problem
    in case the sed is interrupted, which could leave an empty resolv.conf.

  2. It moves the baremetal dispatcher script to common. With the removal of the
    DNS VIP, there is no longer any difference between the master and worker
    versions of this. This can't be done for the other platforms yet because they
    still rely on the DNS VIP.

- Description for the changelog
Make resolv.conf prepend operation atomic to prevent potential race conditions.

@openshift-ci-robot openshift-ci-robot added bugzilla/severity-medium Referenced Bugzilla bug's severity is medium for the branch this PR is targeting. bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. labels May 28, 2020
@openshift-ci-robot
Copy link
Contributor

@cybertron: This pull request references Bugzilla bug 1841260, which is valid. The bug has been moved to the POST state. The bug has been updated to refer to the pull request using the external bug tracker.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target release (4.5.0) matches configured target release for branch (4.5.0)
  • bug is in the state ASSIGNED, which is one of the valid states (NEW, ASSIGNED, ON_DEV, POST, POST)

In response to this:

Bug 1841260: Make resolv.conf prepend operation atomic

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.

@cybertron
Copy link
Member Author

/cc @celebdor @stbenjam @mandre @rgolangh @patrickdillon
/test e2e-openstack

@@ -25,7 +25,8 @@ contents:
logger -s "NM resolv-prepender: Prepending 'nameserver $NAMESERVER_IP' to /etc/resolv.conf (other nameservers from /var/run/NetworkManager/resolv.conf)"
sed -e "/^search/d" \
-e "/Generated by/c# Generated by KNI resolv prepender NM dispatcher script\nsearch $DOMAIN\nnameserver $NAMESERVER_IP" \
/var/run/NetworkManager/resolv.conf > /etc/resolv.conf
/var/run/NetworkManager/resolv.conf > /etc/resolv.tmp
mv -f /etc/resolv.tmp /etc/resolv.conf
Copy link
Member

Choose a reason for hiding this comment

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

There's a sed below at well that limits us to the top 3 nameservers. Should we do it all (including that sed) in a temp file and then copy the file result?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, we should. That fits well with cgwalters suggestion below as well.

@stbenjam
Copy link
Member

@cgwalters reccomended using set -e and bash strict mode.

Maybe we could also run the script through shellcheck just for good measure?

@@ -25,7 +25,8 @@ contents:
logger -s "NM resolv-prepender: Prepending 'nameserver $NAMESERVER_IP' to /etc/resolv.conf (other nameservers from /var/run/NetworkManager/resolv.conf)"
sed -e "/^search/d" \
-e "/Generated by/c# Generated by KNI resolv prepender NM dispatcher script\nsearch $DOMAIN\nnameserver $NAMESERVER_IP" \
/var/run/NetworkManager/resolv.conf > /etc/resolv.conf
/var/run/NetworkManager/resolv.conf > /etc/resolv.tmp
mv -f /etc/resolv.tmp /etc/resolv.conf
else
logger -s "NM resolv-prepender: Couldn't find a Virtual IP, just updating resolv.conf"
cp /var/run/NetworkManager/resolv.conf /etc/resolv.conf
Copy link
Member

Choose a reason for hiding this comment

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

cp is also not atomic - this is a really big trap that a whole lot of people have fallen into. You could probably unify these by having the second path do
cp /var/run/NetworkManager/resolv.conf /etc/resolv.tmp
and then running
mv -f /etc/resolv.tmp /etc/resolv.conf after the if.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. Will do.

@cybertron
Copy link
Member Author

Hmm, just noticed there is a set +e in here. I'm unsure why we would have done that. I don't see anything that would be expected to fail after that. :-/

Redirecting the output of sed directly to resolv.conf is not an
atomic operation. This is dangerous because resolv.conf will be
truncated before sed completes, which means if something happens
in the meantime resolv.conf will be empty.

Additionally, the cp command is not atomic either, so we want to
do that in a similar fashion.

This change moves the sed and cp operations to work on a temporary
file, then once everything is completed the temp file is moved to
/etc/resolv.conf, which is an atomic operation.
Since we dropped the DNS VIP, there is no longer any meaningful
difference between these two files.
If a command fails, we don't want to blindly continue with potentially
garbage data. Consider if the podman call to get the correct IP fails.
We might prepend nonsense to resolv.conf and it certainly won't do
what we want.

set -eo pipefail will cause the script to stop if anything fails.
We won't then mess with resolv.conf if we don't have the appropriate
data first.

Since we're now operating on a temp file, I also removed the set +e
from before the sed commands. I'm not entirely sure what it was
there in the first place, but it shouldn't be necessary now and I
don't think we want to continue if one of these commands fails.
@cgwalters
Copy link
Member

/approve

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 28, 2020
@cgwalters
Copy link
Member

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label May 28, 2020
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cgwalters, cybertron

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

@stbenjam
Copy link
Member

/test e2e-gcp-upgrade

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-ci-robot
Copy link
Contributor

@cybertron: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
ci/prow/e2e-metal-ipi 5be02dc link /test e2e-metal-ipi

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit d7813b9 into openshift:master May 29, 2020
@openshift-ci-robot
Copy link
Contributor

@cybertron: All pull requests linked via external trackers have merged: openshift/machine-config-operator#1763. Bugzilla bug 1841260 has been moved to the MODIFIED state.

In response to this:

Bug 1841260: Make resolv.conf prepend operation atomic

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.

mandre added a commit to mandre/machine-config-operator that referenced this pull request Jun 29, 2020
This aligned OpenStack and vSphere platforms with with the BM one after
openshift#1763 and openshift#1765.
mandre added a commit to mandre/machine-config-operator that referenced this pull request Jul 2, 2020
This aligned OpenStack and vSphere platforms with with the BM one after
openshift#1763 and openshift#1765.
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. bugzilla/severity-medium Referenced Bugzilla bug's severity is medium for the branch this PR is targeting. bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants