Unverified Commit 29c55f5e authored by Roberto Bonafiglia's avatar Roberto Bonafiglia Committed by GitHub

Merge pull request #5238 from rbrtbnfgl/ipv6-only-with-flannel

Added ipv6 only support with flannel
parents 93346904 ff85faa7
...@@ -68,8 +68,10 @@ func flannel(ctx context.Context, flannelIface *net.Interface, flannelConf, kube ...@@ -68,8 +68,10 @@ func flannel(ctx context.Context, flannelIface *net.Interface, flannelConf, kube
return err return err
} }
go network.SetupAndEnsureIPTables(network.MasqRules(config.Network, bn.Lease()), 60) if netMode == (ipv4+ipv6) || netMode == ipv4 {
go network.SetupAndEnsureIPTables(network.ForwardRules(config.Network.String()), 50) go network.SetupAndEnsureIPTables(network.MasqRules(config.Network, bn.Lease()), 60)
go network.SetupAndEnsureIPTables(network.ForwardRules(config.Network.String()), 50)
}
if flannelIPv6Masq && config.IPv6Network.String() != emptyIPv6Network { if flannelIPv6Masq && config.IPv6Network.String() != emptyIPv6Network {
logrus.Debugf("Creating IPv6 masquerading iptables rules for %s network", config.IPv6Network.String()) logrus.Debugf("Creating IPv6 masquerading iptables rules for %s network", config.IPv6Network.String())
...@@ -77,7 +79,7 @@ func flannel(ctx context.Context, flannelIface *net.Interface, flannelConf, kube ...@@ -77,7 +79,7 @@ func flannel(ctx context.Context, flannelIface *net.Interface, flannelConf, kube
go network.SetupAndEnsureIP6Tables(network.ForwardRules(config.IPv6Network.String()), 50) go network.SetupAndEnsureIP6Tables(network.ForwardRules(config.IPv6Network.String()), 50)
} }
if err := WriteSubnetFile(subnetFile, config.Network, config.IPv6Network, true, bn); err != nil { if err := WriteSubnetFile(subnetFile, config.Network, config.IPv6Network, true, bn, netMode); err != nil {
// Continue, even though it failed. // Continue, even though it failed.
logrus.Warningf("Failed to write flannel subnet file: %s", err) logrus.Warningf("Failed to write flannel subnet file: %s", err)
} else { } else {
...@@ -97,8 +99,14 @@ func LookupExtInterface(iface *net.Interface, netMode int) (*backend.ExternalInt ...@@ -97,8 +99,14 @@ func LookupExtInterface(iface *net.Interface, netMode int) (*backend.ExternalInt
if iface == nil { if iface == nil {
logrus.Debug("No interface defined for flannel in the config. Fetching the default gateway interface") logrus.Debug("No interface defined for flannel in the config. Fetching the default gateway interface")
if iface, err = ip.GetDefaultGatewayInterface(); err != nil { if netMode == ipv4 || netMode == (ipv4+ipv6) {
return nil, fmt.Errorf("failed to get default interface: %s", err) if iface, err = ip.GetDefaultGatewayInterface(); err != nil {
return nil, fmt.Errorf("failed to get default interface: %s", err)
}
} else {
if iface, err = ip.GetDefaultV6GatewayInterface(); err != nil {
return nil, fmt.Errorf("failed to get default interface: %s", err)
}
} }
} }
logrus.Debugf("The interface %s will be used by flannel", iface.Name) logrus.Debugf("The interface %s will be used by flannel", iface.Name)
...@@ -147,7 +155,7 @@ func LookupExtInterface(iface *net.Interface, netMode int) (*backend.ExternalInt ...@@ -147,7 +155,7 @@ func LookupExtInterface(iface *net.Interface, netMode int) (*backend.ExternalInt
}, nil }, nil
} }
func WriteSubnetFile(path string, nw ip.IP4Net, nwv6 ip.IP6Net, ipMasq bool, bn backend.Network) error { func WriteSubnetFile(path string, nw ip.IP4Net, nwv6 ip.IP6Net, ipMasq bool, bn backend.Network, netMode int) error {
dir, name := filepath.Split(path) dir, name := filepath.Split(path)
os.MkdirAll(dir, 0755) os.MkdirAll(dir, 0755)
...@@ -161,9 +169,10 @@ func WriteSubnetFile(path string, nw ip.IP4Net, nwv6 ip.IP6Net, ipMasq bool, bn ...@@ -161,9 +169,10 @@ func WriteSubnetFile(path string, nw ip.IP4Net, nwv6 ip.IP6Net, ipMasq bool, bn
// sn.IP by one // sn.IP by one
sn := bn.Lease().Subnet sn := bn.Lease().Subnet
sn.IP++ sn.IP++
if netMode == ipv4 || netMode == (ipv4+ipv6) {
fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw) fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn) fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn)
}
if nwv6.String() != emptyIPv6Network { if nwv6.String() != emptyIPv6Network {
snv6 := bn.Lease().IPv6Subnet snv6 := bn.Lease().IPv6Subnet
......
...@@ -45,7 +45,8 @@ const ( ...@@ -45,7 +45,8 @@ const (
flannelConf = `{ flannelConf = `{
"Network": "%CIDR%", "Network": "%CIDR%",
"EnableIPv6": %DUALSTACK%, "EnableIPv6": %IPV6_ENABLED%,
"EnableIPv4": %IPV4_ENABLED%,
"IPv6Network": "%CIDR_IPV6%", "IPv6Network": "%CIDR_IPV6%",
"Backend": %backend% "Backend": %backend%
} }
...@@ -140,6 +141,7 @@ func createCNIConf(dir string) error { ...@@ -140,6 +141,7 @@ func createCNIConf(dir string) error {
} }
func createFlannelConf(nodeConfig *config.Node) error { func createFlannelConf(nodeConfig *config.Node) error {
var ipv4Enabled string
logrus.Debugf("Creating the flannel configuration for backend %s in file %s", nodeConfig.FlannelBackend, nodeConfig.FlannelConfFile) logrus.Debugf("Creating the flannel configuration for backend %s in file %s", nodeConfig.FlannelBackend, nodeConfig.FlannelConfFile)
if nodeConfig.FlannelConfFile == "" { if nodeConfig.FlannelConfFile == "" {
return errors.New("Flannel configuration not defined") return errors.New("Flannel configuration not defined")
...@@ -148,7 +150,40 @@ func createFlannelConf(nodeConfig *config.Node) error { ...@@ -148,7 +150,40 @@ func createFlannelConf(nodeConfig *config.Node) error {
logrus.Infof("Using custom flannel conf defined at %s", nodeConfig.FlannelConfFile) logrus.Infof("Using custom flannel conf defined at %s", nodeConfig.FlannelConfFile)
return nil return nil
} }
confJSON := strings.ReplaceAll(flannelConf, "%CIDR%", nodeConfig.AgentConfig.ClusterCIDR.String()) netMode, err := findNetMode(nodeConfig.AgentConfig.ClusterCIDRs)
if err != nil {
logrus.Fatalf("Flannel error checking netMode: %v", err)
return err
}
if netMode == ipv4 || netMode == (ipv4+ipv6) {
ipv4Enabled = "true"
} else {
ipv4Enabled = "false"
}
confJSON := strings.ReplaceAll(flannelConf, "%IPV4_ENABLED%", ipv4Enabled)
if netMode == ipv4 {
confJSON = strings.ReplaceAll(confJSON, "%CIDR%", nodeConfig.AgentConfig.ClusterCIDR.String())
confJSON = strings.ReplaceAll(confJSON, "%IPV6_ENABLED%", "false")
confJSON = strings.ReplaceAll(confJSON, "%CIDR_IPV6%", emptyIPv6Network)
} else if netMode == (ipv4 + ipv6) {
confJSON = strings.ReplaceAll(confJSON, "%CIDR%", nodeConfig.AgentConfig.ClusterCIDR.String())
confJSON = strings.ReplaceAll(confJSON, "%IPV6_ENABLED%", "true")
for _, cidr := range nodeConfig.AgentConfig.ClusterCIDRs {
if utilsnet.IsIPv6(cidr.IP) {
// Only one ipv6 range available. This might change in future: https://github.com/kubernetes/enhancements/issues/2593
confJSON = strings.ReplaceAll(confJSON, "%CIDR_IPV6%", cidr.String())
}
}
} else {
confJSON = strings.ReplaceAll(confJSON, "%CIDR%", "0.0.0.0/0")
confJSON = strings.ReplaceAll(confJSON, "%IPV6_ENABLED%", "true")
for _, cidr := range nodeConfig.AgentConfig.ClusterCIDRs {
if utilsnet.IsIPv6(cidr.IP) {
// Only one ipv6 range available. This might change in future: https://github.com/kubernetes/enhancements/issues/2593
confJSON = strings.ReplaceAll(confJSON, "%CIDR_IPV6%", cidr.String())
}
}
}
var backendConf string var backendConf string
...@@ -169,25 +204,6 @@ func createFlannelConf(nodeConfig *config.Node) error { ...@@ -169,25 +204,6 @@ func createFlannelConf(nodeConfig *config.Node) error {
} }
confJSON = strings.ReplaceAll(confJSON, "%backend%", backendConf) confJSON = strings.ReplaceAll(confJSON, "%backend%", backendConf)
netMode, err := findNetMode(nodeConfig.AgentConfig.ClusterCIDRs)
if err != nil {
logrus.Fatalf("Flannel error checking netMode: %v", err)
return err
}
if netMode == (ipv4 + ipv6) {
confJSON = strings.ReplaceAll(confJSON, "%DUALSTACK%", "true")
for _, cidr := range nodeConfig.AgentConfig.ClusterCIDRs {
if utilsnet.IsIPv6(cidr.IP) {
// Only one ipv6 range available. This might change in future: https://github.com/kubernetes/enhancements/issues/2593
confJSON = strings.ReplaceAll(confJSON, "%CIDR_IPV6%", cidr.String())
}
}
} else {
confJSON = strings.ReplaceAll(confJSON, "%DUALSTACK%", "false")
confJSON = strings.ReplaceAll(confJSON, "%CIDR_IPV6%", emptyIPv6Network)
}
logrus.Debugf("The flannel configuration is %s", confJSON) logrus.Debugf("The flannel configuration is %s", confJSON)
return util.WriteFile(nodeConfig.FlannelConfFile, confJSON) return util.WriteFile(nodeConfig.FlannelConfFile, confJSON)
} }
......
...@@ -534,9 +534,6 @@ func validateNetworkConfiguration(serverConfig server.Config) error { ...@@ -534,9 +534,6 @@ func validateNetworkConfiguration(serverConfig server.Config) error {
if serverConfig.ControlConfig.DisableNPC == false { if serverConfig.ControlConfig.DisableNPC == false {
return errors.New("network policy enforcement is not compatible with IPv6 only operation; server must be restarted with --disable-network-policy") return errors.New("network policy enforcement is not compatible with IPv6 only operation; server must be restarted with --disable-network-policy")
} }
if serverConfig.ControlConfig.FlannelBackend != config.FlannelBackendNone {
return errors.New("Flannel is not compatible with IPv6 only operation; server must be restarted with --flannel-backend=none")
}
} }
return nil return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment