forked from projectcalico/cni-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calico.go
442 lines (379 loc) · 16.2 KB
/
calico.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
// Copyright 2015 Tigera Inc
//
// 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 main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"runtime"
"github.com/containernetworking/cni/pkg/ipam"
"github.com/containernetworking/cni/pkg/skel"
cnitypes "github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
cniSpecVersion "github.com/containernetworking/cni/pkg/version"
"github.com/projectcalico/cni-plugin/k8s"
"github.com/projectcalico/cni-plugin/types"
"github.com/projectcalico/cni-plugin/utils"
api "github.com/projectcalico/libcalico-go/lib/apis/v2"
cerrors "github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/logutils"
"github.com/projectcalico/libcalico-go/lib/options"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var nodename string
func init() {
// This ensures that main runs only on main thread (thread group leader).
// since namespace ops (unshare, setns) are done for a single thread, we
// must ensure that the goroutine does not jump from OS thread to thread
runtime.LockOSThread()
}
func cmdAdd(args *skel.CmdArgs) error {
// Unmarshal the network config, and perform validation
conf := types.NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %v", err)
}
utils.ConfigureLogging(conf.LogLevel)
// Allow the nodename to be overridden by the network config
nodename = utils.DetermineNodename(conf)
// Extract WEP identifiers such as pod name, pod namespace (for k8s), containerID, IfName.
wepIDs, err := utils.GetIdentifiers(args, nodename)
if err != nil {
return err
}
logrus.WithField("EndpointIDs", wepIDs).Info("Extracted identifiers")
logrus.WithField("NetConfg", conf).Info("Loaded CNI NetConf")
calicoClient, err := utils.CreateClient(conf)
if err != nil {
return err
}
// Remove the endpoint field (IfName) from the wepIDs so we can get a WEP name prefix.
// We use the WEP name prefix (e.g. prefix: "node1-k8s-mypod--1-", full name: "node1-k8s-mypod--1-eth0"
// to list all the WEPs so if we have a WEP with a different IfName (e.g. "node1-k8s-mypod--1-eth1")
// we could still get that.
wepIDs.Endpoint = ""
// Calculate the workload name prefix from the WEP specific identifiers
// for the given orchestrator.
wepPrefix, err := wepIDs.CalculateWorkloadEndpointName(true)
if err != nil {
return fmt.Errorf("error constructing WorkloadEndpoint prefix: %s", err)
}
ctx := context.Background()
// Check if there's an existing endpoint by listing the existing endpoints based on the WEP name prefix.
endpoints, err := calicoClient.WorkloadEndpoints().List(ctx, options.ListOptions{Name: wepPrefix, Namespace: wepIDs.Namespace, Prefix: true})
if err != nil {
return err
}
var logger *logrus.Entry
if wepIDs.Orchestrator == "k8s" {
logger = logrus.WithFields(logrus.Fields{
"WorkloadEndpoint": fmt.Sprintf("%s%s", wepPrefix, wepIDs.Endpoint),
"ContainerID": wepIDs.ContainerID,
"Pod": wepIDs.Pod,
"Namespace": wepIDs.Namespace,
})
} else {
logger = logrus.WithFields(logrus.Fields{
"ContainerID": wepIDs.ContainerID,
})
}
logger.Debugf("Retrieved list of endpoints: %v", endpoints)
var endpoint *api.WorkloadEndpoint
// If the prefix list returns 1 or more items, we go through the items and try to see if the name matches the WEP
// identifiers we have. The identifiers we use for this match at this point are:
// 1. Node name
// 2. Orchestrator ID ('cni' or 'k8s')
// 3. ContainerID
// 4. Pod name (only for k8s)
// Note we don't use the interface name (endpoint) for this match.
// If we find a match from the returned list then we've found the workload endpoint,
// and we reuse that even if it has a different interface name, because
// we only support one interface per pod right now.
// For example, you have a WEP for a k8s pod "mypod-1", and IfName "eth0" on node "node1", that will result in
// a WEP name "node1-k8s-mypod--1-eth0" in the datastore, now you're trying to schedule another pod "mypod",
// IfName "eth0" and node "node1", so we do a prefix list to get all the endpoints for that workload, with
// the prefix "node1-k8s-mypod-". Now this search would return any existing endpoints for "mypod", but it will also
// list "node1-k8s-mypod--1-eth0" which is not the same WorkloadEndpoint, so to avoid that, we go through the
// list of returned WEPs from the prefix list and call NameMatches() based on all the
// identifiers (pod name, containerID, node name, orchestrator), but omit the IfName (Endpoint field) since we can
// only have one interface per pod right now, and NameMatches() will return true if the WEP matches the identifiers.
// It is possible that none of the WEPs in the list match the identifiers, which means we don't already have an
// existing WEP to reuse. See `names.WorkloadEndpointIdentifiers` GoDoc comments for more details.
if len(endpoints.Items) > 0 {
logger.Debugf("List of WorkloadEndpoints %v", endpoints.Items)
for _, ep := range endpoints.Items {
match, err := wepIDs.WorkloadEndpointIdentifiers.NameMatches(ep.Name)
if err != nil {
// We should never hit this error, because it should have already been
// caught by CalculateWorkloadEndpointName.
return fmt.Errorf("invalid WorkloadEndpoint identifiers: %v", wepIDs.WorkloadEndpointIdentifiers)
}
if match {
logger.Debugf("Found a match for WorkloadEndpoint: %v", ep)
endpoint = &ep
// Assign the WEP name to wepIDs' WEPName field.
wepIDs.WEPName = endpoint.Name
// Put the endpoint name from the matched WEP in the identifiers.
wepIDs.Endpoint = ep.Spec.Endpoint
logger.Infof("Calico CNI found existing endpoint: %v", endpoint)
break
}
}
}
// If we don't find a match from the existing WorkloadEndpoints then we calculate
// the WEP name with the IfName passed in so we can create the WorkloadEndpoint later in the process.
if endpoint == nil {
wepIDs.Endpoint = args.IfName
wepIDs.WEPName, err = wepIDs.CalculateWorkloadEndpointName(false)
if err != nil {
return fmt.Errorf("error constructing WorkloadEndpoint name: %s", err)
}
}
// Collect the result in this variable - this is ultimately what gets "returned" by this function by printing
// it to stdout.
var result *current.Result
// If running under Kubernetes then branch off into the kubernetes code, otherwise handle everything in this
// function.
if wepIDs.Orchestrator == "k8s" {
if result, err = k8s.CmdAddK8s(ctx, args, conf, *wepIDs, calicoClient, endpoint); err != nil {
return err
}
} else {
// Default CNI behavior - use the CNI network name as the Calico profile.
profileID := conf.Name
if endpoint != nil {
// There is an existing endpoint - no need to create another.
// This occurs when adding an existing container to a new CNI network
// Find the IP address from the endpoint and use that in the response.
// Don't create the veth or do any networking.
// Just update the profile on the endpoint. The profile will be created if needed during the
// profile processing step.
logger.Infof("Calico CNI appending profile: %s\n", profileID)
endpoint.Spec.Profiles = append(endpoint.Spec.Profiles, profileID)
result, err = utils.CreateResultFromEndpoint(endpoint)
logger.WithField("result", result).Debug("Created result from endpoint")
if err != nil {
return err
}
} else {
// There's no existing endpoint, so we need to do the following:
// 1) Call the configured IPAM plugin to get IP address(es)
// 2) Configure the Calico endpoint
// 3) Create the veth, configuring it on both the host and container namespace.
// 1) Run the IPAM plugin and make sure there's an IP address returned.
logger.WithFields(logrus.Fields{"paths": os.Getenv("CNI_PATH"),
"type": conf.IPAM.Type}).Debug("Looking for IPAM plugin in paths")
ipamResult, err := ipam.ExecAdd(conf.IPAM.Type, args.StdinData)
logger.WithField("IPAM result", ipamResult).Info("Got result from IPAM plugin")
if err != nil {
return err
}
// Convert IPAM result into current Result.
// IPAM result has a bunch of fields that are optional for an IPAM plugin
// but required for a CNI plugin, so this is to populate those fields.
// See CNI Spec doc for more details.
result, err = current.NewResultFromResult(ipamResult)
if err != nil {
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return err
}
if len(result.IPs) == 0 {
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return errors.New("IPAM plugin returned missing IP config")
}
// Parse endpoint labels passed in by Mesos, and store in a map.
labels := map[string]string{}
for _, label := range conf.Args.Mesos.NetworkInfo.Labels.Labels {
labels[label.Key] = label.Value
}
// 2) Create the endpoint object
endpoint = api.NewWorkloadEndpoint()
endpoint.Name = wepIDs.WEPName
endpoint.Namespace = wepIDs.Namespace
endpoint.Spec.Endpoint = wepIDs.Endpoint
endpoint.Spec.Node = wepIDs.Node
endpoint.Spec.Orchestrator = wepIDs.Orchestrator
endpoint.Spec.ContainerID = wepIDs.ContainerID
endpoint.Labels = labels
endpoint.Spec.Profiles = []string{profileID}
logger.WithField("endpoint", endpoint).Debug("Populated endpoint (without nets)")
if err = utils.PopulateEndpointNets(endpoint, result); err != nil {
// Cleanup IP allocation and return the error.
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return err
}
logger.WithField("endpoint", endpoint).Info("Populated endpoint (with nets)")
fmt.Fprintf(os.Stderr, "Calico CNI using IPs: %s\n", endpoint.Spec.IPNetworks)
// 3) Set up the veth
hostVethName, contVethMac, err := utils.DoNetworking(args, conf, result, logger, "")
if err != nil {
// Cleanup IP allocation and return the error.
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return err
}
logger.WithFields(logrus.Fields{
"HostVethName": hostVethName,
"ContainerVethMac": contVethMac,
}).Info("Networked namespace")
endpoint.Spec.MAC = contVethMac
endpoint.Spec.InterfaceName = hostVethName
}
// Write the endpoint object (either the newly created one, or the updated one with a new ProfileIDs).
if _, err := utils.CreateOrUpdate(ctx, calicoClient, endpoint); err != nil {
// Cleanup IP allocation and return the error.
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return err
}
logger.WithField("endpoint", endpoint).Info("Wrote endpoint to datastore")
}
// Handle profile creation - this is only done if there isn't a specific policy handler.
if conf.Policy.PolicyType == "" {
logger.Debug("Handling profiles")
// Start by checking if the profile already exists. If it already exists then there is no work to do.
// The CNI plugin never updates a profile.
exists := true
_, err = calicoClient.Profiles().Get(ctx, conf.Name, options.GetOptions{})
if err != nil {
_, ok := err.(cerrors.ErrorResourceDoesNotExist)
if ok {
exists = false
} else {
// Cleanup IP allocation and return the error.
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return err
}
}
if !exists {
// The profile doesn't exist so needs to be created. The rules vary depending on whether k8s is being used.
// Under k8s (without full policy support) the rule is permissive and allows all traffic.
// Otherwise, incoming traffic is only allowed from profiles with the same tag.
fmt.Fprintf(os.Stderr, "Calico CNI creating profile: %s\n", conf.Name)
var inboundRules []api.Rule
if wepIDs.Orchestrator == "k8s" {
inboundRules = []api.Rule{{Action: "allow"}}
} else {
inboundRules = []api.Rule{{Action: "allow", Source: api.EntityRule{Selector: fmt.Sprintf("has(%s)", conf.Name)}}}
}
profile := &api.Profile{
ObjectMeta: metav1.ObjectMeta{
Name: conf.Name,
Labels: map[string]string{conf.Name: ""},
},
Spec: api.ProfileSpec{
EgressRules: []api.Rule{{Action: "allow"}},
IngressRules: inboundRules,
LabelsToApply: map[string]string{conf.Name: ""},
},
}
logger.WithField("profile", profile).Info("Creating profile")
if _, err := calicoClient.Profiles().Create(ctx, profile, options.SetOptions{}); err != nil {
// Cleanup IP allocation and return the error.
utils.ReleaseIPAllocation(logger, conf.IPAM.Type, args.StdinData)
return err
}
}
}
// Set Gateway to nil. Calico-IPAM doesn't set it, but host-local does.
// We modify IPs subnet received from the IPAM plugin (host-local),
// so Gateway isn't valid anymore. It is also not used anywhere by Calico.
for _, ip := range result.IPs {
ip.Gateway = nil
}
// Print result to stdout, in the format defined by the requested cniVersion.
return cnitypes.PrintResult(result, conf.CNIVersion)
}
func cmdDel(args *skel.CmdArgs) error {
conf := types.NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %v", err)
}
utils.ConfigureLogging(conf.LogLevel)
// Allow the nodename to be overridden by the network config
nodename := utils.DetermineNodename(conf)
epIDs, err := utils.GetIdentifiers(args, nodename)
if err != nil {
return err
}
logger := logrus.WithFields(logrus.Fields{
"ContainerID": epIDs.ContainerID,
})
calicoClient, err := utils.CreateClient(conf)
if err != nil {
return err
}
// Calculate the WEP name so we can call DEL on the exact endpoint.
epIDs.WEPName, err = epIDs.CalculateWorkloadEndpointName(false)
if err != nil {
return fmt.Errorf("error constructing WorkloadEndpoint name: %s", err)
}
logger.WithFields(logrus.Fields{
"Orchestrator": epIDs.Orchestrator,
"Node": epIDs.Node,
"WorkloadEndpoint": epIDs.WEPName,
"ContainerID": epIDs.ContainerID,
}).Info("Extracted identifiers")
ctx := context.Background()
// Handle k8s specific bits of handling the DEL.
if epIDs.Orchestrator == "k8s" {
return k8s.CmdDelK8s(ctx, calicoClient, *epIDs, args, conf, logger)
}
// Release the IP address by calling the configured IPAM plugin.
ipamErr := utils.CleanUpIPAM(conf, args, logger)
// Delete the WorkloadEndpoint object from the datastore.
if _, err = calicoClient.WorkloadEndpoints().Delete(ctx, epIDs.Namespace, epIDs.WEPName, options.DeleteOptions{}); err != nil {
if _, ok := err.(cerrors.ErrorResourceDoesNotExist); ok {
// Log and proceed with the clean up if WEP doesn't exist.
logger.WithField("WorkloadEndpoint", epIDs.WEPName).Info("Endpoint object does not exist, no need to clean up.")
} else {
return err
}
}
// Clean up namespace by removing the interfaces.
err = utils.CleanUpNamespace(args, logger)
if err != nil {
return err
}
// Return the IPAM error if there was one. The IPAM error will be lost if there was also an error in cleaning up
// the device or endpoint, but crucially, the user will know the overall operation failed.
return ipamErr
}
// VERSION is filled out during the build process (using git describe output)
var VERSION string
func main() {
// Set up logging formatting.
logrus.SetFormatter(&logutils.Formatter{})
// Install a hook that adds file/line no information.
logrus.AddHook(&logutils.ContextHook{})
// Display the version on "-v", otherwise just delegate to the skel code.
// Use a new flag set so as not to conflict with existing libraries which use "flag"
flagSet := flag.NewFlagSet("Calico", flag.ExitOnError)
version := flagSet.Bool("v", false, "Display version")
err := flagSet.Parse(os.Args[1:])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if *version {
fmt.Println(VERSION)
os.Exit(0)
}
if err := utils.AddIgnoreUnknownArgs(); err != nil {
os.Exit(1)
}
skel.PluginMain(cmdAdd, cmdDel, cniSpecVersion.All)
}