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

fix vlan subnet use logical gw can not access outside cluster node #3007

Merged
merged 1 commit into from
Jul 7, 2023
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
36 changes: 30 additions & 6 deletions pkg/daemon/controller_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,15 @@ func (c *Controller) reconcileRouters(event *subnetEvent) error {
return fmt.Errorf("failed to get nic %s", util.NodeNic)
}

existRoutes, err := getNicExistRoutes(nic, gateway)
allRoutes, err := getNicExistRoutes(nil, gateway)
if err != nil {
return err
}

toAdd, toDel := routeDiff(existRoutes, cidrs, joinCIDR, gateway, net.ParseIP(nodeIPv4), net.ParseIP(nodeIPv6))
nodeNicRoutes, err := getNicExistRoutes(nic, gateway)
if err != nil {
return err
}
toAdd, toDel := routeDiff(nodeNicRoutes, allRoutes, cidrs, joinCIDR, gateway, net.ParseIP(nodeIPv4), net.ParseIP(nodeIPv6))
for _, r := range toDel {
if err = netlink.RouteDel(&netlink.Route{Dst: r.Dst}); err != nil {
klog.Errorf("failed to del route %v", err)
Expand Down Expand Up @@ -268,8 +271,8 @@ func getNicExistRoutes(nic netlink.Link, gateway string) ([]netlink.Route, error
return existRoutes, nil
}

func routeDiff(existRoutes []netlink.Route, cidrs, joinCIDR []string, gateway string, srcIPv4, srcIPv6 net.IP) (toAdd, toDel []netlink.Route) {
for _, route := range existRoutes {
func routeDiff(nodeNicRoutes, allRoutes []netlink.Route, cidrs, joinCIDR []string, gateway string, srcIPv4, srcIPv6 net.IP) (toAdd, toDel []netlink.Route) {
for _, route := range nodeNicRoutes {
if route.Scope == netlink.SCOPE_LINK || route.Dst == nil || route.Dst.IP.IsLinkLocalUnicast() {
continue
}
Expand All @@ -284,6 +287,17 @@ func routeDiff(existRoutes []netlink.Route, cidrs, joinCIDR []string, gateway st
if !found {
toDel = append(toDel, route)
}
conflict := false
for _, ar := range allRoutes {
if ar.Dst != nil && ar.Dst.String() == route.Dst.String() && ar.LinkIndex != route.LinkIndex {
// route conflict
conflict = true
break
}
}
if conflict {
toDel = append(toDel, route)
}
}
if len(toDel) > 0 {
klog.Infof("route to del %v", toDel)
Expand All @@ -305,7 +319,17 @@ func routeDiff(existRoutes []netlink.Route, cidrs, joinCIDR []string, gateway st
}

found := false
for _, r := range existRoutes {
for _, ar := range allRoutes {
if ar.Dst != nil && ar.Dst.String() == c {
// route already exist
found = true
break
}
}
if found {
continue
}
for _, r := range nodeNicRoutes {
if r.Dst == nil || r.Dst.String() != c {
continue
}
Expand Down