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>
(cherry picked from commit 21dcbada2d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Albin Kerouanton 2023-07-08 15:19:04 +02:00 committed by Sebastiaan van Stijn
parent deea880581
commit a5c0fda157
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -291,6 +291,16 @@ func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string
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 {
@ -309,12 +319,6 @@ func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string
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
@ -373,10 +377,6 @@ func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string
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
}