aboutsummaryrefslogtreecommitdiff
path: root/libpod/network/cni/run.go
blob: 667ed3ab148cb1141a8fee68be2ec5096f463849 (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
// +build linux

package cni

import (
	"context"
	"net"
	"os"
	"strings"

	"github.com/containernetworking/cni/libcni"
	cnitypes "github.com/containernetworking/cni/pkg/types"
	types040 "github.com/containernetworking/cni/pkg/types/040"
	"github.com/containernetworking/plugins/pkg/ns"
	"github.com/containers/podman/v3/libpod/define"
	"github.com/containers/podman/v3/libpod/network/internal/util"
	"github.com/containers/podman/v3/libpod/network/types"
	"github.com/hashicorp/go-multierror"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/vishvananda/netlink"
)

// Setup will setup the container network namespace. It returns
// a map of StatusBlocks, the key is the network name.
func (n *cniNetwork) Setup(namespacePath string, options types.SetupOptions) (map[string]types.StatusBlock, error) {
	n.lock.Lock()
	defer n.lock.Unlock()
	err := n.loadNetworks()
	if err != nil {
		return nil, err
	}

	err = util.ValidateSetupOptions(n, namespacePath, options)
	if err != nil {
		return nil, err
	}

	// set the loopback adapter up in the container netns
	err = ns.WithNetNSPath(namespacePath, func(_ ns.NetNS) error {
		link, err := netlink.LinkByName("lo")
		if err == nil {
			err = netlink.LinkSetUp(link)
		}
		return err
	})
	if err != nil {
		return nil, errors.Wrapf(err, "failed to set the loopback adapter up")
	}

	var retErr error
	teardownOpts := options
	teardownOpts.Networks = map[string]types.PerNetworkOptions{}
	// make sure to teardown the already connected networks on error
	defer func() {
		if retErr != nil {
			if len(teardownOpts.Networks) > 0 {
				err := n.teardown(namespacePath, types.TeardownOptions(teardownOpts))
				if err != nil {
					logrus.Warn(err)
				}
			}
		}
	}()

	ports, err := convertSpecgenPortsToCNIPorts(options.PortMappings)
	if err != nil {
		return nil, err
	}

	results := make(map[string]types.StatusBlock, len(options.Networks))
	for name, netOpts := range options.Networks {
		network := n.networks[name]
		rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, netOpts)

		// If we have more than one static ip we need parse the ips via runtime config,
		// make sure to add the ips capability to the first plugin otherwise it doesn't get the ips
		if len(netOpts.StaticIPs) > 0 && !network.cniNet.Plugins[0].Network.Capabilities["ips"] {
			caps := make(map[string]interface{})
			caps["capabilities"] = map[string]bool{"ips": true}
			network.cniNet.Plugins[0], retErr = libcni.InjectConf(network.cniNet.Plugins[0], caps)
			if retErr != nil {
				return nil, retErr
			}
		}

		var res cnitypes.Result
		res, retErr = n.cniConf.AddNetworkList(context.Background(), network.cniNet, rt)
		// Add this network to teardown opts since it is now connected.
		// Also add this if an errors was returned since we want to call teardown on this regardless.
		teardownOpts.Networks[name] = netOpts
		if retErr != nil {
			return nil, retErr
		}

		logrus.Debugf("cni result for container %s network %s: %v", options.ContainerID, name, res)
		var status types.StatusBlock
		status, retErr = CNIResultToStatus(res)
		if retErr != nil {
			return nil, retErr
		}
		results[name] = status
	}
	return results, nil
}

// CNIResultToStatus convert the cni result to status block
// nolint:golint
func CNIResultToStatus(res cnitypes.Result) (types.StatusBlock, error) {
	result := types.StatusBlock{}
	cniResult, err := types040.GetResult(res)
	if err != nil {
		return result, err
	}
	nameservers := make([]net.IP, 0, len(cniResult.DNS.Nameservers))
	for _, nameserver := range cniResult.DNS.Nameservers {
		ip := net.ParseIP(nameserver)
		if ip == nil {
			return result, errors.Errorf("failed to parse cni nameserver ip %s", nameserver)
		}
		nameservers = append(nameservers, ip)
	}
	result.DNSServerIPs = nameservers
	result.DNSSearchDomains = cniResult.DNS.Search

	interfaces := make(map[string]types.NetInterface)
	for _, ip := range cniResult.IPs {
		if ip.Interface == nil {
			// we do no expect ips without an interface
			continue
		}
		if len(cniResult.Interfaces) <= *ip.Interface {
			return result, errors.Errorf("invalid cni result, interface index %d out of range", *ip.Interface)
		}
		cniInt := cniResult.Interfaces[*ip.Interface]
		netInt, ok := interfaces[cniInt.Name]
		if ok {
			netInt.Networks = append(netInt.Networks, types.NetAddress{
				Subnet:  types.IPNet{IPNet: ip.Address},
				Gateway: ip.Gateway,
			})
			interfaces[cniInt.Name] = netInt
		} else {
			mac, err := net.ParseMAC(cniInt.Mac)
			if err != nil {
				return result, err
			}
			interfaces[cniInt.Name] = types.NetInterface{
				MacAddress: types.HardwareAddr(mac),
				Networks: []types.NetAddress{{
					Subnet:  types.IPNet{IPNet: ip.Address},
					Gateway: ip.Gateway,
				}},
			}
		}
	}
	result.Interfaces = interfaces
	return result, nil
}

func getRuntimeConfig(netns, conName, conID, networkName string, ports []cniPortMapEntry, opts types.PerNetworkOptions) *libcni.RuntimeConf {
	rt := &libcni.RuntimeConf{
		ContainerID: conID,
		NetNS:       netns,
		IfName:      opts.InterfaceName,
		Args: [][2]string{
			{"IgnoreUnknown", "1"},
			// Do not set the K8S env vars, see https://github.com/containers/podman/issues/12083.
			// Only K8S_POD_NAME is used by dnsname to get the container name.
			{"K8S_POD_NAME", conName},
		},
		CapabilityArgs: map[string]interface{}{},
	}

	// Propagate environment CNI_ARGS
	for _, kvpairs := range strings.Split(os.Getenv("CNI_ARGS"), ";") {
		if keyval := strings.SplitN(kvpairs, "=", 2); len(keyval) == 2 {
			rt.Args = append(rt.Args, [2]string{keyval[0], keyval[1]})
		}
	}

	// Add mac address to cni args
	if len(opts.StaticMAC) > 0 {
		rt.Args = append(rt.Args, [2]string{"MAC", opts.StaticMAC.String()})
	}

	if len(opts.StaticIPs) == 1 {
		// Add a single IP to the args field. CNI plugins < 1.0.0
		// do not support multiple ips via capability args.
		rt.Args = append(rt.Args, [2]string{"IP", opts.StaticIPs[0].String()})
	} else if len(opts.StaticIPs) > 1 {
		// Set the static ips in the capability args
		// to support more than one static ip per network.
		rt.CapabilityArgs["ips"] = opts.StaticIPs
	}

	// Set network aliases for the dnsname plugin.
	if len(opts.Aliases) > 0 {
		rt.CapabilityArgs["aliases"] = map[string][]string{
			networkName: opts.Aliases,
		}
	}

	// Set PortMappings in Capabilities
	if len(ports) > 0 {
		rt.CapabilityArgs["portMappings"] = ports
	}

	return rt
}

// Teardown will teardown the container network namespace.
func (n *cniNetwork) Teardown(namespacePath string, options types.TeardownOptions) error {
	n.lock.Lock()
	defer n.lock.Unlock()
	err := n.loadNetworks()
	if err != nil {
		return err
	}
	return n.teardown(namespacePath, options)
}

func (n *cniNetwork) teardown(namespacePath string, options types.TeardownOptions) error {
	// Note: An empty namespacePath is allowed because some plugins
	// still need teardown, for example ipam should remove used ip allocations.

	ports, err := convertSpecgenPortsToCNIPorts(options.PortMappings)
	if err != nil {
		return err
	}

	var multiErr *multierror.Error
	for name, netOpts := range options.Networks {
		rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, netOpts)

		cniConfList, newRt, err := getCachedNetworkConfig(n.cniConf, name, rt)
		if err == nil {
			rt = newRt
		} else {
			logrus.Warnf("Failed to load cached network config: %v, falling back to loading network %s from disk", err, name)
			network := n.networks[name]
			if network == nil {
				multiErr = multierror.Append(multiErr, errors.Wrapf(define.ErrNoSuchNetwork, "network %s", name))
				continue
			}
			cniConfList = network.cniNet
		}

		err = n.cniConf.DelNetworkList(context.Background(), cniConfList, rt)
		if err != nil {
			multiErr = multierror.Append(multiErr, err)
		}
	}
	return multiErr.ErrorOrNil()
}

func getCachedNetworkConfig(cniConf *libcni.CNIConfig, name string, rt *libcni.RuntimeConf) (*libcni.NetworkConfigList, *libcni.RuntimeConf, error) {
	cniConfList := &libcni.NetworkConfigList{
		Name: name,
	}
	confBytes, rt, err := cniConf.GetNetworkListCachedConfig(cniConfList, rt)
	if err != nil {
		return nil, nil, err
	} else if confBytes == nil {
		return nil, nil, errors.Errorf("network %s not found in CNI cache", name)
	}

	cniConfList, err = libcni.ConfListFromBytes(confBytes)
	if err != nil {
		return nil, nil, err
	}
	return cniConfList, rt, nil
}