-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpod.go
531 lines (469 loc) · 17.9 KB
/
pod.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
package controller
import (
"encoding/json"
"fmt"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
v1coreinformerfactory "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
v1corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
nadclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
nadinformers "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions"
nadlisterv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/listers/k8s.cni.cncf.io/v1"
nadutils "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils"
multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
"github.com/k8snetworkplumbingwg/multus-dynamic-networks-controller/pkg/annotations"
"github.com/k8snetworkplumbingwg/multus-dynamic-networks-controller/pkg/cri"
"github.com/k8snetworkplumbingwg/multus-dynamic-networks-controller/pkg/logging"
"github.com/k8snetworkplumbingwg/multus-dynamic-networks-controller/pkg/multuscni"
)
const (
AdvertisedName = "pod-networks-updates"
maxRetries = 2
add DynamicAttachmentRequestType = "add"
remove DynamicAttachmentRequestType = "remove"
)
type DynamicAttachmentRequestType string
type DynamicAttachmentRequest struct {
Pod *corev1.Pod
Attachments []nadv1.NetworkSelectionElement
Type DynamicAttachmentRequestType
PodNetNS string
}
func (dar *DynamicAttachmentRequest) String() string {
req, err := json.Marshal(dar)
if err != nil {
klog.Warningf("failed to marshal DynamicAttachmentRequest: %v", err)
return ""
}
return string(req)
}
// PodNetworksController handles the cncf networks annotations update, and
// triggers adding / removing networks from a running pod.
type PodNetworksController struct {
k8sClientSet kubernetes.Interface
arePodsSynched cache.InformerSynced
areNetAttachDefsSynched cache.InformerSynced
podsInformer cache.SharedIndexInformer
netAttachDefInformer cache.SharedIndexInformer
podsLister v1corelisters.PodLister
netAttachDefLister nadlisterv1.NetworkAttachmentDefinitionLister
broadcaster record.EventBroadcaster
recorder record.EventRecorder
workqueue workqueue.RateLimitingInterface
nadClientSet nadclient.Interface
containerRuntime cri.ContainerRuntime
multusClient multuscni.Client
}
// NewPodNetworksController returns new PodNetworksController instance
func NewPodNetworksController(
k8sCoreInformerFactory v1coreinformerfactory.SharedInformerFactory,
nadInformers nadinformers.SharedInformerFactory,
broadcaster record.EventBroadcaster,
recorder record.EventRecorder,
k8sClientSet kubernetes.Interface,
nadClientSet nadclient.Interface,
containerRuntime cri.ContainerRuntime,
multusClient multuscni.Client,
) (*PodNetworksController, error) {
podInformer := k8sCoreInformerFactory.Core().V1().Pods().Informer()
nadInformer := nadInformers.K8sCniCncfIo().V1().NetworkAttachmentDefinitions().Informer()
podNetworksController := &PodNetworksController{
arePodsSynched: podInformer.HasSynced,
areNetAttachDefsSynched: nadInformer.HasSynced,
podsInformer: podInformer,
podsLister: k8sCoreInformerFactory.Core().V1().Pods().Lister(),
netAttachDefInformer: nadInformer,
netAttachDefLister: nadInformers.K8sCniCncfIo().V1().NetworkAttachmentDefinitions().Lister(),
recorder: recorder,
broadcaster: broadcaster,
workqueue: workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(),
AdvertisedName),
k8sClientSet: k8sClientSet,
nadClientSet: nadClientSet,
containerRuntime: containerRuntime,
multusClient: multusClient,
}
if _, err := podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: podNetworksController.handlePodUpdate,
}); err != nil {
return nil, fmt.Errorf("error setting the add event handlers: %v", err)
}
return podNetworksController, nil
}
// Start runs worker thread after performing cache synchronization
func (pnc *PodNetworksController) Start(stopChan <-chan struct{}) {
klog.Infof("starting network controller")
defer pnc.workqueue.ShutDown()
if ok := cache.WaitForCacheSync(stopChan, pnc.arePodsSynched, pnc.areNetAttachDefsSynched); !ok {
klog.Infof("failed waiting for caches to sync")
return
}
// ensure that we didn't miss any updates before the cache sync completion
if err := pnc.reconcileOnStartup(); err != nil {
klog.Infof("failed to reconcile pods on startup: %v", err)
return
}
go wait.Until(pnc.worker, time.Second, stopChan)
<-stopChan
klog.Infof("shutting down network controller")
}
func (pnc *PodNetworksController) worker() {
for pnc.processNextWorkItem() {
}
}
func (pnc *PodNetworksController) ignoreHostNetworkedPods(pod *corev1.Pod) bool {
// since there is no such "not has" relation in a field selector,
// filter out pods that are of no concern to the controller here
if pod.Spec.HostNetwork {
_, haveNetworkAttachments := pod.GetAnnotations()[nadv1.NetworkAttachmentAnnot]
namespacedName := annotations.NamespacedName(pod.GetNamespace(), pod.GetName())
if haveNetworkAttachments {
klog.Warningf("rejecting to add interfaces for host networked pod: %s", namespacedName)
pnc.Eventf(pod, corev1.EventTypeWarning, "InterfaceAddRejected", rejectInterfaceAddEventFormat(pod))
} else {
klog.V(logging.Debug).Infof("host networked pod [%s] got filtered out", namespacedName)
}
return true
}
return false
}
func (pnc *PodNetworksController) reconcileOnStartup() error {
pods, err := pnc.podsLister.List(labels.Everything())
if err != nil {
return fmt.Errorf("failed to list pods on current node: %v", err)
}
for _, pod := range pods {
if pnc.ignoreHostNetworkedPods(pod) {
continue
}
namespacedName := annotations.NamespacedName(pod.GetNamespace(), pod.GetName())
klog.V(logging.Debug).Infof("pod [%s] added to reconcile on startup", namespacedName)
pnc.workqueue.Add(&namespacedName)
}
return nil
}
func (pnc *PodNetworksController) processNextWorkItem() bool {
queueItem, shouldQuit := pnc.workqueue.Get()
if shouldQuit {
return false
}
defer pnc.workqueue.Done(queueItem)
podNamespacedName := queueItem.(*string)
klog.Infof("extracted update request for pod [%s] from the queue", *podNamespacedName)
podNamespace, podName, err := separateNamespaceAndName(*podNamespacedName)
if err != nil {
klog.Errorf("the update key - [%s] - is not in the namespaced name format: %v", *podNamespacedName, err)
return true
}
var results []annotations.AttachmentResult
var pod *corev1.Pod
defer func() {
pnc.handleResult(err, podNamespacedName, pod, results)
}()
pod, err = pnc.podsLister.Pods(podNamespace).Get(podName)
if err != nil {
klog.Errorf("could not access pod from the informer")
return true
}
indexedNetworkSelectionElements := annotations.IndexPodNetworkSelectionElements(pod)
indexedNetworkStatus := annotations.IndexNetworkStatusIgnoringDefaultNetwork(pod)
netnsPath, err := pnc.netnsPath(pod)
if err != nil {
klog.Errorf("failed to figure out the pod's network namespace: %v", err)
return true
}
var attachmentsToAdd []nadv1.NetworkSelectionElement
for key := range indexedNetworkSelectionElements {
if _, wasFound := indexedNetworkStatus[key]; !wasFound {
attachmentsToAdd = append(attachmentsToAdd, indexedNetworkSelectionElements[key])
}
}
if len(attachmentsToAdd) > 0 {
results, err = pnc.handleDynamicInterfaceRequest(
&DynamicAttachmentRequest{
Pod: pod,
Attachments: attachmentsToAdd,
Type: add,
PodNetNS: netnsPath,
})
if err != nil {
klog.Errorf("error adding attachments: %v", err)
return true
}
}
var attachmentsToRemove []nadv1.NetworkSelectionElement
for key := range indexedNetworkStatus {
networkNamespace, networkName, _ := separateNamespaceAndName(key)
if _, wasFound := indexedNetworkSelectionElements[key]; !wasFound {
attachmentsToRemove = append(attachmentsToRemove, nadv1.NetworkSelectionElement{
Name: networkName,
Namespace: networkNamespace,
InterfaceRequest: indexedNetworkStatus[key].Interface,
})
}
}
if len(attachmentsToRemove) > 0 {
var res []annotations.AttachmentResult
res, err = pnc.handleDynamicInterfaceRequest(&DynamicAttachmentRequest{
Pod: pod,
Attachments: attachmentsToRemove,
Type: remove,
PodNetNS: netnsPath,
})
if err != nil {
klog.Errorf("error removing attachments: %v", err)
return true
}
results = append(results, res...)
}
return true
}
func (pnc *PodNetworksController) handleDynamicInterfaceRequest(
dynamicAttachmentRequest *DynamicAttachmentRequest,
) ([]annotations.AttachmentResult, error) {
klog.Infof("handleDynamicInterfaceRequest: read from queue: %v", dynamicAttachmentRequest)
if dynamicAttachmentRequest.Type == add {
return pnc.addNetworks(dynamicAttachmentRequest)
} else if dynamicAttachmentRequest.Type == remove {
return pnc.removeNetworks(dynamicAttachmentRequest)
} else {
klog.Infof("very weird attachment request: %+v", dynamicAttachmentRequest)
}
klog.Infof("handleDynamicInterfaceRequest: exited & successfully processed: %v", dynamicAttachmentRequest)
return nil, nil
}
func (pnc *PodNetworksController) handleResult(
err error,
namespacedPodName *string,
pod *corev1.Pod,
results []annotations.AttachmentResult,
) {
if results != nil {
updatedStatus, podNetworkStatusUpdateError := annotations.UpdatePodNetworkStatus(pod, results)
if podNetworkStatusUpdateError != nil {
klog.Errorf(
"error computing pod %q updated network status: %v",
namespacedPodName,
podNetworkStatusUpdateError,
)
}
if setNetworkStatusError := nadutils.SetNetworkStatus(
pnc.k8sClientSet,
pod,
updatedStatus,
); setNetworkStatusError != nil {
klog.Errorf("error updating pod %q network status: %v", namespacedPodName, setNetworkStatusError)
}
}
if err == nil {
pnc.workqueue.Forget(namespacedPodName)
return
}
currentRetries := pnc.workqueue.NumRequeues(namespacedPodName)
if currentRetries <= maxRetries {
klog.Errorf("re-queued request for: %s. Error: %v", *namespacedPodName, err)
pnc.workqueue.AddRateLimited(namespacedPodName)
return
}
pnc.workqueue.Forget(namespacedPodName)
}
func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj interface{}) {
oldPod := oldObj.(*corev1.Pod)
newPod := newObj.(*corev1.Pod)
if pnc.ignoreHostNetworkedPods(newPod) {
return
}
if !didNetworkSelectionElementsChange(oldPod, newPod) {
return
}
namespacedName := annotations.NamespacedName(oldPod.GetNamespace(), oldPod.GetName())
klog.V(logging.Debug).Infof("pod [%s] updated", namespacedName)
pnc.workqueue.Add(&namespacedName)
}
func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicAttachmentRequest) ([]annotations.AttachmentResult, error) {
pod := dynamicAttachmentRequest.Pod
var attachmentResults []annotations.AttachmentResult
for i := range dynamicAttachmentRequest.Attachments {
netToAdd := dynamicAttachmentRequest.Attachments[i]
klog.Infof("network to add: %v", netToAdd)
netAttachDef, err := pnc.netAttachDefLister.NetworkAttachmentDefinitions(netToAdd.Namespace).Get(netToAdd.Name)
if err != nil {
klog.Errorf("failed to access the networkattachmentdefinition %s/%s: %v", netToAdd.Namespace, netToAdd.Name, err)
return nil, err
}
netAttachDefWithDefaults, err := serializeNetAttachDefWithDefaults(netAttachDef)
if err != nil {
return nil, err
}
response, err := pnc.multusClient.InvokeDelegate(
multusapi.CreateDelegateRequest(
multuscni.CmdAdd,
podContainerID(pod),
dynamicAttachmentRequest.PodNetNS,
netToAdd.InterfaceRequest,
pod.GetNamespace(),
pod.GetName(),
string(pod.UID),
netAttachDefWithDefaults,
interfaceAttributes(netToAdd),
))
if err != nil {
return nil, fmt.Errorf("failed to ADD delegate: %v", err)
}
klog.Infof("response: %v", *response.Result)
attachmentResults = append(attachmentResults, *annotations.NewAttachmentResult(&netToAdd, response))
pnc.Eventf(pod, corev1.EventTypeNormal, "AddedInterface", addIfaceEventFormat(pod, &netToAdd))
klog.Infof(
"added interface %s to pod %s",
annotations.NetworkSelectionElementIndexKey(netToAdd),
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
)
}
return attachmentResults, nil
}
func (pnc *PodNetworksController) removeNetworks(
dynamicAttachmentRequest *DynamicAttachmentRequest,
) ([]annotations.AttachmentResult, error) {
pod := dynamicAttachmentRequest.Pod
var attachmentResults []annotations.AttachmentResult
for i := range dynamicAttachmentRequest.Attachments {
netToRemove := dynamicAttachmentRequest.Attachments[i]
klog.Infof("network to remove: %v", dynamicAttachmentRequest.Attachments[i])
netAttachDef, err := pnc.netAttachDefLister.NetworkAttachmentDefinitions(netToRemove.Namespace).Get(netToRemove.Name)
if err != nil {
klog.Errorf("failed to access the network-attachment-definition %s/%s: %v", netToRemove.Namespace, netToRemove.Name, err)
return nil, err
}
netAttachDefWithDefaults, err := serializeNetAttachDefWithDefaults(netAttachDef)
if err != nil {
return nil, err
}
_, err = pnc.multusClient.InvokeDelegate(
multusapi.CreateDelegateRequest(
multuscni.CmdDel,
podContainerID(pod),
dynamicAttachmentRequest.PodNetNS,
netToRemove.InterfaceRequest,
pod.GetNamespace(),
pod.GetName(),
string(pod.UID),
netAttachDefWithDefaults,
interfaceAttributes(netToRemove),
))
if err != nil {
return nil, fmt.Errorf("failed to remove delegate: %v", err)
}
attachmentResults = append(attachmentResults, *annotations.NewAttachmentResult(&netToRemove, nil))
pnc.Eventf(pod, corev1.EventTypeNormal, "RemovedInterface", removeIfaceEventFormat(pod, &netToRemove))
klog.Infof(
"removed interface %s from pod %s",
annotations.NetworkSelectionElementIndexKey(netToRemove),
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
)
}
return attachmentResults, nil
}
// Eventf puts event into kubernetes events
func (pnc *PodNetworksController) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
if pnc != nil && pnc.recorder != nil {
pnc.recorder.Eventf(object, eventtype, reason, messageFmt, args...)
}
}
func (pnc *PodNetworksController) netnsPath(pod *corev1.Pod) (string, error) {
if containerID := podContainerID(pod); containerID != "" {
netns, err := pnc.containerRuntime.NetNS(containerID)
if err != nil {
return "", fmt.Errorf("failed to get netns for container [%s]: %w", containerID, err)
}
return netns, nil
}
return "", nil
}
func podContainerID(pod *corev1.Pod) string {
cidURI := pod.Status.ContainerStatuses[0].ContainerID
// format is docker://<cid>
parts := strings.Split(cidURI, "//")
if len(parts) > 1 {
return parts[1]
}
return cidURI
}
func addIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement) string {
attributes := ""
if len(network.IPRequest) > 0 || network.MacRequest != "" || network.CNIArgs != nil {
attributes = fmt.Sprintf("(ips: %v, mac: %s, cni-args: %v)",
network.IPRequest,
network.MacRequest,
network.CNIArgs,
)
}
return fmt.Sprintf(
"pod [%s]: added interface %s to network: %s%s",
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
network.InterfaceRequest,
network.Name,
attributes,
)
}
func removeIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElement) string {
return fmt.Sprintf(
"pod [%s]: removed interface %s from network: %s",
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
network.InterfaceRequest,
network.Name,
)
}
func rejectInterfaceAddEventFormat(pod *corev1.Pod) string {
return fmt.Sprintf(
"pod [%s]: will not add interface to host networked pod",
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
)
}
func interfaceAttributes(networkData nadv1.NetworkSelectionElement) *multusapi.DelegateInterfaceAttributes {
if len(networkData.IPRequest) > 0 || networkData.MacRequest != "" || networkData.CNIArgs != nil {
return &multusapi.DelegateInterfaceAttributes{
IPRequest: networkData.IPRequest,
MacRequest: networkData.MacRequest,
CNIArgs: networkData.CNIArgs,
}
}
return nil
}
func serializeNetAttachDefWithDefaults(netAttachDef *nadv1.NetworkAttachmentDefinition) ([]byte, error) {
netAttachDefWithDefaults, err := nadutils.GetCNIConfigFromSpec(netAttachDef.Spec.Config, netAttachDef.GetName())
if err != nil {
return nil, fmt.Errorf(
"failed to apply defaults to the net-attach-def %s: %v",
annotations.NamespacedName(netAttachDef.GetNamespace(), netAttachDef.GetName()),
err,
)
}
return netAttachDefWithDefaults, nil
}
func didNetworkSelectionElementsChange(oldPod *corev1.Pod, newPod *corev1.Pod) bool {
oldNetworkSelectionElementsString, didPodHaveExtraAttachments := oldPod.Annotations[nadv1.NetworkAttachmentAnnot]
newNetworkSelectionElementsString, doesPodHaveExtraAttachmentsNow := newPod.Annotations[nadv1.NetworkAttachmentAnnot]
if didPodHaveExtraAttachments != doesPodHaveExtraAttachmentsNow ||
oldNetworkSelectionElementsString != newNetworkSelectionElementsString {
return true
}
return false
}
func separateNamespaceAndName(namespacedName string) (namespace string, name string, err error) {
splitNamespacedName := strings.Split(namespacedName, "/")
if len(splitNamespacedName) != 2 && len(splitNamespacedName) != 3 {
return "", "", fmt.Errorf("invalid namespaced name: %s", namespacedName)
}
return splitNamespacedName[0], splitNamespacedName[1], nil
}