Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry-pick of #17334 on release-3.7 #17383

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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