@@ -66,11 +66,13 @@ 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"
72
73
podName = "tiny-winy-pod"
73
74
)
75
+ cniArgs := & map [string ]string {"foo" : "bar" }
74
76
var (
75
77
eventRecorder * record.FakeRecorder
76
78
k8sClient k8sclient.Interface
@@ -168,8 +170,8 @@ var _ = Describe("Dynamic Attachment controller", func() {
168
170
}
169
171
return status , nil
170
172
}).Should (ConsistOf (
171
- ifaceStatus ( namespace , networkName , "net0" , "" ),
172
- ifaceStatus ( namespace , networkToAdd , "net1" , macAddr )))
173
+ ifaceStatusForDefaultNamespace ( networkName , "net0" , "" ),
174
+ ifaceStatusForDefaultNamespace ( networkToAdd , "net1" , macAddr )))
173
175
})
174
176
})
175
177
@@ -233,7 +235,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
233
235
}
234
236
return status , nil
235
237
}).Should (ConsistOf (
236
- ifaceStatus ( namespace , networkName , "net0" , "" )))
238
+ ifaceStatusForDefaultNamespace ( networkName , "net0" , "" )))
237
239
})
238
240
239
241
It ("throws an event indicating the interface add operation is rejected" , func () {
@@ -244,6 +246,61 @@ var _ = Describe("Dynamic Attachment controller", func() {
244
246
Eventually (<- eventRecorder .Events ).Should (Equal (expectedEventPayload ))
245
247
})
246
248
})
249
+
250
+ When ("an attachment is added with attributes (IPs, MAC, cni-args)" , func () {
251
+ JustBeforeEach (func () {
252
+ pod = updatePodSpec (pod )
253
+ netSelectionElements := append (generateNetworkSelectionElements (namespace , networkName ),
254
+ nad.NetworkSelectionElement {
255
+ Name : networkToAdd ,
256
+ Namespace : namespace ,
257
+ InterfaceRequest : "net1" ,
258
+ IPRequest : []string {ipAddr },
259
+ MacRequest : macAddr ,
260
+ CNIArgs : & map [string ]interface {}{
261
+ "foo" : "bar" ,
262
+ },
263
+ },
264
+ )
265
+ serelizedNetSelectionElements , _ := json .Marshal (netSelectionElements )
266
+ pod .Annotations [nad .NetworkAttachmentAnnot ] = string (serelizedNetSelectionElements )
267
+ _ , err := k8sClient .CoreV1 ().Pods (namespace ).UpdateStatus (
268
+ context .TODO (),
269
+ pod ,
270
+ metav1.UpdateOptions {})
271
+ Expect (err ).NotTo (HaveOccurred ())
272
+ })
273
+
274
+ It ("an `AddedInterface` event is seen in the event recorded" , func () {
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
+ ifaceStatusForDefaultNamespace (networkName , "net0" , "" ),
300
+ ifaceStatusForDefaultNamespace (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 ""
@@ -479,7 +541,8 @@ func dummyMultusConfig() string {
479
541
}`
480
542
}
481
543
482
- func ifaceStatus (namespace , networkName , ifaceName , macAddress string ) nad.NetworkStatus {
544
+ func ifaceStatusForDefaultNamespace (networkName , ifaceName , macAddress string ) nad.NetworkStatus {
545
+ const namespace = "default"
483
546
return nad.NetworkStatus {
484
547
Name : fmt .Sprintf ("%s/%s" , namespace , networkName ),
485
548
Interface : ifaceName ,
0 commit comments