-
Notifications
You must be signed in to change notification settings - Fork 286
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
Grab the iptables lock before running iptables-restore. #1491
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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 |
---|---|---|
|
@@ -140,6 +140,20 @@ func (p *SecondsParam) Parse(raw string) (result interface{}, err error) { | |
return | ||
} | ||
|
||
type MillisParam struct { | ||
Metadata | ||
} | ||
|
||
func (p *MillisParam) Parse(raw string) (result interface{}, err error) { | ||
millis, err := strconv.ParseFloat(raw, 64) | ||
if err != nil { | ||
err = p.parseFailed(raw, "invalid float") | ||
return | ||
} | ||
result = time.Duration(millis * float64(time.Millisecond)) | ||
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. Is this cast needed? I'm curious to understand, if so. |
||
return | ||
} | ||
|
||
type RegexpParam struct { | ||
Metadata | ||
Regexp *regexp.Regexp | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
fv.test |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2017 Tigera, Inc. All rights reserved. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// The fv packge contains FV tests that execute Felix for-real. | ||
package fv |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2017 Tigera, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fv_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
"github.com/onsi/ginkgo/reporters" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/projectcalico/libcalico-go/lib/testutils" | ||
) | ||
|
||
func init() { | ||
testutils.HookLogrusForGinkgo() | ||
} | ||
|
||
func TestFv(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
junitReporter := reporters.NewJUnitReporter("junit.xml") | ||
RunSpecsWithDefaultAndCustomReporters(t, "FV Suite", []Reporter{junitReporter}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2017 Tigera, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/docopt/docopt-go" | ||
|
||
"github.com/projectcalico/felix/iptables" | ||
) | ||
|
||
const usage = `iptables-locker, test tool for grabbing the iptables lock. | ||
|
||
Usage: | ||
iptables-locker <duration> | ||
|
||
` | ||
|
||
func main() { | ||
arguments, err := docopt.Parse(usage, nil, true, "v0.1", false) | ||
if err != nil { | ||
println(usage) | ||
log.WithError(err).Fatal("Failed to parse usage") | ||
} | ||
durationStr := arguments["<duration>"].(string) | ||
duration, err := time.ParseDuration(durationStr) | ||
if err != nil { | ||
println(usage) | ||
log.WithError(err).Fatal("Failed to parse usage") | ||
} | ||
|
||
iptablesLock := iptables.NewSharedLock( | ||
"/run/xtables.lock", | ||
1*time.Second, | ||
50*time.Millisecond, | ||
) | ||
iptablesLock.Lock() | ||
println("LOCKED") | ||
time.Sleep(duration) | ||
iptablesLock.Unlock() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// +build fvtests | ||
|
||
// Copyright (c) 2017 Tigera, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fv_test | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"time" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("with running container", func() { | ||
var containerIdx int | ||
var containerName string | ||
var felixCmd *exec.Cmd | ||
|
||
cmdInContainer := func(cmd ...string) *exec.Cmd { | ||
arg := []string{"exec", containerName} | ||
arg = append(arg, cmd...) | ||
return exec.Command("docker", arg...) | ||
} | ||
|
||
BeforeEach(func() { | ||
containerName = fmt.Sprintf("felix-fv-%d-%d", os.Getpid(), containerIdx) | ||
containerIdx++ | ||
myDir, err := os.Getwd() | ||
Expect(err).NotTo(HaveOccurred()) | ||
log.WithFields(log.Fields{ | ||
"name": containerName, | ||
"myDir": myDir, | ||
}).Info("Starting a Felix container") | ||
// Run a felix container. The tests in this file don't actually rely on Felix | ||
// but the calico/felix container has all the iptables dependencies we need to | ||
// check the lock behaviour. Note: we don't map the host's iptables lock into the | ||
// container so the scope of the lock is limited to the container. | ||
felixCmd = exec.Command("docker", "run", | ||
"--rm", | ||
"--name", containerName, | ||
"-v", fmt.Sprintf("%s/..:/codebase", myDir), | ||
"--privileged", | ||
"calico/felix") | ||
err = felixCmd.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
log.Info("Waiting for container to be listed in docker ps") | ||
start := time.Now() | ||
for { | ||
cmd := exec.Command("docker", "ps") | ||
out, err := cmd.CombinedOutput() | ||
Expect(err).NotTo(HaveOccurred()) | ||
if strings.Contains(string(out), containerName) { | ||
break | ||
} | ||
if time.Since(start) > 10*time.Second { | ||
log.Panic("Timed out waiting for container to be listed.") | ||
} | ||
} | ||
}) | ||
AfterEach(func() { | ||
// Send an interrupt to ensure that docker gracefully shuts down the container. | ||
// If we kill the docker process then it detaches the container. | ||
log.Info("Stopping Felix container") | ||
felixCmd.Process.Signal(os.Interrupt) | ||
}) | ||
|
||
Describe("with the lock being held for 2s", func() { | ||
var lockCmd *exec.Cmd | ||
BeforeEach(func() { | ||
// Start the iptables-locker, which is a simple test app that locks | ||
// the iptables lock and then releases it after a timeout. | ||
log.Info("Starting iptables-locker") | ||
lockCmd = cmdInContainer("/codebase/bin/iptables-locker", "2s") | ||
stdErr, err := lockCmd.StderrPipe() | ||
Expect(err).NotTo(HaveOccurred()) | ||
lockCmd.Start() | ||
|
||
// Wait for the iptables-locker to tell us that it actually acquired the | ||
// lock. | ||
log.Info("Waiting for iptables-locker to acquire lock") | ||
scanner := bufio.NewScanner(stdErr) | ||
Expect(scanner.Scan()).To(BeTrue()) | ||
Expect(scanner.Text()).To(Equal("LOCKED")) | ||
Expect(scanner.Err()).NotTo(HaveOccurred()) | ||
log.Info("iptables-locker acquired lock") | ||
}) | ||
|
||
It("iptables should fail to get the lock in 1s", func() { | ||
iptCmd := cmdInContainer("iptables", "-w", "1", "-A", "FORWARD") | ||
out, err := iptCmd.CombinedOutput() | ||
Expect(string(out)).To(ContainSubstring("Stopped waiting")) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("iptables should succeed in getting the lock after 3s", func() { | ||
iptCmd := cmdInContainer("iptables", "-w", "3", "-A", "FORWARD") | ||
out, err := iptCmd.CombinedOutput() | ||
Expect(string(out)).To(ContainSubstring("Another app is currently holding the xtables lock")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
if lockCmd != nil { | ||
log.Info("waiting for iptables-locker to finish") | ||
err := lockCmd.Wait() | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
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.
Is this cast needed? I'm curious to understand, if so.
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.
Yes, it is. I want to allow the input to be a float, so you could say
1.2
(milliseconds). Go's casting rules are very strict so you can't dofloat * duration
(which is really an int64 number of nanoseconds) without a cast. Then I need to cast back totime.Duration
at the end.Casting millis to
time.Duration
first then multiplying doesn't work because that would lose precision.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.
That makes sense - thanks.