-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
driver.go
538 lines (466 loc) · 16.8 KB
/
driver.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2016-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 driver
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"reflect"
"strings"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/client"
"github.com/cilium/cilium/pkg/datapath/connector"
"github.com/cilium/cilium/pkg/datapath/link"
"github.com/cilium/cilium/pkg/datapath/linux/route"
datapathOption "github.com/cilium/cilium/pkg/datapath/option"
endpointIDPkg "github.com/cilium/cilium/pkg/endpoint/id"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
apiTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
dockerCliAPI "github.com/docker/docker/client"
"github.com/docker/libnetwork/drivers/remote/api"
lnTypes "github.com/docker/libnetwork/types"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "cilium-docker-driver")
// Driver interface that listens for docker requests.
type Driver interface {
Listen(string) error
}
type driver struct {
mutex lock.RWMutex
client *client.Client
dockerClient *dockerCliAPI.Client
conf models.DaemonConfigurationStatus
routes []api.StaticRoute
gatewayIPv6 string
gatewayIPv4 string
}
func endpointID(id string) string {
return endpointIDPkg.NewID(endpointIDPkg.DockerEndpointPrefix, id)
}
func newLibnetworkRoute(route route.Route) api.StaticRoute {
rt := api.StaticRoute{
Destination: route.Prefix.String(),
RouteType: lnTypes.CONNECTED,
}
if route.Nexthop != nil {
rt.NextHop = route.Nexthop.String()
rt.RouteType = lnTypes.NEXTHOP
}
return rt
}
// NewDriver creates and returns a new Driver for the given API URL.
// If url is nil then use SockPath provided by CILIUM_SOCK
// or the cilium default SockPath
func NewDriver(ciliumSockPath, dockerHostPath string) (Driver, error) {
if ciliumSockPath == "" {
ciliumSockPath = client.DefaultSockPath()
}
scopedLog := log.WithField("ciliumSockPath", ciliumSockPath)
c, err := client.NewClient(ciliumSockPath)
if err != nil {
scopedLog.WithError(err).Fatal("Error while starting cilium-client")
}
scopedLog = scopedLog.WithField("dockerHostPath", dockerHostPath)
dockerCli, err := dockerCliAPI.NewClientWithOpts(
dockerCliAPI.WithVersion("v1.21"),
dockerCliAPI.WithHost(dockerHostPath),
)
if err != nil {
scopedLog.WithError(err).Fatal("Error while starting cilium-client")
}
d := &driver{client: c, dockerClient: dockerCli}
for tries := 0; tries < 24; tries++ {
if res, err := c.ConfigGet(); err != nil {
if tries == 23 {
scopedLog.WithError(err).Fatal("Unable to connect to cilium daemon")
} else {
scopedLog.Info("Waiting for cilium daemon to start up...")
}
time.Sleep(time.Duration(tries) * time.Second)
} else {
if res.Status.Addressing == nil || (res.Status.Addressing.IPV4 == nil && res.Status.Addressing.IPV6 == nil) {
scopedLog.Fatal("Invalid addressing information from daemon")
}
d.conf = *res.Status
break
}
}
if err := connector.SufficientAddressing(d.conf.Addressing); err != nil {
scopedLog.WithError(err).Fatal("Insufficient addressing")
}
d.updateRoutes(nil)
log.Info("Starting docker events watcher")
go func() {
eventsCh, errCh := dockerCli.Events(context.Background(), apiTypes.EventsOptions{})
for {
select {
case err := <-errCh:
log.WithError(err).Error("Unable to connect to docker events channel, reconnecting...")
time.Sleep(5 * time.Second)
eventsCh, errCh = dockerCli.Events(context.Background(), apiTypes.EventsOptions{})
case event := <-eventsCh:
if event.Type != events.ContainerEventType || event.Action != "start" {
break
}
d.updateCiliumEP(event)
}
}
}()
log.Infof("Cilium Docker plugin ready")
return d, nil
}
func (driver *driver) updateCiliumEP(event events.Message) {
log = log.WithFields(logrus.Fields{"event": fmt.Sprintf("%+v", event)})
cont, err := driver.dockerClient.ContainerInspect(context.Background(), event.Actor.ID)
if err != nil {
log.WithFields(
logrus.Fields{
"container-id": event.Actor.ID,
},
).WithError(err).Error("Unable to inspect container")
}
if cont.Config == nil || cont.NetworkSettings == nil {
return
}
var epID string
for _, network := range cont.NetworkSettings.Networks {
epID = network.EndpointID
break
}
if epID == "" {
return
}
img, _, err := driver.dockerClient.ImageInspectWithRaw(context.Background(), cont.Config.Image)
if err != nil {
log.WithFields(
logrus.Fields{
"image-id": cont.Config.Image,
},
).WithError(err).Error("Unable to inspect image")
}
lbls := cont.Config.Labels
if img.Config != nil && img.Config.Labels != nil {
lbls = img.Config.Labels
// container labels overwrite image labels
for k, v := range cont.Config.Labels {
lbls[k] = v
}
}
addLbls := labels.Map2Labels(lbls, labels.LabelSourceContainer).GetModel()
ecr := &models.EndpointChangeRequest{
ContainerID: event.Actor.ID,
ContainerName: strings.TrimPrefix(cont.Name, "/"),
Labels: addLbls,
State: models.EndpointStateWaitingForIdentity,
}
err = driver.client.EndpointPatch(endpointID(epID), ecr)
if err != nil {
log.WithFields(
logrus.Fields{
"container-id": event.Actor.ID,
"endpoint-id": epID,
"labels": cont.Config.Labels,
"error": err,
}).
Error("Error while patching the endpoint labels of container")
}
}
func (driver *driver) updateRoutes(addressing *models.NodeAddressing) {
driver.mutex.Lock()
defer driver.mutex.Unlock()
if addressing != nil {
driver.conf.Addressing = addressing
}
driver.routes = []api.StaticRoute{}
if driver.conf.Addressing.IPV6 != nil && driver.conf.Addressing.IPV6.Enabled {
if routes, err := connector.IPv6Routes(driver.conf.Addressing, int(driver.conf.RouteMTU)); err != nil {
log.Fatalf("Unable to generate IPv6 routes: %s", err)
} else {
for _, r := range routes {
driver.routes = append(driver.routes, newLibnetworkRoute(r))
}
}
driver.gatewayIPv6 = connector.IPv6Gateway(driver.conf.Addressing)
}
if driver.conf.Addressing.IPV4 != nil && driver.conf.Addressing.IPV4.Enabled {
if routes, err := connector.IPv4Routes(driver.conf.Addressing, int(driver.conf.RouteMTU)); err != nil {
log.Fatalf("Unable to generate IPv4 routes: %s", err)
} else {
for _, r := range routes {
driver.routes = append(driver.routes, newLibnetworkRoute(r))
}
}
driver.gatewayIPv4 = connector.IPv4Gateway(driver.conf.Addressing)
}
}
// Listen listens for docker requests on a particular set of endpoints on the given
// socket path.
func (driver *driver) Listen(socket string) error {
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(notFound)
handleMethod := func(method string, h http.HandlerFunc) {
router.Methods("POST").Path(fmt.Sprintf("/%s", method)).HandlerFunc(h)
}
handleMethod("Plugin.Activate", driver.handshake)
handleMethod("NetworkDriver.GetCapabilities", driver.capabilities)
handleMethod("NetworkDriver.CreateNetwork", driver.createNetwork)
handleMethod("NetworkDriver.DeleteNetwork", driver.deleteNetwork)
handleMethod("NetworkDriver.CreateEndpoint", driver.createEndpoint)
handleMethod("NetworkDriver.DeleteEndpoint", driver.deleteEndpoint)
handleMethod("NetworkDriver.EndpointOperInfo", driver.infoEndpoint)
handleMethod("NetworkDriver.Join", driver.joinEndpoint)
handleMethod("NetworkDriver.Leave", driver.leaveEndpoint)
handleMethod("IpamDriver.GetCapabilities", driver.ipamCapabilities)
handleMethod("IpamDriver.GetDefaultAddressSpaces", driver.getDefaultAddressSpaces)
handleMethod("IpamDriver.RequestPool", driver.requestPool)
handleMethod("IpamDriver.ReleasePool", driver.releasePool)
handleMethod("IpamDriver.RequestAddress", driver.requestAddress)
handleMethod("IpamDriver.ReleaseAddress", driver.releaseAddress)
listener, err := net.Listen("unix", socket)
if err != nil {
return err
}
return http.Serve(listener, router)
}
func notFound(w http.ResponseWriter, r *http.Request) {
log.WithField(logfields.Object, logfields.Repr(r)).Warn("plugin Not found")
http.NotFound(w, r)
}
func sendError(w http.ResponseWriter, msg string, code int) {
log.WithFields(logrus.Fields{
"code": code,
"msg": msg,
}).Error("Sending error")
http.Error(w, msg, code)
}
func objectResponse(w http.ResponseWriter, obj interface{}) {
if err := json.NewEncoder(w).Encode(obj); err != nil {
sendError(w, "Could not JSON encode response", http.StatusInternalServerError)
return
}
}
func emptyResponse(w http.ResponseWriter) {
json.NewEncoder(w).Encode(map[string]string{})
}
type handshakeResp struct {
Implements []string
}
func (driver *driver) handshake(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(&handshakeResp{
[]string{"NetworkDriver", "IpamDriver"},
})
if err != nil {
log.WithError(err).Fatal("handshake encode")
sendError(w, "encode error", http.StatusInternalServerError)
return
}
log.Debug("Handshake completed")
}
func (driver *driver) capabilities(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(&api.GetCapabilityResponse{
Scope: "local",
})
if err != nil {
log.WithError(err).Fatal("capabilities encode")
sendError(w, "encode error", http.StatusInternalServerError)
return
}
log.Debug("NetworkDriver capabilities exchange complete")
}
func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
var create api.CreateNetworkRequest
err := json.NewDecoder(r.Body).Decode(&create)
if err != nil {
sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&create)).Debug("Network Create Called")
emptyResponse(w)
}
func (driver *driver) deleteNetwork(w http.ResponseWriter, r *http.Request) {
var delete api.DeleteNetworkRequest
if err := json.NewDecoder(r.Body).Decode(&delete); err != nil {
sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&delete)).Debug("Delete network request")
emptyResponse(w)
}
// CreateEndpointRequest is the as api.CreateEndpointRequest but with
// json.RawMessage type for Options map
type CreateEndpointRequest struct {
NetworkID string
EndpointID string
Interface api.EndpointInterface
Options map[string]json.RawMessage
}
func (driver *driver) createEndpoint(w http.ResponseWriter, r *http.Request) {
var create CreateEndpointRequest
var err error
if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&create)).Debug("Create endpoint request")
if create.Interface.Address == "" && create.Interface.AddressIPv6 == "" {
sendError(w, "No IPv4 or IPv6 address provided (required)", http.StatusBadRequest)
return
}
endpoint := &models.EndpointChangeRequest{
SyncBuildEndpoint: true,
State: models.EndpointStateWaitingForIdentity,
DockerEndpointID: create.EndpointID,
DockerNetworkID: create.NetworkID,
Addressing: &models.AddressPair{
IPV6: create.Interface.AddressIPv6,
IPV4: create.Interface.Address,
},
}
removeLinkOnErr := func(link netlink.Link) {
if err != nil && link != nil && !reflect.ValueOf(link).IsNil() {
if err := netlink.LinkDel(link); err != nil {
log.WithError(err).WithFields(logrus.Fields{
logfields.DatapathMode: driver.conf.DatapathMode,
logfields.Device: link.Attrs().Name,
}).Warn("failed to clean up")
}
}
}
switch driver.conf.DatapathMode {
case datapathOption.DatapathModeVeth:
var veth *netlink.Veth
veth, _, _, err = connector.SetupVeth(create.EndpointID, int(driver.conf.DeviceMTU), endpoint)
defer removeLinkOnErr(veth)
case datapathOption.DatapathModeIpvlan:
var ipvlan *netlink.IPVlan
ipvlan, _, _, err = connector.CreateIpvlanSlave(
create.EndpointID, int(driver.conf.DeviceMTU),
int(driver.conf.IpvlanConfiguration.MasterDeviceIndex),
driver.conf.IpvlanConfiguration.OperationMode, endpoint,
)
defer removeLinkOnErr(ipvlan)
}
if err != nil {
sendError(w,
fmt.Sprintf("Error while setting up %s mode: %s", driver.conf.DatapathMode, err),
http.StatusBadRequest)
return
}
// FIXME: Translate port mappings to RuleL4 policy elements
if err = driver.client.EndpointCreate(endpoint); err != nil {
sendError(w, fmt.Sprintf("Error creating endpoint %s", err), http.StatusBadRequest)
return
}
log.WithField(logfields.EndpointID, create.EndpointID).Debug("Created new endpoint")
respIface := &api.EndpointInterface{
// Fixme: the lxcmac is an empty string at this point and we only know the
// mac address at the end of joinEndpoint
// There's no problem in the setup but docker inspect will show an empty mac address
MacAddress: "",
}
resp := &api.CreateEndpointResponse{
Interface: respIface,
}
log.WithField(logfields.Response, logfields.Repr(resp)).Debug("Create endpoint response")
objectResponse(w, resp)
}
func (driver *driver) deleteEndpoint(w http.ResponseWriter, r *http.Request) {
var del api.DeleteEndpointRequest
var ifName string
if err := json.NewDecoder(r.Body).Decode(&del); err != nil {
sendError(w, "Could not decode JSON encode payload", http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&del)).Debug("Delete endpoint request")
switch driver.conf.DatapathMode {
case datapathOption.DatapathModeVeth:
ifName = connector.Endpoint2IfName(del.EndpointID)
case datapathOption.DatapathModeIpvlan:
ifName = connector.Endpoint2TempIfName(del.EndpointID)
}
if err := link.DeleteByName(ifName); err != nil {
log.WithError(err).Warn("Error while deleting link")
}
emptyResponse(w)
}
func (driver *driver) infoEndpoint(w http.ResponseWriter, r *http.Request) {
var info api.EndpointInfoRequest
if err := json.NewDecoder(r.Body).Decode(&info); err != nil {
sendError(w, "Could not decode JSON encode payload", http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&info)).Debug("Endpoint info request")
objectResponse(w, &api.EndpointInfoResponse{Value: map[string]interface{}{}})
log.WithField(logfields.Response, info.EndpointID).Debug("Endpoint info")
}
func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
var (
j api.JoinRequest
err error
)
driver.mutex.RLock()
defer driver.mutex.RUnlock()
if err := json.NewDecoder(r.Body).Decode(&j); err != nil {
sendError(w, "Could not decode JSON encode payload", http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&j)).Debug("Join request")
old, err := driver.client.EndpointGet(endpointID(j.EndpointID))
if err != nil {
sendError(w, fmt.Sprintf("Error retrieving endpoint %s", err), http.StatusBadRequest)
return
}
log.WithField(logfields.Object, old).Debug("Existing endpoint")
res := &api.JoinResponse{
InterfaceName: &api.InterfaceName{
SrcName: connector.Endpoint2TempIfName(j.EndpointID),
DstPrefix: connector.ContainerInterfacePrefix,
},
StaticRoutes: driver.routes,
DisableGatewayService: true,
GatewayIPv6: driver.gatewayIPv6,
//GatewayIPv4: driver.gatewayIPv4,
}
// FIXME? Having the following code results on a runtime error: docker: Error
// response from daemon: oci runtime error: process_linux.go:334: running prestart
// hook 0 caused "exit status 1: time=\"2016-10-26T06:33:17-07:00\" level=fatal
// msg=\"failed to set gateway while updating gateway: file exists\" \n"
//
// If empty, it works as expected without docker runtime errors
// res.Gateway = connector.IPv4Gateway(addr)
log.WithField(logfields.Response, logfields.Repr(res)).Debug("Join response")
objectResponse(w, res)
}
func (driver *driver) leaveEndpoint(w http.ResponseWriter, r *http.Request) {
var l api.LeaveRequest
if err := json.NewDecoder(r.Body).Decode(&l); err != nil {
sendError(w, "Could not decode JSON encode payload", http.StatusBadRequest)
return
}
log.WithField(logfields.Request, logfields.Repr(&l)).Debug("Leave request")
if err := driver.client.EndpointDelete(endpointID(l.EndpointID)); err != nil {
log.WithError(err).Warn("Leaving the endpoint failed")
}
emptyResponse(w)
}