forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
355 lines (309 loc) · 11.2 KB
/
endpoint.go
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2017-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package launch
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/datapath/linux/route"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/endpoint/connector"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/endpointmanager"
healthDefaults "github.com/cilium/cilium/pkg/health/defaults"
"github.com/cilium/cilium/pkg/health/probe"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/launcher"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/mtu"
"github.com/cilium/cilium/pkg/netns"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/pidfile"
"github.com/cilium/cilium/pkg/sysctl"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
const (
ciliumHealth = "cilium-health"
netNSName = "cilium-health"
binaryName = "cilium-health-responder"
)
var (
// vethName is the host-side veth link device name for cilium-health EP
// (veth mode only).
vethName = "lxc_health"
// legacyVethName is the host-side cilium-health EP device name used in
// older Cilium versions. Used for removal only.
legacyVethName = "cilium_health"
// epIfaceName is the endpoint-side link device name for cilium-health.
epIfaceName = "cilium"
// PidfilePath
PidfilePath = "health-endpoint.pid"
// LaunchTime is the expected time within which the health endpoint
// should be able to be successfully run and its BPF program attached.
LaunchTime = 30 * time.Second
)
func configureHealthRouting(netns, dev string, addressing *models.NodeAddressing, mtuConfig mtu.Configuration) error {
routes := []route.Route{}
if option.Config.EnableIPv4 {
v4Routes, err := connector.IPv4Routes(addressing, mtuConfig.GetRouteMTU())
if err == nil {
routes = append(routes, v4Routes...)
} else {
log.Debugf("Couldn't get IPv4 routes for health routing")
}
}
if option.Config.EnableIPv6 {
v6Routes, err := connector.IPv6Routes(addressing, mtuConfig.GetRouteMTU())
if err != nil {
return fmt.Errorf("Failed to get IPv6 routes")
}
routes = append(routes, v6Routes...)
}
prog := "ip"
args := []string{"netns", "exec", netns, "bash", "-c"}
routeCmds := []string{}
for _, rt := range routes {
cmd := strings.Join(rt.ToIPCommand(dev), " ")
log.WithField("netns", netns).WithField("command", cmd).Debug("Adding route")
routeCmds = append(routeCmds, cmd)
}
cmd := strings.Join(routeCmds, " && ")
args = append(args, cmd)
log.Debugf("Running \"%s %+v\"", prog, args)
out, err := exec.Command(prog, args...).CombinedOutput()
if err == nil && len(out) > 0 {
log.Warn(out)
}
return err
}
func configureHealthInterface(netNS ns.NetNS, ifName string, ip4Addr, ip6Addr *net.IPNet) error {
return netNS.Do(func(_ ns.NetNS) error {
link, err := netlink.LinkByName(ifName)
if err != nil {
return err
}
if ip6Addr == nil {
name := fmt.Sprintf("net.ipv6.conf.%s.disable_ipv6", ifName)
// Ignore the error; if IPv6 is completely disabled
// then it's okay if we can't write the sysctl.
_ = sysctl.Write(name, "1")
} else {
if err = netlink.AddrAdd(link, &netlink.Addr{IPNet: ip6Addr}); err != nil {
return err
}
}
if ip4Addr != nil {
if err = netlink.AddrAdd(link, &netlink.Addr{IPNet: ip4Addr}); err != nil {
return err
}
}
if err = netlink.LinkSetUp(link); err != nil {
return err
}
lo, err := netlink.LinkByName("lo")
if err != nil {
return err
}
if err = netlink.LinkSetUp(lo); err != nil {
return err
}
return nil
})
}
// Client wraps a client to a specific cilium-health endpoint instance, to
// provide convenience methods such as PingEndpoint().
type Client struct {
host string
}
// PingEndpoint attempts to make an API ping request to the local cilium-health
// endpoint, and returns whether this was successful.
func (c *Client) PingEndpoint() error {
return probe.GetHello(c.host)
}
// KillEndpoint attempts to kill any existing cilium-health endpoint if it
// exists.
//
// This is intended to be invoked in multiple situations:
// * The health endpoint has never been run before
// * The health endpoint was run during a previous run of the Cilium agent
// * The health endpoint crashed during the current run of the Cilium agent
// and needs to be cleaned up before it is restarted.
func KillEndpoint() {
path := filepath.Join(option.Config.StateDir, PidfilePath)
scopedLog := log.WithField(logfields.PIDFile, path)
scopedLog.Debug("Killing old health endpoint process")
pid, err := pidfile.Kill(path)
if err != nil {
scopedLog.WithError(err).Warning("Failed to kill cilium-health-responder")
} else if pid != 0 {
scopedLog.WithField(logfields.PID, pid).Debug("Killed endpoint process")
}
}
// CleanupEndpoint cleans up remaining resources associated with the health
// endpoint.
//
// This is expected to be called after the process is killed and the endpoint
// is removed from the endpointmanager.
func CleanupEndpoint() {
// Removes the interfaces used for the endpoint process, followed by the
// deletion of the health namespace itself. The removal of the interfaces
// is needed, because network namespace removal does not always trigger the
// deletion of associated interfaces immediately (e.g. when a process in the
// namespace marked for deletion has not yet been terminated).
switch option.Config.DatapathMode {
case option.DatapathModeVeth:
for _, iface := range []string{legacyVethName, vethName} {
scopedLog := log.WithField(logfields.Veth, iface)
if link, err := netlink.LinkByName(iface); err == nil {
err = netlink.LinkDel(link)
if err != nil {
scopedLog.WithError(err).Info("Couldn't delete cilium-health veth device")
}
} else {
scopedLog.WithError(err).Debug("Didn't find existing device")
}
}
case option.DatapathModeIpvlan:
if err := netns.RemoveIfFromNetNSWithNameIfBothExist(netNSName, epIfaceName); err != nil {
log.WithError(err).WithField(logfields.Ipvlan, epIfaceName).
Info("Couldn't delete cilium-health ipvlan slave device")
}
}
if err := netns.RemoveNetNSWithName(netNSName); err != nil {
log.WithError(err).Debug("Unable to remove cilium-health namespace")
}
}
// LaunchAsEndpoint launches the cilium-health agent in a nested network
// namespace and attaches it to Cilium the same way as any other endpoint,
// but with special reserved labels.
//
// CleanupEndpoint() must be called before calling LaunchAsEndpoint() to ensure
// cleanup of prior cilium-health endpoint instances.
func LaunchAsEndpoint(baseCtx context.Context, owner regeneration.Owner, n *node.Node, mtuConfig mtu.Configuration) (*Client, error) {
var (
cmd = launcher.Launcher{}
info = &models.EndpointChangeRequest{
ContainerName: ciliumHealth,
State: models.EndpointStateWaitingForIdentity,
Addressing: &models.AddressPair{},
}
healthIP net.IP
ip4Address, ip6Address *net.IPNet
)
if n.IPv6HealthIP != nil {
healthIP = n.IPv6HealthIP
info.Addressing.IPV6 = healthIP.String()
ip6Address = &net.IPNet{IP: healthIP, Mask: defaults.ContainerIPv6Mask}
}
if n.IPv4HealthIP != nil {
healthIP = n.IPv4HealthIP
info.Addressing.IPV4 = healthIP.String()
ip4Address = &net.IPNet{IP: healthIP, Mask: defaults.ContainerIPv4Mask}
}
if option.Config.EnableEndpointRoutes {
dpConfig := &models.EndpointDatapathConfiguration{
InstallEndpointRoute: true,
RequireEgressProg: true,
}
info.DatapathConfiguration = dpConfig
}
netNS, err := netns.ReplaceNetNSWithName(netNSName)
if err != nil {
return nil, err
}
switch option.Config.DatapathMode {
case option.DatapathModeVeth:
_, epLink, err := connector.SetupVethWithNames(vethName, epIfaceName, mtuConfig.GetDeviceMTU(), info)
if err != nil {
return nil, fmt.Errorf("Error while creating veth: %s", err)
}
if err = netlink.LinkSetNsFd(*epLink, int(netNS.Fd())); err != nil {
return nil, fmt.Errorf("failed to move device %q to health namespace: %s", epIfaceName, err)
}
case option.DatapathModeIpvlan:
mapFD, err := connector.CreateAndSetupIpvlanSlave("",
epIfaceName, netNS, mtuConfig.GetDeviceMTU(),
option.Config.Ipvlan.MasterDeviceIndex,
option.Config.Ipvlan.OperationMode, info)
if err != nil {
if errDel := netns.RemoveNetNSWithName(netNSName); errDel != nil {
log.WithError(errDel).WithField(logfields.NetNSName, netNSName).
Warning("Unable to remove network namespace")
}
return nil, err
}
defer unix.Close(mapFD)
}
if err = configureHealthInterface(netNS, epIfaceName, ip4Address, ip6Address); err != nil {
return nil, fmt.Errorf("failed configure health interface %q: %s", epIfaceName, err)
}
pidfile := filepath.Join(option.Config.StateDir, PidfilePath)
prog := "ip"
args := []string{"netns", "exec", netNSName, binaryName, "--pidfile", pidfile}
cmd.SetTarget(prog)
cmd.SetArgs(args)
log.Infof("Spawning health endpoint with command %q %q", prog, args)
if err := cmd.Run(); err != nil {
return nil, err
}
// Create the endpoint
ep, err := endpoint.NewEndpointFromChangeModel(owner, info)
if err != nil {
return nil, fmt.Errorf("Error while creating endpoint model: %s", err)
}
// Wait until the cilium-health endpoint is running before setting up routes
deadline := time.Now().Add(1 * time.Minute)
for {
if _, err := os.Stat(pidfile); err == nil {
log.WithField("pidfile", pidfile).Debug("cilium-health agent running")
break
} else if time.Now().After(deadline) {
return nil, fmt.Errorf("Endpoint failed to run: %s", err)
} else {
time.Sleep(1 * time.Second)
}
}
// Set up the endpoint routes
hostAddressing := node.GetNodeAddressing()
if err = configureHealthRouting(info.ContainerName, epIfaceName, hostAddressing, mtuConfig); err != nil {
return nil, fmt.Errorf("Error while configuring routes: %s", err)
}
if err := endpointmanager.AddEndpoint(owner, ep, "Create cilium-health endpoint"); err != nil {
return nil, fmt.Errorf("Error while adding endpoint: %s", err)
}
if err := ep.LockAlive(); err != nil {
return nil, err
}
ep.PinDatapathMap()
ep.Unlock()
// Give the endpoint a security identity
ctx, cancel := context.WithTimeout(baseCtx, LaunchTime)
defer cancel()
ep.UpdateLabels(ctx, labels.LabelHealth, nil, true)
// Initialize the health client to talk to this instance.
client := &Client{host: "http://" + net.JoinHostPort(healthIP.String(), fmt.Sprintf("%d", healthDefaults.HTTPPathPort))}
metrics.SubprocessStart.WithLabelValues(ciliumHealth).Inc()
return client, nil
}