Skip to content

Commit

Permalink
Merge pull request #3491 from apostasie/bug-proactive-net-lock
Browse files Browse the repository at this point in the history
Make CreateNetwork safer wrt concurrency
  • Loading branch information
AkihiroSuda authored Oct 4, 2024
2 parents 8a06614 + bd1eefa commit 251b95f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,17 @@ type cniNetworkConfig struct {

func (e *CNIEnv) CreateNetwork(opts types.NetworkCreateOptions) (*NetworkConfig, error) { //nolint:revive
var net *NetworkConfig
netMap, err := e.NetworkMap()
if err != nil {
return nil, err
}

if _, ok := netMap[opts.Name]; ok {
return nil, errdefs.ErrAlreadyExists
}

fn := func() error {
netMap, err := e.NetworkMap()
if err != nil {
return err
}

if _, ok := netMap[opts.Name]; ok {
return errdefs.ErrAlreadyExists
}

ipam, err := e.generateIPAM(opts.IPAMDriver, opts.Subnets, opts.Gateway, opts.IPRange, opts.IPAMOptions, opts.IPv6)
if err != nil {
return err
Expand All @@ -277,7 +278,7 @@ func (e *CNIEnv) CreateNetwork(opts types.NetworkCreateOptions) (*NetworkConfig,
}
return e.writeNetworkConfig(net)
}
err = lockutil.WithDirLock(e.NetconfPath, fn)
err := lockutil.WithDirLock(e.NetconfPath, fn)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 251b95f

Please sign in to comment.