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
put block/unblock network function into util #20458
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2939,3 +2939,57 @@ func ensureGCELoadBalancerResourcesDeleted(ip, portRange string) error { | |
return true, nil | ||
}) | ||
} | ||
|
||
// The following helper functions can block/unblock network from source | ||
// host to destination host by manipulating iptable rules. | ||
// This function assumes it can ssh to the source host. | ||
// | ||
// Caution: | ||
// Recommend to input IP instead of hostnames. Using hostnames will cause iptables to | ||
// do a DNS lookup to resolve the name to an IP address, which will | ||
// slow down the test and cause it to fail if DNS is absent or broken. | ||
// | ||
// Suggested usage pattern: | ||
// func foo() { | ||
// ... | ||
// defer unblockNetwork(from, to) | ||
// blockNetwork(from, to) | ||
// ... | ||
// } | ||
// | ||
func blockNetwork(from string, to string) { | ||
Logf("block network traffic from %s to %s", from, to) | ||
iptablesRule := fmt.Sprintf("OUTPUT --destination %s --jump REJECT", to) | ||
dropCmd := fmt.Sprintf("sudo iptables --insert %s", iptablesRule) | ||
if result, err := SSH(dropCmd, from, testContext.Provider); result.Code != 0 || err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: probably 100% of the people who use this are going to poll till node NotReady right after, and then poll till NodeReady after recovery, so maybe encapsulate that too? |
||
LogSSHResult(result) | ||
Failf("Unexpected error: %v", err) | ||
} | ||
} | ||
|
||
func unblockNetwork(from string, to string) { | ||
Logf("Unblock network traffic from %s to %s", from, to) | ||
iptablesRule := fmt.Sprintf("OUTPUT --destination %s --jump REJECT", to) | ||
undropCmd := fmt.Sprintf("sudo iptables --delete %s", iptablesRule) | ||
// Undrop command may fail if the rule has never been created. | ||
// In such case we just lose 30 seconds, but the cluster is healthy. | ||
// But if the rule had been created and removing it failed, the node is broken and | ||
// not coming back. Subsequent tests will run or fewer nodes (some of the tests | ||
// may fail). Manual intervention is required in such case (recreating the | ||
// cluster solves the problem too). | ||
err := wait.Poll(time.Millisecond*100, time.Second*30, func() (bool, error) { | ||
result, err := SSH(undropCmd, from, testContext.Provider) | ||
if result.Code == 0 && err == nil { | ||
return true, nil | ||
} | ||
LogSSHResult(result) | ||
if err != nil { | ||
Logf("Unexpected error: %v", err) | ||
} | ||
return false, nil | ||
}) | ||
if err != nil { | ||
Failf("Failed to remove the iptable REJECT rule. Manual intervention is "+ | ||
"required on host %s: remove rule %s, if exists", from, iptablesRule) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a word of caution around using dns names?