Skip to content

Commit

Permalink
add flannel to support dual stack ip
Browse files Browse the repository at this point in the history
support three mode ip stack:
- only ipv4 stack
- only ipv6 stack
- dual stack ip

Signed-off-by: yaoice <[email protected]>
  • Loading branch information
yaoice authored and squeed committed Jan 27, 2021
1 parent d1769dd commit 8feef71
Show file tree
Hide file tree
Showing 3 changed files with 519 additions and 101 deletions.
22 changes: 18 additions & 4 deletions plugins/meta/flannel/flannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ type NetConf struct {
type subnetEnv struct {
nw *net.IPNet
sn *net.IPNet
ip6Nw *net.IPNet
ip6Sn *net.IPNet
mtu *uint
ipmasq *bool
}

func (se *subnetEnv) missing() string {
m := []string{}

if se.nw == nil {
m = append(m, "FLANNEL_NETWORK")
if se.nw == nil && se.ip6Nw == nil {
m = append(m, []string{"FLANNEL_NETWORK", "FLANNEL_IPV6_NETWORK"}...)
}
if se.sn == nil {
m = append(m, "FLANNEL_SUBNET")
if se.sn == nil && se.ip6Sn == nil {
m = append(m, []string{"FLANNEL_SUBNET", "FLANNEL_IPV6_SUBNET"}...)
}
if se.mtu == nil {
m = append(m, "FLANNEL_MTU")
Expand Down Expand Up @@ -128,6 +130,18 @@ func loadFlannelSubnetEnv(fn string) (*subnetEnv, error) {
return nil, err
}

case "FLANNEL_IPV6_NETWORK":
_, se.ip6Nw, err = net.ParseCIDR(parts[1])
if err != nil {
return nil, err
}

case "FLANNEL_IPV6_SUBNET":
_, se.ip6Sn, err = net.ParseCIDR(parts[1])
if err != nil {
return nil, err
}

case "FLANNEL_MTU":
mtu, err := strconv.ParseUint(parts[1], 10, 32)
if err != nil {
Expand Down
24 changes: 22 additions & 2 deletions plugins/meta/flannel/flannel_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,33 @@ func getDelegateIPAM(n *NetConf, fenv *subnetEnv) (map[string]interface{}, error
if !hasKey(ipam, "type") {
ipam["type"] = "host-local"
}
ipam["subnet"] = fenv.sn.String()

var rangesSlice [][]map[string]interface{}

if fenv.sn != nil && fenv.sn.String() != "" {
rangesSlice = append(rangesSlice, []map[string]interface{}{
{"subnet": fenv.sn.String()},
},
)
}
if fenv.ip6Sn != nil && fenv.ip6Sn.String() != "" {
rangesSlice = append(rangesSlice, []map[string]interface{}{
{"subnet": fenv.ip6Sn.String()},
},
)
}
ipam["ranges"] = rangesSlice

rtes, err := getIPAMRoutes(n)
if err != nil {
return nil, fmt.Errorf("failed to read IPAM routes: %w", err)
}
rtes = append(rtes, types.Route{Dst: *fenv.nw})
if fenv.nw != nil {
rtes = append(rtes, types.Route{Dst: *fenv.nw})
}
if fenv.ip6Nw != nil {
rtes = append(rtes, types.Route{Dst: *fenv.ip6Nw})
}
ipam["routes"] = rtes

return ipam, nil
Expand Down
Loading

0 comments on commit 8feef71

Please sign in to comment.