Skip to content

Commit 7e41456

Browse files
committed
Move netutil flock to subdirectory
Signed-off-by: apostasie <[email protected]>
1 parent 1673252 commit 7e41456

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

pkg/lockutil/lockutil_unix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
func WithDirLock(dir string, fn func() error) error {
31+
_ = os.MkdirAll(dir, 0700)
3132
dirFile, err := os.Open(dir)
3233
if err != nil {
3334
return err
@@ -55,6 +56,7 @@ func flock(f *os.File, flags int) error {
5556
}
5657

5758
func Lock(dir string) (*os.File, error) {
59+
_ = os.MkdirAll(dir, 0700)
5860
dirFile, err := os.Open(dir)
5961
if err != nil {
6062
return nil, err

pkg/netutil/netutil.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func (e *CNIEnv) ListNetworksMatch(reqs []string, allowPseudoNetwork bool) (list
5656
var err error
5757

5858
var networkConfigs []*NetworkConfig
59-
err = lockutil.WithDirLock(e.NetconfPath, func() error {
59+
// NOTE: we cannot lock NetconfPath directly, as Cilium (maybe others) are also locking it.
60+
err = lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), func() error {
6061
networkConfigs, err = e.networkConfigList()
6162
return err
6263
})
@@ -220,7 +221,7 @@ func (e *CNIEnv) NetworkList() ([]*NetworkConfig, error) {
220221
netConfigList, err = e.networkConfigList()
221222
return err
222223
}
223-
err = lockutil.WithDirLock(e.NetconfPath, fn)
224+
err = lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), fn)
224225

225226
return netConfigList, err
226227
}
@@ -336,7 +337,7 @@ func (e *CNIEnv) CreateNetwork(opts types.NetworkCreateOptions) (*NetworkConfig,
336337
}
337338
return e.writeNetworkConfig(netConf)
338339
}
339-
err := lockutil.WithDirLock(e.NetconfPath, fn)
340+
err := lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), fn)
340341
if err != nil {
341342
return nil, err
342343
}
@@ -350,7 +351,7 @@ func (e *CNIEnv) RemoveNetwork(net *NetworkConfig) error {
350351
}
351352
return net.clean()
352353
}
353-
return lockutil.WithDirLock(e.NetconfPath, fn)
354+
return lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), fn)
354355
}
355356

356357
// GetDefaultNetworkConfig checks whether the default network exists

pkg/netutil/netutil_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ func testDefaultNetworkCreation(t *testing.T) {
196196
}
197197
err = filepath.Walk(cniConfTestDir, walkF)
198198
assert.NilError(t, err)
199-
assert.Assert(t, len(files) == 2) // files[0] is the entry for '.'
200-
assert.Assert(t, filepath.Join(cniConfTestDir, files[1].Name()) == defaultNetConf.File)
201-
assert.Assert(t, firstConfigModTime.Equal(files[1].ModTime()))
199+
assert.Equal(t, len(files), 3) // files[0] is the entry for '.', files[1] is the lock
200+
assert.Assert(t, filepath.Join(cniConfTestDir, files[2].Name()) == defaultNetConf.File)
201+
assert.Assert(t, firstConfigModTime.Equal(files[2].ModTime()))
202202
}
203203

204204
// Tests whether nerdctl properly creates the default network
@@ -295,9 +295,9 @@ func testDefaultNetworkCreationWithBridgeIP(t *testing.T) {
295295
}
296296
err = filepath.Walk(cniConfTestDir, walkF)
297297
assert.NilError(t, err)
298-
assert.Assert(t, len(files) == 2) // files[0] is the entry for '.'
299-
assert.Assert(t, filepath.Join(cniConfTestDir, files[1].Name()) == defaultNetConf.File)
300-
assert.Assert(t, firstConfigModTime.Equal(files[1].ModTime()))
298+
assert.Equal(t, len(files), 3) // files[0] is the entry for '.', files[1] is the lock
299+
assert.Assert(t, filepath.Join(cniConfTestDir, files[2].Name()) == defaultNetConf.File)
300+
assert.Assert(t, firstConfigModTime.Equal(files[2].ModTime()))
301301
}
302302

303303
// Tests whether nerdctl skips the creation of the default network if a

pkg/ocihook/ocihook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func Run(stdin io.Reader, stderr io.Writer, event, dataStore, cniPath, cniNetcon
107107
if err != nil {
108108
return err
109109
}
110-
lock, err := lockutil.Lock(cniNetconfPath)
110+
lock, err := lockutil.Lock(filepath.Join(cniNetconfPath, ".nerdctl.lock"))
111111
if err != nil {
112112
return err
113113
}

0 commit comments

Comments
 (0)