Skip to content

Commit

Permalink
Allow assign-macvlan annotation to specify an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
danwinship authored and cherrypicker committed Nov 20, 2017
1 parent 250e6d7 commit 4a99886
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions pkg/network/node/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ func getVethInfo(netns, containerIfname string) (string, string, string, error)

// Adds a macvlan interface to a container, if requested, for use with the egress router feature
func maybeAddMacvlan(pod *kapi.Pod, netns string) error {
val, ok := pod.Annotations[networkapi.AssignMacvlanAnnotation]
if !ok || val != "true" {
annotation, ok := pod.Annotations[networkapi.AssignMacvlanAnnotation]
if !ok || annotation == "false" {
return nil
}

Expand All @@ -393,23 +393,31 @@ func maybeAddMacvlan(pod *kapi.Pod, netns string) error {
return fmt.Errorf("pod has %q annotation but is not privileged", networkapi.AssignMacvlanAnnotation)
}

// Find interface with the default route
var defIface netlink.Link
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return fmt.Errorf("failed to read routes: %v", err)
}
var iface netlink.Link
var err error
if annotation == "true" {
// Find interface with the default route
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return fmt.Errorf("failed to read routes: %v", err)
}

for _, r := range routes {
if r.Dst == nil {
defIface, err = netlink.LinkByIndex(r.LinkIndex)
if err != nil {
return fmt.Errorf("failed to get default route interface: %v", err)
for _, r := range routes {
if r.Dst == nil {
iface, err = netlink.LinkByIndex(r.LinkIndex)
if err != nil {
return fmt.Errorf("failed to get default route interface: %v", err)
}
}
}
}
if defIface == nil {
return fmt.Errorf("failed to find default route interface")
if iface == nil {
return fmt.Errorf("failed to find default route interface")
}
} else {
iface, err = netlink.LinkByName(annotation)
if err != nil {
return fmt.Errorf("pod annotation %q is neither 'true' nor the name of a local network interface", networkapi.AssignMacvlanAnnotation)
}
}

podNs, err := ns.GetNS(netns)
Expand All @@ -420,9 +428,9 @@ func maybeAddMacvlan(pod *kapi.Pod, netns string) error {

err = netlink.LinkAdd(&netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
MTU: defIface.Attrs().MTU,
MTU: iface.Attrs().MTU,
Name: "macvlan0",
ParentIndex: defIface.Attrs().Index,
ParentIndex: iface.Attrs().Index,
Namespace: netlink.NsFd(podNs.Fd()),
},
Mode: netlink.MACVLAN_MODE_PRIVATE,
Expand Down

0 comments on commit 4a99886

Please sign in to comment.