Skip to content

Commit e5202ef

Browse files
authored
Merge pull request kubernetes#94553 from knight42/fix/TestRestoreAllWaitOldIptablesRestore
Deflake TestRestoreAllWaitOldIptablesRestore
2 parents 338d233 + ce0a423 commit e5202ef

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

pkg/util/iptables/iptables.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,12 @@ const WaitIntervalString = "-W"
186186
// WaitIntervalUsecondsValue a constant for specifying the default wait interval useconds
187187
const WaitIntervalUsecondsValue = "100000"
188188

189-
// LockfilePath16x is the iptables lock file acquired by any process that's making any change in the iptable rule
189+
// LockfilePath16x is the iptables 1.6.x lock file acquired by any process that's making any change in the iptable rule
190190
const LockfilePath16x = "/run/xtables.lock"
191191

192+
// LockfilePath14x is the iptables 1.4.x lock file acquired by any process that's making any change in the iptable rule
193+
const LockfilePath14x = "@xtables"
194+
192195
// runner implements Interface in terms of exec("iptables").
193196
type runner struct {
194197
mu sync.Mutex
@@ -198,20 +201,24 @@ type runner struct {
198201
hasRandomFully bool
199202
waitFlag []string
200203
restoreWaitFlag []string
201-
lockfilePath string
204+
lockfilePath14x string
205+
lockfilePath16x string
202206
}
203207

204208
// newInternal returns a new Interface which will exec iptables, and allows the
205209
// caller to change the iptables-restore lockfile path
206-
func newInternal(exec utilexec.Interface, protocol Protocol, lockfilePath string) Interface {
210+
func newInternal(exec utilexec.Interface, protocol Protocol, lockfilePath14x, lockfilePath16x string) Interface {
207211
version, err := getIPTablesVersion(exec, protocol)
208212
if err != nil {
209213
klog.Warningf("Error checking iptables version, assuming version at least %s: %v", MinCheckVersion, err)
210214
version = MinCheckVersion
211215
}
212216

213-
if lockfilePath == "" {
214-
lockfilePath = LockfilePath16x
217+
if lockfilePath16x == "" {
218+
lockfilePath16x = LockfilePath16x
219+
}
220+
if lockfilePath14x == "" {
221+
lockfilePath14x = LockfilePath14x
215222
}
216223

217224
runner := &runner{
@@ -221,14 +228,15 @@ func newInternal(exec utilexec.Interface, protocol Protocol, lockfilePath string
221228
hasRandomFully: version.AtLeast(RandomFullyMinVersion),
222229
waitFlag: getIPTablesWaitFlag(version),
223230
restoreWaitFlag: getIPTablesRestoreWaitFlag(version, exec, protocol),
224-
lockfilePath: lockfilePath,
231+
lockfilePath14x: lockfilePath14x,
232+
lockfilePath16x: lockfilePath16x,
225233
}
226234
return runner
227235
}
228236

229237
// New returns a new Interface which will exec iptables.
230238
func New(exec utilexec.Interface, protocol Protocol) Interface {
231-
return newInternal(exec, protocol, "")
239+
return newInternal(exec, protocol, "", "")
232240
}
233241

234242
// EnsureChain is part of Interface.
@@ -390,7 +398,7 @@ func (runner *runner) restoreInternal(args []string, data []byte, flush FlushFla
390398
// from stepping on each other. iptables-restore 1.6.2 will have
391399
// a --wait option like iptables itself, but that's not widely deployed.
392400
if len(runner.restoreWaitFlag) == 0 {
393-
locker, err := grabIptablesLocks(runner.lockfilePath)
401+
locker, err := grabIptablesLocks(runner.lockfilePath14x, runner.lockfilePath16x)
394402
if err != nil {
395403
return err
396404
}

pkg/util/iptables/iptables_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (l *locker) Close() error {
4949
return utilerrors.NewAggregate(errList)
5050
}
5151

52-
func grabIptablesLocks(lockfilePath string) (iptablesLocker, error) {
52+
func grabIptablesLocks(lockfilePath14x, lockfilePath16x string) (iptablesLocker, error) {
5353
var err error
5454
var success bool
5555

@@ -66,9 +66,9 @@ func grabIptablesLocks(lockfilePath string) (iptablesLocker, error) {
6666
// can't assume which lock method it'll use.
6767

6868
// Roughly duplicate iptables 1.6.x xtables_lock() function.
69-
l.lock16, err = os.OpenFile(lockfilePath, os.O_CREATE, 0600)
69+
l.lock16, err = os.OpenFile(lockfilePath16x, os.O_CREATE, 0600)
7070
if err != nil {
71-
return nil, fmt.Errorf("failed to open iptables lock %s: %v", lockfilePath, err)
71+
return nil, fmt.Errorf("failed to open iptables lock %s: %v", lockfilePath16x, err)
7272
}
7373

7474
if err := wait.PollImmediate(200*time.Millisecond, 2*time.Second, func() (bool, error) {
@@ -82,7 +82,7 @@ func grabIptablesLocks(lockfilePath string) (iptablesLocker, error) {
8282

8383
// Roughly duplicate iptables 1.4.x xtables_lock() function.
8484
if err := wait.PollImmediate(200*time.Millisecond, 2*time.Second, func() (bool, error) {
85-
l.lock14, err = net.ListenUnix("unix", &net.UnixAddr{Name: "@xtables", Net: "unix"})
85+
l.lock14, err = net.ListenUnix("unix", &net.UnixAddr{Name: lockfilePath14x, Net: "unix"})
8686
if err != nil {
8787
return false, nil
8888
}

pkg/util/iptables/iptables_test.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ import (
3535
fakeexec "k8s.io/utils/exec/testing"
3636
)
3737

38-
const TestLockfilePath = "xtables.lock"
38+
func getLockPaths() (string, string) {
39+
lock14x := fmt.Sprintf("@xtables-%d", time.Now().Nanosecond())
40+
lock16x := fmt.Sprintf("xtables-%d.lock", time.Now().Nanosecond())
41+
return lock14x, lock16x
42+
}
3943

4044
func testIPTablesVersionCmds(t *testing.T, protocol Protocol) {
4145
version := " v1.4.22"
@@ -934,8 +938,8 @@ func TestRestoreAll(t *testing.T) {
934938
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
935939
},
936940
}
937-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
938-
defer os.Remove(TestLockfilePath)
941+
lockPath14x, lockPath16x := getLockPaths()
942+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
939943

940944
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
941945
if err != nil {
@@ -975,8 +979,8 @@ func TestRestoreAllWait(t *testing.T) {
975979
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
976980
},
977981
}
978-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
979-
defer os.Remove(TestLockfilePath)
982+
lockPath14x, lockPath16x := getLockPaths()
983+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
980984

981985
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
982986
if err != nil {
@@ -1020,8 +1024,8 @@ func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
10201024
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
10211025
},
10221026
}
1023-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1024-
defer os.Remove(TestLockfilePath)
1027+
lockPath14x, lockPath16x := getLockPaths()
1028+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
10251029

10261030
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
10271031
if err != nil {
@@ -1065,24 +1069,23 @@ func TestRestoreAllGrabNewLock(t *testing.T) {
10651069
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
10661070
},
10671071
}
1068-
1069-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1070-
defer os.Remove(TestLockfilePath)
1072+
lockPath14x, lockPath16x := getLockPaths()
1073+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
10711074

10721075
// Grab the /run lock and ensure the RestoreAll fails
1073-
runLock, err := os.OpenFile(TestLockfilePath, os.O_CREATE, 0600)
1076+
runLock, err := os.OpenFile(lockPath16x, os.O_CREATE, 0600)
10741077
if err != nil {
1075-
t.Fatalf("expected to open %s, got %v", TestLockfilePath, err)
1078+
t.Fatalf("expected to open %s, got %v", lockPath16x, err)
10761079
}
10771080
defer runLock.Close()
10781081

10791082
if err := grabIptablesFileLock(runLock); err != nil {
1080-
t.Errorf("expected to lock %s, got %v", TestLockfilePath, err)
1083+
t.Errorf("expected to lock %s, got %v", lockPath16x, err)
10811084
}
10821085

10831086
err = runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
10841087
if err == nil {
1085-
t.Errorf("expected failure, got success instead")
1088+
t.Fatal("expected failure, got success instead")
10861089
}
10871090
if !strings.Contains(err.Error(), "failed to acquire new iptables lock: timed out waiting for the condition") {
10881091
t.Errorf("expected timeout error, got %v", err)
@@ -1107,22 +1110,21 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
11071110
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
11081111
},
11091112
}
1110-
1111-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1112-
defer os.Remove(TestLockfilePath)
1113+
lockPath14x, lockPath16x := getLockPaths()
1114+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
11131115

11141116
var runLock *net.UnixListener
11151117
// Grab the abstract @xtables socket, will retry if the socket exists
11161118
err := wait.PollImmediate(time.Second, wait.ForeverTestTimeout, func() (done bool, err error) {
1117-
runLock, err = net.ListenUnix("unix", &net.UnixAddr{Name: "@xtables", Net: "unix"})
1119+
runLock, err = net.ListenUnix("unix", &net.UnixAddr{Name: lockPath14x, Net: "unix"})
11181120
if err != nil {
1119-
t.Logf("Failed to lock @xtables: %v, will retry.", err)
1121+
t.Logf("Failed to lock %s: %v, will retry.", lockPath14x, err)
11201122
return false, nil
11211123
}
11221124
return true, nil
11231125
})
11241126
if err != nil {
1125-
t.Fatal("Timed out locking @xtables")
1127+
t.Fatalf("Timed out locking %s", lockPath14x)
11261128
}
11271129
if runLock == nil {
11281130
t.Fatal("Unexpected nil runLock")
@@ -1132,7 +1134,7 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
11321134

11331135
err = runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
11341136
if err == nil {
1135-
t.Errorf("expected failure, got success instead")
1137+
t.Fatal("expected failure, got success instead")
11361138
}
11371139
if !strings.Contains(err.Error(), "failed to acquire old iptables lock: timed out waiting for the condition") {
11381140
t.Errorf("expected timeout error, got %v", err)
@@ -1160,8 +1162,8 @@ func TestRestoreAllWaitBackportedIptablesRestore(t *testing.T) {
11601162
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
11611163
},
11621164
}
1163-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1164-
defer os.Remove(TestLockfilePath)
1165+
lockPath14x, lockPath16x := getLockPaths()
1166+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
11651167

11661168
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
11671169
if err != nil {

pkg/util/iptables/iptables_unsupported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"os"
2424
)
2525

26-
func grabIptablesLocks(lockfilePath string) (iptablesLocker, error) {
26+
func grabIptablesLocks(lock14filePath, lock16filePath string) (iptablesLocker, error) {
2727
return nil, fmt.Errorf("iptables unsupported on this platform")
2828
}
2929

0 commit comments

Comments
 (0)