Skip to content

Commit

Permalink
Add CNI modify for dualstack
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Dec 10, 2020
1 parent e36024c commit a6fef94
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dist/images/kube-ovn-monitor
kube-ovn.yaml
kube-ovn-crd.yaml
ovn.yaml
kind.yaml
75 changes: 54 additions & 21 deletions cmd/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alauda/kube-ovn/pkg/util"
"net"
"runtime"
"strings"

"github.com/alauda/kube-ovn/pkg/util"

kubeovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/alauda/kube-ovn/pkg/request"
"github.com/containernetworking/cni/pkg/skel"
Expand Down Expand Up @@ -66,31 +67,33 @@ func generateCNIResult(cniVersion string, cniResponse *request.CniResponse) curr
_, mask, _ := net.ParseCIDR(cniResponse.CIDR)
switch cniResponse.Protocol {
case kubeovnv1.ProtocolIPv4:
ip := current.IPConfig{
Version: "4",
Address: net.IPNet{IP: net.ParseIP(cniResponse.IpAddress).To4(), Mask: mask.Mask},
Gateway: net.ParseIP(cniResponse.Gateway).To4(),
}
ip, route := assignV4Address(cniResponse.IpAddress, cniResponse.Gateway, mask)
result.IPs = []*current.IPConfig{&ip}

route := types.Route{
Dst: net.IPNet{IP: net.ParseIP("0.0.0.0").To4(), Mask: net.CIDRMask(0, 32)},
GW: net.ParseIP(cniResponse.Gateway).To4(),
}
result.Routes = []*types.Route{&route}
case kubeovnv1.ProtocolIPv6:
ip := current.IPConfig{
Version: "6",
Address: net.IPNet{IP: net.ParseIP(cniResponse.IpAddress).To16(), Mask: mask.Mask},
Gateway: net.ParseIP(cniResponse.Gateway).To16(),
}
ip, route := assignV6Address(cniResponse.IpAddress, cniResponse.Gateway, mask)
result.IPs = []*current.IPConfig{&ip}

route := types.Route{
Dst: net.IPNet{IP: net.ParseIP("::").To16(), Mask: net.CIDRMask(0, 128)},
GW: net.ParseIP(cniResponse.Gateway).To16(),
}
result.Routes = []*types.Route{&route}
case kubeovnv1.ProtocolDual:
var netMask *net.IPNet
for _, cidrBlock := range strings.Split(cniResponse.CIDR, ",") {
_, netMask, _ = net.ParseCIDR(cidrBlock)
if util.CheckProtocol(cidrBlock) == kubeovnv1.ProtocolIPv4 {
ipStr := strings.Split(cniResponse.IpAddress, ",")[0]
gwStr := strings.Split(cniResponse.Gateway, ",")[0]

ip, route := assignV4Address(ipStr, gwStr, netMask)
result.IPs = append(result.IPs, &ip)
result.Routes = append(result.Routes, &route)
} else if util.CheckProtocol(cidrBlock) == kubeovnv1.ProtocolIPv6 {
ipStr := strings.Split(cniResponse.IpAddress, ",")[1]
gwStr := strings.Split(cniResponse.Gateway, ",")[1]

ip, route := assignV6Address(ipStr, gwStr, netMask)
result.IPs = append(result.IPs, &ip)
result.Routes = append(result.Routes, &route)
}
}
}

return result
Expand Down Expand Up @@ -173,3 +176,33 @@ func parseValueFromArgs(key, argString string) (string, error) {
}
return "", fmt.Errorf("%s is required in CNI_ARGS", key)
}

func assignV4Address(ipAddress, gateway string, mask *net.IPNet) (current.IPConfig, types.Route) {
ip := current.IPConfig{
Version: "4",
Address: net.IPNet{IP: net.ParseIP(ipAddress).To4(), Mask: mask.Mask},
Gateway: net.ParseIP(gateway).To4(),
}

route := types.Route{
Dst: net.IPNet{IP: net.ParseIP("0.0.0.0").To4(), Mask: net.CIDRMask(0, 32)},
GW: net.ParseIP(gateway).To4(),
}

return ip, route
}

func assignV6Address(ipAddress, gateway string, mask *net.IPNet) (current.IPConfig, types.Route) {
ip := current.IPConfig{
Version: "6",
Address: net.IPNet{IP: net.ParseIP(ipAddress).To16(), Mask: mask.Mask},
Gateway: net.ParseIP(gateway).To16(),
}

route := types.Route{
Dst: net.IPNet{IP: net.ParseIP("::").To16(), Mask: net.CIDRMask(0, 128)},
GW: net.ParseIP(gateway).To16(),
}

return ip, route
}
4 changes: 2 additions & 2 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,13 +834,13 @@ func (c *Controller) reconcileGateway(subnet *kubeovnv1.Subnet) error {
return err
}
}
gw, err := getNodeTunlIP(node)
nodeIP, err := getNodeTunlIP(node)
if err != nil {
klog.Errorf("failed to get node %s tunl ip, %v", node.Name, err)
return err
}

nextHop := getNextHopByTunnelIP(gw)
nextHop := getNextHopByTunnelIP(nodeIP)
if pod.Annotations[util.NorthGatewayAnnotation] != "" {
nextHop = pod.Annotations[util.NorthGatewayAnnotation]
}
Expand Down
20 changes: 15 additions & 5 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,22 @@ func LastIP(subnet string) (string, error) {
}

func CIDRConflict(a, b string) bool {
aIp, aIpNet, aErr := net.ParseCIDR(a)
bIp, bIpNet, bErr := net.ParseCIDR(b)
if aErr != nil || bErr != nil {
return false
for _, cidra := range strings.Split(a, ",") {
for _, cidrb := range strings.Split(b, ",") {
if CheckProtocol(cidra) != CheckProtocol(cidrb) {
continue
}
aIp, aIpNet, aErr := net.ParseCIDR(cidra)
bIp, bIpNet, bErr := net.ParseCIDR(cidrb)
if aErr != nil || bErr != nil {
return false
}
if aIpNet.Contains(bIp) || bIpNet.Contains(aIp) {
return true
}
}
}
return aIpNet.Contains(bIp) || bIpNet.Contains(aIp)
return false
}

func CIDRContainIP(cidrStr, ipStr string) bool {
Expand Down
23 changes: 0 additions & 23 deletions yamls/kind.yaml

This file was deleted.

0 comments on commit a6fef94

Please sign in to comment.