@@ -66,6 +66,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
66
66
Context ("with an existing running pod" , func () {
67
67
const (
68
68
cniVersion = "0.3.0"
69
+ ipAddr = "172.16.0.1"
69
70
macAddr = "02:03:04:05:06:07"
70
71
namespace = "default"
71
72
networkName = "tiny-net"
@@ -244,6 +245,62 @@ var _ = Describe("Dynamic Attachment controller", func() {
244
245
Eventually (<- eventRecorder .Events ).Should (Equal (expectedEventPayload ))
245
246
})
246
247
})
248
+
249
+ When ("an attachment is added with attributes (IPs, MAC, cni-args)" , func () {
250
+ JustBeforeEach (func () {
251
+ pod = updatePodSpec (pod )
252
+ netSelectionElements := append (generateNetworkSelectionElements (namespace , networkName ),
253
+ nad.NetworkSelectionElement {
254
+ Name : networkToAdd ,
255
+ Namespace : namespace ,
256
+ InterfaceRequest : "net1" ,
257
+ IPRequest : []string {ipAddr },
258
+ MacRequest : macAddr ,
259
+ CNIArgs : & map [string ]interface {}{
260
+ "foo" : "bar" ,
261
+ },
262
+ },
263
+ )
264
+ serelizedNetSelectionElements , _ := json .Marshal (netSelectionElements )
265
+ pod .Annotations [nad .NetworkAttachmentAnnot ] = string (serelizedNetSelectionElements )
266
+ _ , err := k8sClient .CoreV1 ().Pods (namespace ).UpdateStatus (
267
+ context .TODO (),
268
+ pod ,
269
+ metav1.UpdateOptions {})
270
+ Expect (err ).NotTo (HaveOccurred ())
271
+ })
272
+
273
+ It ("an `AddedInterface` event is seen in the event recorded" , func () {
274
+ cniArgs := & map [string ]string {"foo" : "bar" }
275
+ expectedEventPayload := fmt .Sprintf (
276
+ "Normal AddedInterface pod [%s]: added interface %s to network: %s (ips: [%s], mac: %s, cni-args: %v)" ,
277
+ annotations .NamespacedName (namespace , podName ),
278
+ "net1" ,
279
+ networkToAdd ,
280
+ ipAddr ,
281
+ macAddr ,
282
+ cniArgs ,
283
+ )
284
+ Eventually (<- eventRecorder .Events ).Should (Equal (expectedEventPayload ))
285
+ })
286
+
287
+ It ("the pod network-status is updated with the new network attachment" , func () {
288
+ Eventually (func () ([]nad.NetworkStatus , error ) {
289
+ updatedPod , err := k8sClient .CoreV1 ().Pods (namespace ).Get (context .TODO (), podName , metav1.GetOptions {})
290
+ if err != nil {
291
+ return nil , err
292
+ }
293
+ status , err := annotations .PodDynamicNetworkStatus (updatedPod )
294
+ if err != nil {
295
+ return nil , err
296
+ }
297
+ return status , nil
298
+ }).Should (ConsistOf (
299
+ ifaceStatus (namespace , networkName , "net0" , "" ),
300
+ ifaceStatus (namespace , networkToAdd , "net1" , macAddr )))
301
+ })
302
+ })
303
+
247
304
})
248
305
})
249
306
})
@@ -425,7 +482,7 @@ func podNetworkConfig(networkNames ...string) map[string]string {
425
482
}
426
483
}
427
484
428
- func generateNetworkSelectionAnnotation (namespace string , networkNames ... string ) string {
485
+ func generateNetworkSelectionElements (namespace string , networkNames ... string ) []nad. NetworkSelectionElement {
429
486
var netSelectionElements []nad.NetworkSelectionElement
430
487
for i , networkName := range networkNames {
431
488
netSelectionElements = append (
@@ -439,6 +496,11 @@ func generateNetworkSelectionAnnotation(namespace string, networkNames ...string
439
496
if netSelectionElements == nil {
440
497
netSelectionElements = make ([]nad.NetworkSelectionElement , 0 )
441
498
}
499
+ return netSelectionElements
500
+ }
501
+
502
+ func generateNetworkSelectionAnnotation (namespace string , networkNames ... string ) string {
503
+ netSelectionElements := generateNetworkSelectionElements (namespace , networkNames ... )
442
504
serelizedNetSelectionElements , err := json .Marshal (netSelectionElements )
443
505
if err != nil {
444
506
return ""
0 commit comments