summaryrefslogtreecommitdiff
path: root/cmd/podman/common/netflags.go
blob: 425d85c9d16a73136b7055436c947de4396b2472 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package common

import (
	"net"

	"github.com/containers/common/pkg/completion"
	"github.com/containers/podman/v3/cmd/podman/parse"
	"github.com/containers/podman/v3/libpod/define"
	"github.com/containers/podman/v3/libpod/network/types"
	"github.com/containers/podman/v3/pkg/domain/entities"
	"github.com/containers/podman/v3/pkg/specgen"
	"github.com/containers/podman/v3/pkg/specgenutil"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

func DefineNetFlags(cmd *cobra.Command) {
	netFlags := cmd.Flags()

	addHostFlagName := "add-host"
	netFlags.StringSlice(
		addHostFlagName, []string{},
		"Add a custom host-to-IP mapping (host:ip) (default [])",
	)
	_ = cmd.RegisterFlagCompletionFunc(addHostFlagName, completion.AutocompleteNone)

	dnsFlagName := "dns"
	netFlags.StringSlice(
		dnsFlagName, containerConfig.DNSServers(),
		"Set custom DNS servers",
	)
	_ = cmd.RegisterFlagCompletionFunc(dnsFlagName, completion.AutocompleteNone)

	dnsOptFlagName := "dns-opt"
	netFlags.StringSlice(
		dnsOptFlagName, containerConfig.DNSOptions(),
		"Set custom DNS options",
	)
	_ = cmd.RegisterFlagCompletionFunc(dnsOptFlagName, completion.AutocompleteNone)

	dnsSearchFlagName := "dns-search"
	netFlags.StringSlice(
		dnsSearchFlagName, containerConfig.DNSSearches(),
		"Set custom DNS search domains",
	)
	_ = cmd.RegisterFlagCompletionFunc(dnsSearchFlagName, completion.AutocompleteNone)

	ipFlagName := "ip"
	netFlags.String(
		ipFlagName, "",
		"Specify a static IPv4 address for the container",
	)
	_ = cmd.RegisterFlagCompletionFunc(ipFlagName, completion.AutocompleteNone)

	ip6FlagName := "ip6"
	netFlags.String(
		ip6FlagName, "",
		"Specify a static IPv6 address for the container",
	)
	_ = cmd.RegisterFlagCompletionFunc(ip6FlagName, completion.AutocompleteNone)

	macAddressFlagName := "mac-address"
	netFlags.String(
		macAddressFlagName, "",
		"Container MAC address (e.g. 92:d0:c6:0a:29:33)",
	)
	_ = cmd.RegisterFlagCompletionFunc(macAddressFlagName, completion.AutocompleteNone)

	networkFlagName := "network"
	netFlags.StringArray(
		networkFlagName, nil,
		"Connect a container to a network",
	)
	_ = cmd.RegisterFlagCompletionFunc(networkFlagName, AutocompleteNetworkFlag)

	networkAliasFlagName := "network-alias"
	netFlags.StringSlice(
		networkAliasFlagName, []string{},
		"Add network-scoped alias for the container",
	)
	_ = cmd.RegisterFlagCompletionFunc(networkAliasFlagName, completion.AutocompleteNone)

	publishFlagName := "publish"
	netFlags.StringSliceP(
		publishFlagName, "p", []string{},
		"Publish a container's port, or a range of ports, to the host (default [])",
	)
	_ = cmd.RegisterFlagCompletionFunc(publishFlagName, completion.AutocompleteNone)

	netFlags.Bool(
		"no-hosts", containerConfig.Containers.NoHosts,
		"Do not create /etc/hosts within the container, instead use the version from the image",
	)
}

// NetFlagsToNetOptions parses the network flags for the given cmd.
func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*entities.NetOptions, error) {
	var (
		err error
	)
	if opts == nil {
		opts = &entities.NetOptions{}
	}

	opts.AddHosts, err = flags.GetStringSlice("add-host")
	if err != nil {
		return nil, err
	}
	// Verify the additional hosts are in correct format
	for _, host := range opts.AddHosts {
		if _, err := parse.ValidateExtraHost(host); err != nil {
			return nil, err
		}
	}

	servers, err := flags.GetStringSlice("dns")
	if err != nil {
		return nil, err
	}
	for _, d := range servers {
		if d == "none" {
			opts.UseImageResolvConf = true
			if len(servers) > 1 {
				return nil, errors.Errorf("%s is not allowed to be specified with other DNS ip addresses", d)
			}
			break
		}
		dns := net.ParseIP(d)
		if dns == nil {
			return nil, errors.Errorf("%s is not an ip address", d)
		}
		opts.DNSServers = append(opts.DNSServers, dns)
	}

	options, err := flags.GetStringSlice("dns-opt")
	if err != nil {
		return nil, err
	}
	opts.DNSOptions = options

	dnsSearches, err := flags.GetStringSlice("dns-search")
	if err != nil {
		return nil, err
	}
	// Validate domains are good
	for _, dom := range dnsSearches {
		if dom == "." {
			if len(dnsSearches) > 1 {
				return nil, errors.Errorf("cannot pass additional search domains when also specifying '.'")
			}
			continue
		}
		if _, err := parse.ValidateDomain(dom); err != nil {
			return nil, err
		}
	}
	opts.DNSSearch = dnsSearches

	inputPorts, err := flags.GetStringSlice("publish")
	if err != nil {
		return nil, err
	}
	if len(inputPorts) > 0 {
		opts.PublishPorts, err = specgenutil.CreatePortBindings(inputPorts)
		if err != nil {
			return nil, err
		}
	}

	opts.NoHosts, err = flags.GetBool("no-hosts")
	if err != nil {
		return nil, err
	}

	// parse the network only when network was changed
	// otherwise we send default to server so that the server
	// can pick the correct default instead of the client
	if flags.Changed("network") {
		network, err := flags.GetStringArray("network")
		if err != nil {
			return nil, err
		}

		ns, networks, options, err := specgen.ParseNetworkFlag(network)
		if err != nil {
			return nil, err
		}

		opts.NetworkOptions = options
		opts.Network = ns
		opts.Networks = networks
	}

	if flags.Changed("ip") || flags.Changed("ip6") || flags.Changed("mac-address") || flags.Changed("network-alias") {
		// if there is no network we add the default
		if len(opts.Networks) == 0 {
			opts.Networks = map[string]types.PerNetworkOptions{
				"default": {},
			}
		}

		for _, ipFlagName := range []string{"ip", "ip6"} {
			ip, err := flags.GetString(ipFlagName)
			if err != nil {
				return nil, err
			}
			if ip != "" {
				// if pod create --infra=false
				if infra, err := flags.GetBool("infra"); err == nil && !infra {
					return nil, errors.Wrapf(define.ErrInvalidArg, "cannot set --%s without infra container", ipFlagName)
				}

				staticIP := net.ParseIP(ip)
				if staticIP == nil {
					return nil, errors.Errorf("%q is not an ip address", ip)
				}
				if !opts.Network.IsBridge() && !opts.Network.IsDefault() {
					return nil, errors.Wrapf(define.ErrInvalidArg, "--%s can only be set when the network mode is bridge", ipFlagName)
				}
				if len(opts.Networks) != 1 {
					return nil, errors.Wrapf(define.ErrInvalidArg, "--%s can only be set for a single network", ipFlagName)
				}
				for name, netOpts := range opts.Networks {
					netOpts.StaticIPs = append(netOpts.StaticIPs, staticIP)
					opts.Networks[name] = netOpts
				}
			}
		}

		m, err := flags.GetString("mac-address")
		if err != nil {
			return nil, err
		}
		if len(m) > 0 {
			// if pod create --infra=false
			if infra, err := flags.GetBool("infra"); err == nil && !infra {
				return nil, errors.Wrap(define.ErrInvalidArg, "cannot set --mac without infra container")
			}
			mac, err := net.ParseMAC(m)
			if err != nil {
				return nil, err
			}
			if !opts.Network.IsBridge() && !opts.Network.IsDefault() {
				return nil, errors.Wrap(define.ErrInvalidArg, "--mac-address can only be set when the network mode is bridge")
			}
			if len(opts.Networks) != 1 {
				return nil, errors.Wrap(define.ErrInvalidArg, "--mac-address can only be set for a single network")
			}
			for name, netOpts := range opts.Networks {
				netOpts.StaticMAC = types.HardwareAddr(mac)
				opts.Networks[name] = netOpts
			}
		}

		aliases, err := flags.GetStringSlice("network-alias")
		if err != nil {
			return nil, err
		}
		if len(aliases) > 0 {
			// if pod create --infra=false
			if infra, err := flags.GetBool("infra"); err == nil && !infra {
				return nil, errors.Wrap(define.ErrInvalidArg, "cannot set --network-alias without infra container")
			}
			if !opts.Network.IsBridge() && !opts.Network.IsDefault() {
				return nil, errors.Wrap(define.ErrInvalidArg, "--network-alias can only be set when the network mode is bridge")
			}
			for name, netOpts := range opts.Networks {
				netOpts.Aliases = aliases
				opts.Networks[name] = netOpts
			}
		}
	}

	return opts, err
}