diff --git a/pkg/agent/flannel/flannel.go b/pkg/agent/flannel/flannel.go index 26b2950c01..87973d9f52 100644 --- a/pkg/agent/flannel/flannel.go +++ b/pkg/agent/flannel/flannel.go @@ -68,8 +68,10 @@ func flannel(ctx context.Context, flannelIface *net.Interface, flannelConf, kube return err } - go network.SetupAndEnsureIPTables(network.MasqRules(config.Network, bn.Lease()), 60) - go network.SetupAndEnsureIPTables(network.ForwardRules(config.Network.String()), 50) + if netMode == (ipv4+ipv6) || netMode == ipv4 { + 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 { 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 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. logrus.Warningf("Failed to write flannel subnet file: %s", err) } else { @@ -97,8 +99,14 @@ func LookupExtInterface(iface *net.Interface, netMode int) (*backend.ExternalInt if iface == nil { logrus.Debug("No interface defined for flannel in the config. Fetching the default gateway interface") - if iface, err = ip.GetDefaultGatewayInterface(); err != nil { - return nil, fmt.Errorf("failed to get default interface: %s", err) + if netMode == ipv4 || netMode == (ipv4+ipv6) { + 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) @@ -147,7 +155,7 @@ func LookupExtInterface(iface *net.Interface, netMode int) (*backend.ExternalInt }, 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) os.MkdirAll(dir, 0755) @@ -161,9 +169,10 @@ func WriteSubnetFile(path string, nw ip.IP4Net, nwv6 ip.IP6Net, ipMasq bool, bn // sn.IP by one sn := bn.Lease().Subnet sn.IP++ - - fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw) - fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn) + if netMode == ipv4 || netMode == (ipv4+ipv6) { + fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw) + fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn) + } if nwv6.String() != emptyIPv6Network { snv6 := bn.Lease().IPv6Subnet diff --git a/pkg/agent/flannel/setup.go b/pkg/agent/flannel/setup.go index 18065c185e..180578f7e7 100644 --- a/pkg/agent/flannel/setup.go +++ b/pkg/agent/flannel/setup.go @@ -45,7 +45,8 @@ const ( flannelConf = `{ "Network": "%CIDR%", - "EnableIPv6": %DUALSTACK%, + "EnableIPv6": %IPV6_ENABLED%, + "EnableIPv4": %IPV4_ENABLED%, "IPv6Network": "%CIDR_IPV6%", "Backend": %backend% } @@ -140,6 +141,7 @@ func createCNIConf(dir string) 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) if nodeConfig.FlannelConfFile == "" { return errors.New("Flannel configuration not defined") @@ -148,7 +150,40 @@ func createFlannelConf(nodeConfig *config.Node) error { logrus.Infof("Using custom flannel conf defined at %s", nodeConfig.FlannelConfFile) 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 @@ -169,25 +204,6 @@ func createFlannelConf(nodeConfig *config.Node) error { } 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) return util.WriteFile(nodeConfig.FlannelConfFile, confJSON) } diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index 829a608d6b..45e411dda9 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -534,9 +534,6 @@ func validateNetworkConfiguration(serverConfig server.Config) error { 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") } - 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