summaryrefslogtreecommitdiff
path: root/libpod/network/internal/util/create.go
blob: cecfd71331328fc82ba99c8a0ee6ad08685a2fb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package util

import (
	"github.com/containers/podman/v3/libpod/define"
	"github.com/containers/podman/v3/libpod/network/types"
	"github.com/pkg/errors"
)

func CommonNetworkCreate(n NetUtil, network *types.Network) error {
	if network.Labels == nil {
		network.Labels = map[string]string{}
	}
	if network.Options == nil {
		network.Options = map[string]string{}
	}
	if network.IPAMOptions == nil {
		network.IPAMOptions = map[string]string{}
	}

	var name string
	var err error
	// validate the name when given
	if network.Name != "" {
		if !define.NameRegex.MatchString(network.Name) {
			return errors.Wrapf(define.RegexError, "network name %s invalid", network.Name)
		}
		if _, err := n.Network(network.Name); err == nil {
			return errors.Wrapf(define.ErrNetworkExists, "network name %s already used", network.Name)
		}
	} else {
		name, err = GetFreeDeviceName(n)
		if err != nil {
			return err
		}
		network.Name = name
		// also use the name as interface name when we create a bridge network
		if network.Driver == types.BridgeNetworkDriver && network.NetworkInterface == "" {
			network.NetworkInterface = name
		}
	}
	return nil
}