@@ -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,58 @@ 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
+ expectedEventPayload := fmt .Sprintf (
275
+ "Normal AddedInterface pod [%s]: added interface %s to network: %s (ips: [172.16.0.1], mac: 02:03:04:05:06:07, cni-args: &map[foo:bar])" ,
276
+ annotations .NamespacedName (namespace , podName ),
277
+ "net1" ,
278
+ networkToAdd ,
279
+ )
280
+ Eventually (<- eventRecorder .Events ).Should (Equal (expectedEventPayload ))
281
+ })
282
+
283
+ It ("the pod network-status is updated with the new network attachment" , func () {
284
+ Eventually (func () ([]nad.NetworkStatus , error ) {
285
+ updatedPod , err := k8sClient .CoreV1 ().Pods (namespace ).Get (context .TODO (), podName , metav1.GetOptions {})
286
+ if err != nil {
287
+ return nil , err
288
+ }
289
+ status , err := annotations .PodDynamicNetworkStatus (updatedPod )
290
+ if err != nil {
291
+ return nil , err
292
+ }
293
+ return status , nil
294
+ }).Should (ConsistOf (
295
+ ifaceStatus (namespace , networkName , "net0" , "" ),
296
+ ifaceStatus (namespace , networkToAdd , "net1" , macAddr )))
297
+ })
298
+ })
299
+
247
300
})
248
301
})
249
302
})
@@ -425,7 +478,7 @@ func podNetworkConfig(networkNames ...string) map[string]string {
425
478
}
426
479
}
427
480
428
- func generateNetworkSelectionAnnotation (namespace string , networkNames ... string ) string {
481
+ func generateNetworkSelectionElements (namespace string , networkNames ... string ) []nad. NetworkSelectionElement {
429
482
var netSelectionElements []nad.NetworkSelectionElement
430
483
for i , networkName := range networkNames {
431
484
netSelectionElements = append (
@@ -439,6 +492,11 @@ func generateNetworkSelectionAnnotation(namespace string, networkNames ...string
439
492
if netSelectionElements == nil {
440
493
netSelectionElements = make ([]nad.NetworkSelectionElement , 0 )
441
494
}
495
+ return netSelectionElements
496
+ }
497
+
498
+ func generateNetworkSelectionAnnotation (namespace string , networkNames ... string ) string {
499
+ netSelectionElements := generateNetworkSelectionElements (namespace , networkNames ... )
442
500
serelizedNetSelectionElements , err := json .Marshal (netSelectionElements )
443
501
if err != nil {
444
502
return ""
0 commit comments