|
@@ -18,6 +18,7 @@ import (
|
|
|
"errors"
|
|
|
"flag"
|
|
|
"fmt"
|
|
|
+ "math/big"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"os"
|
|
@@ -80,6 +81,8 @@ type CmdLineOpts struct {
|
|
|
etcdPassword string
|
|
|
help bool
|
|
|
version bool
|
|
|
+ autoDetectIPv4 bool
|
|
|
+ autoDetectIPv6 bool
|
|
|
kubeSubnetMgr bool
|
|
|
kubeApiUrl string
|
|
|
kubeAnnotationPrefix string
|
|
@@ -90,6 +93,7 @@ type CmdLineOpts struct {
|
|
|
subnetFile string
|
|
|
subnetDir string
|
|
|
publicIP string
|
|
|
+ publicIPv6 string
|
|
|
subnetLeaseRenewMargin int
|
|
|
healthzIP string
|
|
|
healthzPort int
|
|
@@ -108,6 +112,13 @@ var (
|
|
|
flannelFlags = flag.NewFlagSet("flannel", flag.ExitOnError)
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ ipv4Stack int = iota
|
|
|
+ ipv6Stack
|
|
|
+ dualStack
|
|
|
+ noneStack
|
|
|
+)
|
|
|
+
|
|
|
func init() {
|
|
|
flannelFlags.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001,http://127.0.0.1:2379", "a comma-delimited list of etcd endpoints")
|
|
|
flannelFlags.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
|
|
@@ -120,8 +131,11 @@ func init() {
|
|
|
flannelFlags.Var(&opts.ifaceRegex, "iface-regex", "regex expression to match the first interface to use (IP or name) for inter-host communication. Can be specified multiple times to check each regex in order. Returns the first match found. Regexes are checked after specific interfaces specified by the iface option have already been checked.")
|
|
|
flannelFlags.StringVar(&opts.subnetFile, "subnet-file", "/run/flannel/subnet.env", "filename where env variables (subnet, MTU, ... ) will be written to")
|
|
|
flannelFlags.StringVar(&opts.publicIP, "public-ip", "", "IP accessible by other nodes for inter-host communication")
|
|
|
+ flannelFlags.StringVar(&opts.publicIPv6, "public-ipv6", "", "IPv6 accessible by other nodes for inter-host communication")
|
|
|
flannelFlags.IntVar(&opts.subnetLeaseRenewMargin, "subnet-lease-renew-margin", 60, "subnet lease renewal margin, in minutes, ranging from 1 to 1439")
|
|
|
flannelFlags.BoolVar(&opts.ipMasq, "ip-masq", false, "setup IP masquerade rule for traffic destined outside of overlay network")
|
|
|
+ flannelFlags.BoolVar(&opts.autoDetectIPv4, "auto-detect-ipv4", true, "auto detect ipv4 address of the iface")
|
|
|
+ flannelFlags.BoolVar(&opts.autoDetectIPv6, "auto-detect-ipv6", false, "auto detect ipv6 address of the iface")
|
|
|
flannelFlags.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "contact the Kubernetes API for subnet assignment instead of etcd.")
|
|
|
flannelFlags.StringVar(&opts.kubeApiUrl, "kube-api-url", "", "Kubernetes API server URL. Does not need to be specified if flannel is running in a pod.")
|
|
|
flannelFlags.StringVar(&opts.kubeAnnotationPrefix, "kube-annotation-prefix", "flannel.alpha.coreos.com", `Kubernetes annotation prefix. Can contain single slash "/", otherwise it will be appended at the end.`)
|
|
@@ -162,6 +176,17 @@ func usage() {
|
|
|
os.Exit(0)
|
|
|
}
|
|
|
|
|
|
+func getIPFamily(autoDetectIPv4, autoDetectIPv6 bool) (int, error) {
|
|
|
+ if autoDetectIPv4 && !autoDetectIPv6 {
|
|
|
+ return ipv4Stack, nil
|
|
|
+ } else if !autoDetectIPv4 && autoDetectIPv6 {
|
|
|
+ return ipv6Stack, nil
|
|
|
+ } else if autoDetectIPv4 && autoDetectIPv6 {
|
|
|
+ return dualStack, nil
|
|
|
+ }
|
|
|
+ return noneStack, errors.New("none defined stack")
|
|
|
+}
|
|
|
+
|
|
|
func newSubnetManager(ctx context.Context) (subnet.Manager, error) {
|
|
|
if opts.kubeSubnetMgr {
|
|
|
return kube.NewSubnetManager(ctx, opts.kubeApiUrl, opts.kubeConfigFile, opts.kubeAnnotationPrefix, opts.netConfPath, opts.setNodeNetworkUnavailable)
|
|
@@ -200,12 +225,18 @@ func main() {
|
|
|
os.Exit(1)
|
|
|
}
|
|
|
|
|
|
+ // Get ip family stack
|
|
|
+ ipStack, stackErr := getIPFamily(opts.autoDetectIPv4, opts.autoDetectIPv6)
|
|
|
+ if stackErr != nil {
|
|
|
+ log.Error(stackErr.Error())
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
// Work out which interface to use
|
|
|
var extIface *backend.ExternalInterface
|
|
|
var err error
|
|
|
// Check the default interface only if no interfaces are specified
|
|
|
if len(opts.iface) == 0 && len(opts.ifaceRegex) == 0 {
|
|
|
- extIface, err = LookupExtIface(opts.publicIP, "")
|
|
|
+ extIface, err = LookupExtIface(opts.publicIP, "", ipStack)
|
|
|
if err != nil {
|
|
|
log.Error("Failed to find any valid interface to use: ", err)
|
|
|
os.Exit(1)
|
|
@@ -213,7 +244,7 @@ func main() {
|
|
|
} else {
|
|
|
// Check explicitly specified interfaces
|
|
|
for _, iface := range opts.iface {
|
|
|
- extIface, err = LookupExtIface(iface, "")
|
|
|
+ extIface, err = LookupExtIface(iface, "", ipStack)
|
|
|
if err != nil {
|
|
|
log.Infof("Could not find valid interface matching %s: %s", iface, err)
|
|
|
}
|
|
@@ -226,7 +257,7 @@ func main() {
|
|
|
// Check interfaces that match any specified regexes
|
|
|
if extIface == nil {
|
|
|
for _, ifaceRegex := range opts.ifaceRegex {
|
|
|
- extIface, err = LookupExtIface("", ifaceRegex)
|
|
|
+ extIface, err = LookupExtIface("", ifaceRegex, ipStack)
|
|
|
if err != nil {
|
|
|
log.Infof("Could not find valid interface matching %s: %s", ifaceRegex, err)
|
|
|
}
|
|
@@ -303,25 +334,44 @@ func main() {
|
|
|
|
|
|
// Set up ipMasq if needed
|
|
|
if opts.ipMasq {
|
|
|
- if err = recycleIPTables(config.Network, bn.Lease()); err != nil {
|
|
|
- log.Errorf("Failed to recycle IPTables rules, %v", err)
|
|
|
- cancel()
|
|
|
- wg.Wait()
|
|
|
- os.Exit(1)
|
|
|
+ if config.EnableIPv4 {
|
|
|
+ if err = recycleIPTables(config.Network, bn.Lease()); err != nil {
|
|
|
+ log.Errorf("Failed to recycle IPTables rules, %v", err)
|
|
|
+ cancel()
|
|
|
+ wg.Wait()
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ log.Infof("Setting up masking rules")
|
|
|
+ go network.SetupAndEnsureIPTables(network.MasqRules(config.Network, bn.Lease()), opts.iptablesResyncSeconds)
|
|
|
+
|
|
|
+ }
|
|
|
+ if config.EnableIPv6 {
|
|
|
+ if err = recycleIP6Tables(config.IPv6Network, bn.Lease()); err != nil {
|
|
|
+ log.Errorf("Failed to recycle IP6Tables rules, %v", err)
|
|
|
+ cancel()
|
|
|
+ wg.Wait()
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ log.Infof("Setting up masking ip6 rules")
|
|
|
+ go network.SetupAndEnsureIP6Tables(network.MasqIP6Rules(config.IPv6Network, bn.Lease()), opts.iptablesResyncSeconds)
|
|
|
}
|
|
|
- log.Infof("Setting up masking rules")
|
|
|
- go network.SetupAndEnsureIPTables(network.MasqRules(config.Network, bn.Lease()), opts.iptablesResyncSeconds)
|
|
|
}
|
|
|
|
|
|
// Always enables forwarding rules. This is needed for Docker versions >1.13 (https://docs.docker.com/engine/userguide/networking/default_network/container-communication/#container-communication-between-hosts)
|
|
|
// In Docker 1.12 and earlier, the default FORWARD chain policy was ACCEPT.
|
|
|
// In Docker 1.13 and later, Docker sets the default policy of the FORWARD chain to DROP.
|
|
|
if opts.iptablesForwardRules {
|
|
|
- log.Infof("Changing default FORWARD chain policy to ACCEPT")
|
|
|
- go network.SetupAndEnsureIPTables(network.ForwardRules(config.Network.String()), opts.iptablesResyncSeconds)
|
|
|
+ if config.EnableIPv4 {
|
|
|
+ log.Infof("Changing default FORWARD chain policy to ACCEPT")
|
|
|
+ go network.SetupAndEnsureIPTables(network.ForwardRules(config.Network.String()), opts.iptablesResyncSeconds)
|
|
|
+ }
|
|
|
+ if config.EnableIPv6 {
|
|
|
+ log.Infof("IPv6: Changing default FORWARD chain policy to ACCEPT")
|
|
|
+ go network.SetupAndEnsureIP6Tables(network.ForwardRules(config.IPv6Network.String()), opts.iptablesResyncSeconds)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if err := WriteSubnetFile(opts.subnetFile, config.Network, opts.ipMasq, bn); err != nil {
|
|
|
+ if err := WriteSubnetFile(opts.subnetFile, config, opts.ipMasq, bn); err != nil {
|
|
|
// Continue, even though it failed.
|
|
|
log.Warningf("Failed to write subnet file: %s", err)
|
|
|
} else {
|
|
@@ -370,6 +420,22 @@ func recycleIPTables(nw ip.IP4Net, lease *subnet.Lease) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func recycleIP6Tables(nw ip.IP6Net, lease *subnet.Lease) error {
|
|
|
+ prevNetwork := ReadIP6CIDRFromSubnetFile(opts.subnetFile, "FLANNEL_IPV6_NETWORK")
|
|
|
+ prevSubnet := ReadIP6CIDRFromSubnetFile(opts.subnetFile, "FLANNEL_IPV6_SUBNET")
|
|
|
+ // recycle iptables rules only when network configured or subnet leased is not equal to current one.
|
|
|
+ if prevNetwork.String() != nw.String() && prevSubnet.String() != lease.IPv6Subnet.String() {
|
|
|
+ log.Infof("Current ipv6 network or subnet (%v, %v) is not equal to previous one (%v, %v), trying to recycle old ip6tables rules", nw, lease.IPv6Subnet, prevNetwork, prevSubnet)
|
|
|
+ lease := &subnet.Lease{
|
|
|
+ IPv6Subnet: prevSubnet,
|
|
|
+ }
|
|
|
+ if err := network.DeleteIP6Tables(network.MasqIP6Rules(prevNetwork, lease)); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func shutdownHandler(ctx context.Context, sigs chan os.Signal, cancel context.CancelFunc) {
|
|
|
// Wait for the context do be Done or for the signal to come in to shutdown.
|
|
|
select {
|
|
@@ -451,17 +517,43 @@ func MonitorLease(ctx context.Context, sm subnet.Manager, bn backend.Network, wg
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func LookupExtIface(ifname string, ifregex string) (*backend.ExternalInterface, error) {
|
|
|
+func LookupExtIface(ifname string, ifregex string, ipStack int) (*backend.ExternalInterface, error) {
|
|
|
var iface *net.Interface
|
|
|
var ifaceAddr net.IP
|
|
|
+ var ifaceV6Addr net.IP
|
|
|
var err error
|
|
|
|
|
|
+ // Check ip family stack
|
|
|
+ if ipStack == noneStack {
|
|
|
+ return nil, fmt.Errorf("none matched ip stack")
|
|
|
+ }
|
|
|
+
|
|
|
if len(ifname) > 0 {
|
|
|
if ifaceAddr = net.ParseIP(ifname); ifaceAddr != nil {
|
|
|
log.Infof("Searching for interface using %s", ifaceAddr)
|
|
|
- iface, err = ip.GetInterfaceByIP(ifaceAddr)
|
|
|
- if err != nil {
|
|
|
- return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
|
|
|
+ switch ipStack {
|
|
|
+ case ipv4Stack:
|
|
|
+ iface, err = ip.GetInterfaceByIP(ifaceAddr)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
|
|
|
+ }
|
|
|
+ case ipv6Stack:
|
|
|
+ iface, err = ip.GetInterfaceByIP6(ifaceAddr)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("error looking up v6 interface %s: %s", ifname, err)
|
|
|
+ }
|
|
|
+ case dualStack:
|
|
|
+ iface, err = ip.GetInterfaceByIP(ifaceAddr)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
|
|
|
+ }
|
|
|
+ v6Iface, err := ip.GetInterfaceByIP6(ifaceAddr)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("error looking up v6 interface %s: %s", ifname, err)
|
|
|
+ }
|
|
|
+ if iface.Name != v6Iface.Name {
|
|
|
+ return nil, fmt.Errorf("v6 interface %s must be the same with v4 interface %s", v6Iface.Name, iface.Name)
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
iface, err = net.InterfaceByName(ifname)
|
|
@@ -478,26 +570,75 @@ func LookupExtIface(ifname string, ifregex string) (*backend.ExternalInterface,
|
|
|
|
|
|
// Check IP
|
|
|
for _, ifaceToMatch := range ifaces {
|
|
|
- ifaceIP, err := ip.GetInterfaceIP4Addr(&ifaceToMatch)
|
|
|
- if err != nil {
|
|
|
- // Skip if there is no IPv4 address
|
|
|
- continue
|
|
|
- }
|
|
|
+ switch ipStack {
|
|
|
+ case ipv4Stack:
|
|
|
+ ifaceIP, err := ip.GetInterfaceIP4Addr(&ifaceToMatch)
|
|
|
+ if err != nil {
|
|
|
+ // Skip if there is no IPv4 address
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
- matched, err := regexp.MatchString(ifregex, ifaceIP.String())
|
|
|
- if err != nil {
|
|
|
- return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
|
|
|
- }
|
|
|
+ matched, err := regexp.MatchString(ifregex, ifaceIP.String())
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
|
|
|
+ }
|
|
|
|
|
|
- if matched {
|
|
|
- ifaceAddr = ifaceIP
|
|
|
- iface = &ifaceToMatch
|
|
|
- break
|
|
|
+ if matched {
|
|
|
+ ifaceAddr = ifaceIP
|
|
|
+ iface = &ifaceToMatch
|
|
|
+ break
|
|
|
+ }
|
|
|
+ case ipv6Stack:
|
|
|
+ ifaceIP, err := ip.GetInterfaceIP6Addr(&ifaceToMatch)
|
|
|
+ if err != nil {
|
|
|
+ // Skip if there is no IPv6 address
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ matched, err := regexp.MatchString(ifregex, ifaceIP.String())
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
|
|
|
+ }
|
|
|
+
|
|
|
+ if matched {
|
|
|
+ ifaceV6Addr = ifaceIP
|
|
|
+ iface = &ifaceToMatch
|
|
|
+ break
|
|
|
+ }
|
|
|
+ case dualStack:
|
|
|
+ ifaceIP, err := ip.GetInterfaceIP4Addr(&ifaceToMatch)
|
|
|
+ if err != nil {
|
|
|
+ // Skip if there is no IPv4 address
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ matched, err := regexp.MatchString(ifregex, ifaceIP.String())
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
|
|
|
+ }
|
|
|
+
|
|
|
+ ifaceV6IP, err := ip.GetInterfaceIP6Addr(&ifaceToMatch)
|
|
|
+ if err != nil {
|
|
|
+ // Skip if there is no IPv6 address
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ v6Matched, err := regexp.MatchString(ifregex, ifaceV6IP.String())
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
|
|
|
+ }
|
|
|
+
|
|
|
+ if matched && v6Matched {
|
|
|
+ ifaceAddr = ifaceIP
|
|
|
+ ifaceV6Addr = ifaceV6IP
|
|
|
+ iface = &ifaceToMatch
|
|
|
+ break
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Check Name
|
|
|
- if iface == nil && ifaceAddr == nil {
|
|
|
+ if iface == nil && (ifaceAddr == nil || ifaceV6Addr == nil) {
|
|
|
for _, ifaceToMatch := range ifaces {
|
|
|
matched, err := regexp.MatchString(ifregex, ifaceToMatch.Name)
|
|
|
if err != nil {
|
|
@@ -515,33 +656,78 @@ func LookupExtIface(ifname string, ifregex string) (*backend.ExternalInterface,
|
|
|
if iface == nil {
|
|
|
var availableFaces []string
|
|
|
for _, f := range ifaces {
|
|
|
- ip, _ := ip.GetInterfaceIP4Addr(&f) // We can safely ignore errors. We just won't log any ip
|
|
|
- availableFaces = append(availableFaces, fmt.Sprintf("%s:%s", f.Name, ip))
|
|
|
+ var ipaddr net.IP
|
|
|
+ switch ipStack {
|
|
|
+ case ipv4Stack, dualStack:
|
|
|
+ ipaddr, _ = ip.GetInterfaceIP4Addr(&f) // We can safely ignore errors. We just won't log any ip
|
|
|
+ case ipv6Stack:
|
|
|
+ ipaddr, _ = ip.GetInterfaceIP6Addr(&f) // We can safely ignore errors. We just won't log any ip
|
|
|
+ }
|
|
|
+ availableFaces = append(availableFaces, fmt.Sprintf("%s:%s", f.Name, ipaddr))
|
|
|
}
|
|
|
|
|
|
return nil, fmt.Errorf("Could not match pattern %s to any of the available network interfaces (%s)", ifregex, strings.Join(availableFaces, ", "))
|
|
|
}
|
|
|
} else {
|
|
|
log.Info("Determining IP address of default interface")
|
|
|
- if iface, err = ip.GetDefaultGatewayInterface(); err != nil {
|
|
|
- return nil, fmt.Errorf("failed to get default interface: %s", err)
|
|
|
+ switch ipStack {
|
|
|
+ case ipv4Stack:
|
|
|
+ if iface, err = ip.GetDefaultGatewayInterface(); err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to get default interface: %s", err)
|
|
|
+ }
|
|
|
+ case ipv6Stack:
|
|
|
+ if iface, err = ip.GetDefaultV6GatewayInterface(); err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to get default v6 interface: %s", err)
|
|
|
+ }
|
|
|
+ case dualStack:
|
|
|
+ if iface, err = ip.GetDefaultGatewayInterface(); err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to get default interface: %s", err)
|
|
|
+ }
|
|
|
+ v6Iface, err := ip.GetDefaultV6GatewayInterface()
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to get default v6 interface: %s", err)
|
|
|
+ }
|
|
|
+ if iface.Name != v6Iface.Name {
|
|
|
+ return nil, fmt.Errorf("v6 default route interface %s "+
|
|
|
+ "must be the same with v4 default route interface %s", v6Iface.Name, iface.Name)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if ifaceAddr == nil {
|
|
|
+ if ipStack == ipv4Stack && ifaceAddr == nil {
|
|
|
+ ifaceAddr, err = ip.GetInterfaceIP4Addr(iface)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to find IPv4 address for interface %s", iface.Name)
|
|
|
+ }
|
|
|
+ } else if ipStack == ipv6Stack && ifaceV6Addr == nil {
|
|
|
+ ifaceV6Addr, err = ip.GetInterfaceIP6Addr(iface)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to find IPv6 address for interface %s", iface.Name)
|
|
|
+ }
|
|
|
+ } else if ipStack == dualStack && ifaceAddr == nil && ifaceV6Addr == nil {
|
|
|
ifaceAddr, err = ip.GetInterfaceIP4Addr(iface)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("failed to find IPv4 address for interface %s", iface.Name)
|
|
|
}
|
|
|
+ ifaceV6Addr, err = ip.GetInterfaceIP6Addr(iface)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to find IPv6 address for interface %s", iface.Name)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- log.Infof("Using interface with name %s and address %s", iface.Name, ifaceAddr)
|
|
|
+ if ifaceAddr != nil {
|
|
|
+ log.Infof("Using interface with name %s and address %s", iface.Name, ifaceAddr)
|
|
|
+ }
|
|
|
+ if ifaceV6Addr != nil {
|
|
|
+ log.Infof("Using interface with name %s and v6 address %s", iface.Name, ifaceV6Addr)
|
|
|
+ }
|
|
|
|
|
|
if iface.MTU == 0 {
|
|
|
return nil, fmt.Errorf("failed to determine MTU for %s interface", ifaceAddr)
|
|
|
}
|
|
|
|
|
|
var extAddr net.IP
|
|
|
+ var extV6Addr net.IP
|
|
|
|
|
|
if len(opts.publicIP) > 0 {
|
|
|
extAddr = net.ParseIP(opts.publicIP)
|
|
@@ -556,30 +742,53 @@ func LookupExtIface(ifname string, ifregex string) (*backend.ExternalInterface,
|
|
|
extAddr = ifaceAddr
|
|
|
}
|
|
|
|
|
|
+ if len(opts.publicIPv6) > 0 {
|
|
|
+ extV6Addr = net.ParseIP(opts.publicIPv6)
|
|
|
+ if extV6Addr == nil {
|
|
|
+ return nil, fmt.Errorf("invalid public IPv6 address: %s", opts.publicIPv6)
|
|
|
+ }
|
|
|
+ log.Infof("Using %s as external address", extV6Addr)
|
|
|
+ }
|
|
|
+
|
|
|
+ if extV6Addr == nil {
|
|
|
+ log.Infof("Defaulting external v6 address to interface address (%s)", ifaceV6Addr)
|
|
|
+ extV6Addr = ifaceV6Addr
|
|
|
+ }
|
|
|
+
|
|
|
return &backend.ExternalInterface{
|
|
|
- Iface: iface,
|
|
|
- IfaceAddr: ifaceAddr,
|
|
|
- ExtAddr: extAddr,
|
|
|
+ Iface: iface,
|
|
|
+ IfaceAddr: ifaceAddr,
|
|
|
+ IfaceV6Addr: ifaceV6Addr,
|
|
|
+ ExtAddr: extAddr,
|
|
|
+ ExtV6Addr: extV6Addr,
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func WriteSubnetFile(path string, nw ip.IP4Net, ipMasq bool, bn backend.Network) error {
|
|
|
+func WriteSubnetFile(path string, config *subnet.Config, ipMasq bool, bn backend.Network) error {
|
|
|
dir, name := filepath.Split(path)
|
|
|
os.MkdirAll(dir, 0755)
|
|
|
-
|
|
|
tempFile := filepath.Join(dir, "."+name)
|
|
|
f, err := os.Create(tempFile)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+ if config.EnableIPv4 {
|
|
|
+ nw := config.Network
|
|
|
+ // Write out the first usable IP by incrementing
|
|
|
+ // sn.IP by one
|
|
|
+ sn := bn.Lease().Subnet
|
|
|
+ sn.IP += 1
|
|
|
+ fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
|
|
|
+ fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn)
|
|
|
+ }
|
|
|
+ if config.EnableIPv6 {
|
|
|
+ ip6Nw := config.IPv6Network
|
|
|
+ ip6Sn := bn.Lease().IPv6Subnet
|
|
|
+ ip6Sn.IP = (*ip.IP6)(big.NewInt(0).Add((*big.Int)(ip6Sn.IP), big.NewInt(1)))
|
|
|
+ fmt.Fprintf(f, "FLANNEL_IPV6_NETWORK=%s\n", ip6Nw)
|
|
|
+ fmt.Fprintf(f, "FLANNEL_IPV6_SUBNET=%s\n", ip6Sn)
|
|
|
+ }
|
|
|
|
|
|
- // Write out the first usable IP by incrementing
|
|
|
- // sn.IP by one
|
|
|
- sn := bn.Lease().Subnet
|
|
|
- sn.IP += 1
|
|
|
-
|
|
|
- fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
|
|
|
- fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn)
|
|
|
fmt.Fprintf(f, "FLANNEL_MTU=%d\n", bn.MTU())
|
|
|
_, err = fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", ipMasq)
|
|
|
f.Close()
|
|
@@ -623,3 +832,19 @@ func ReadCIDRFromSubnetFile(path string, CIDRKey string) ip.IP4Net {
|
|
|
}
|
|
|
return prevCIDR
|
|
|
}
|
|
|
+
|
|
|
+func ReadIP6CIDRFromSubnetFile(path string, CIDRKey string) ip.IP6Net {
|
|
|
+ var prevCIDR ip.IP6Net
|
|
|
+ if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
|
+ prevSubnetVals, err := godotenv.Read(path)
|
|
|
+ if err != nil {
|
|
|
+ log.Errorf("Couldn't fetch previous %s from subnet file at %s: %s", CIDRKey, path, err)
|
|
|
+ } else if prevCIDRString, ok := prevSubnetVals[CIDRKey]; ok {
|
|
|
+ err = prevCIDR.UnmarshalJSON([]byte(prevCIDRString))
|
|
|
+ if err != nil {
|
|
|
+ log.Errorf("Couldn't parse previous %s from subnet file at %s: %s", CIDRKey, path, err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return prevCIDR
|
|
|
+}
|