@@ -377,8 +377,8 @@ func getVethInfo(netns, containerIfname string) (string, string, string, error)
377377
378378// Adds a macvlan interface to a container, if requested, for use with the egress router feature
379379func maybeAddMacvlan (pod * kapi.Pod , netns string ) error {
380- val , ok := pod .Annotations [networkapi .AssignMacvlanAnnotation ]
381- if ! ok || val != "true " {
380+ annotation , ok := pod .Annotations [networkapi .AssignMacvlanAnnotation ]
381+ if ! ok || annotation == "false " {
382382 return nil
383383 }
384384
@@ -393,23 +393,31 @@ func maybeAddMacvlan(pod *kapi.Pod, netns string) error {
393393 return fmt .Errorf ("pod has %q annotation but is not privileged" , networkapi .AssignMacvlanAnnotation )
394394 }
395395
396- // Find interface with the default route
397- var defIface netlink.Link
398- routes , err := netlink .RouteList (nil , netlink .FAMILY_V4 )
399- if err != nil {
400- return fmt .Errorf ("failed to read routes: %v" , err )
401- }
396+ var iface netlink.Link
397+ var err error
398+ if annotation == "true" {
399+ // Find interface with the default route
400+ routes , err := netlink .RouteList (nil , netlink .FAMILY_V4 )
401+ if err != nil {
402+ return fmt .Errorf ("failed to read routes: %v" , err )
403+ }
402404
403- for _ , r := range routes {
404- if r .Dst == nil {
405- defIface , err = netlink .LinkByIndex (r .LinkIndex )
406- if err != nil {
407- return fmt .Errorf ("failed to get default route interface: %v" , err )
405+ for _ , r := range routes {
406+ if r .Dst == nil {
407+ iface , err = netlink .LinkByIndex (r .LinkIndex )
408+ if err != nil {
409+ return fmt .Errorf ("failed to get default route interface: %v" , err )
410+ }
408411 }
409412 }
410- }
411- if defIface == nil {
412- return fmt .Errorf ("failed to find default route interface" )
413+ if iface == nil {
414+ return fmt .Errorf ("failed to find default route interface" )
415+ }
416+ } else {
417+ iface , err = netlink .LinkByName (annotation )
418+ if err != nil {
419+ return fmt .Errorf ("pod annotation %q is neither 'true' nor the name of a local network interface" , networkapi .AssignMacvlanAnnotation )
420+ }
413421 }
414422
415423 podNs , err := ns .GetNS (netns )
@@ -420,9 +428,9 @@ func maybeAddMacvlan(pod *kapi.Pod, netns string) error {
420428
421429 err = netlink .LinkAdd (& netlink.Macvlan {
422430 LinkAttrs : netlink.LinkAttrs {
423- MTU : defIface .Attrs ().MTU ,
431+ MTU : iface .Attrs ().MTU ,
424432 Name : "macvlan0" ,
425- ParentIndex : defIface .Attrs ().Index ,
433+ ParentIndex : iface .Attrs ().Index ,
426434 Namespace : netlink .NsFd (podNs .Fd ()),
427435 },
428436 Mode : netlink .MACVLAN_MODE_PRIVATE ,
0 commit comments