Skip to content

Commit b25af8e

Browse files
committed
feat(iptables): be able to override iptables-1.4-compatible lock path
1 parent 66334f0 commit b25af8e

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
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_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)