Skip to content

Commit 9391a66

Browse files
committed
host-local: still allocate empty file to avoid IP leak
It may happen when host-local creates an empty file successfully, but fails to write string into it and further fails to remove it. Many empty file will be created if runtime retries to create a new container until all free IPs are leaked in the range set. The root cause is that host-local cannot handle this kind of error when performing a rollback. We should check if IP file is empty, this prevents exhausting all free IPs and helps to recover leaked IP files. Fix #498
1 parent 1fb9793 commit 9391a66

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

plugins/ipam/host-local/backend/disk/backend.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,24 @@ func New(network, dataDir string) (*Store, error) {
5959
func (s *Store) Reserve(id string, ifname string, ip net.IP, rangeID string) (bool, error) {
6060
fname := GetEscapedPath(s.dataDir, ip.String())
6161

62-
f, err := os.OpenFile(fname, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
62+
var f *os.File
63+
var err error
64+
65+
f, err = os.OpenFile(fname, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
6366
if os.IsExist(err) {
64-
return false, nil
67+
// file fname exists and f is nil, try to reopen it without O_CREATE flag.
68+
f, err = os.OpenFile(fname, os.O_RDWR, 0644)
69+
if err != nil {
70+
return false, err
71+
}
72+
statInfo, err := f.Stat()
73+
if err != nil {
74+
return false, err
75+
}
76+
// non-empty file means it is already occupied by a container. Otherwise we can still allocate it to a new container.
77+
if statInfo.Size() != 0 {
78+
return false, nil
79+
}
6580
}
6681
if err != nil {
6782
return false, err

0 commit comments

Comments
 (0)