libnet: Return proper error when overlay network can't be created

The commit befff0e13f inadvertendly
disabled the error returned when trying to create an overlay network on
a node which is not part of a Swarm cluster.

Since commit e3708a89cc the overlay
netdriver returns the error: `no VNI provided`.

This commit reinstate the original error message by checking if the node
is a manager before calling libnetwork's `controller.NewNetwork()`.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
This commit is contained in:
Albin Kerouanton 2023-07-08 15:19:04 +02:00
parent c57097bcd4
commit 21dcbada2d
No known key found for this signature in database
GPG key ID: 630B8E1DCBDB1864

View file

@ -293,6 +293,16 @@ func (daemon *Daemon) createNetwork(cfg *config.Config, create types.NetworkCrea
return nil, PredefinedNetworkError(create.Name)
}
c := daemon.netController
driver := create.Driver
if driver == "" {
driver = c.Config().DefaultDriver
}
if driver == "overlay" && !daemon.cluster.IsManager() && !agent {
return nil, errors.New(`This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.`)
}
var warning string
nw, err := daemon.GetNetworkByName(create.Name)
if err != nil {
@ -311,12 +321,6 @@ func (daemon *Daemon) createNetwork(cfg *config.Config, create types.NetworkCrea
warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
}
c := daemon.netController
driver := create.Driver
if driver == "" {
driver = c.Config().DefaultDriver
}
networkOptions := make(map[string]string)
for k, v := range create.Options {
networkOptions[k] = v
@ -375,10 +379,6 @@ func (daemon *Daemon) createNetwork(cfg *config.Config, create types.NetworkCrea
n, err := c.NewNetwork(driver, create.Name, id, nwOptions...)
if err != nil {
if errors.Is(err, libnetwork.ErrDataStoreNotInitialized) {
//nolint: revive
return nil, errors.New(`This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.`)
}
return nil, err
}