Allow kube-proxy iptables mode to support dual-stack, with the meta-proxier.

This commit is contained in:
Vallery Lancey
2019-09-07 22:18:07 -07:00
parent 4a8205b6fd
commit 23957a6b28
8 changed files with 123 additions and 20 deletions

View File

@@ -148,22 +148,55 @@ func newProxyServer(
return nil, fmt.Errorf("unable to read IPTables MasqueradeBit from config") return nil, fmt.Errorf("unable to read IPTables MasqueradeBit from config")
} }
// TODO this has side effects that should only happen when Run() is invoked. if utilfeature.DefaultFeatureGate.Enabled(features.IPv6DualStack) {
proxier, err = iptables.NewProxier( klog.V(0).Info("creating dualStackProxier for iptables.")
iptInterface,
utilsysctl.New(), // Create iptables handlers for both families, one is already created
execer, // Always ordered as IPv4, IPv6
config.IPTables.SyncPeriod.Duration, var ipt [2]utiliptables.Interface
config.IPTables.MinSyncPeriod.Duration, if iptInterface.IsIpv6() {
config.IPTables.MasqueradeAll, ipt[1] = iptInterface
int(*config.IPTables.MasqueradeBit), ipt[0] = utiliptables.New(execer, utiliptables.ProtocolIpv4)
config.ClusterCIDR, } else {
hostname, ipt[0] = iptInterface
nodeIP, ipt[1] = utiliptables.New(execer, utiliptables.ProtocolIpv6)
recorder, }
healthzServer,
config.NodePortAddresses, // TODO this has side effects that should only happen when Run() is invoked.
) proxier, err = iptables.NewDualStackProxier(
ipt,
utilsysctl.New(),
execer,
config.IPTables.SyncPeriod.Duration,
config.IPTables.MinSyncPeriod.Duration,
config.IPTables.MasqueradeAll,
int(*config.IPTables.MasqueradeBit),
cidrTuple(config.ClusterCIDR),
hostname,
nodeIPTuple(config.BindAddress),
recorder,
healthzServer,
config.NodePortAddresses,
)
} else { // Create a single-stack proxier.
// TODO this has side effects that should only happen when Run() is invoked.
proxier, err = iptables.NewProxier(
iptInterface,
utilsysctl.New(),
execer,
config.IPTables.SyncPeriod.Duration,
config.IPTables.MinSyncPeriod.Duration,
config.IPTables.MasqueradeAll,
int(*config.IPTables.MasqueradeBit),
config.ClusterCIDR,
hostname,
nodeIP,
recorder,
healthzServer,
config.NodePortAddresses,
)
}
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to create proxier: %v", err) return nil, fmt.Errorf("unable to create proxier: %v", err)
} }
@@ -174,6 +207,7 @@ func newProxyServer(
klog.V(0).Info("creating dualStackProxier for ipvs.") klog.V(0).Info("creating dualStackProxier for ipvs.")
// Create iptables handlers for both families, one is already created // Create iptables handlers for both families, one is already created
// Always ordered as IPv4, IPv6
var ipt [2]utiliptables.Interface var ipt [2]utiliptables.Interface
if iptInterface.IsIpv6() { if iptInterface.IsIpv6() {
ipt[1] = iptInterface ipt[1] = iptInterface

View File

@@ -48,6 +48,7 @@ filegroup(
"//pkg/proxy/healthcheck:all-srcs", "//pkg/proxy/healthcheck:all-srcs",
"//pkg/proxy/iptables:all-srcs", "//pkg/proxy/iptables:all-srcs",
"//pkg/proxy/ipvs:all-srcs", "//pkg/proxy/ipvs:all-srcs",
"//pkg/proxy/metaproxier:all-srcs",
"//pkg/proxy/metrics:all-srcs", "//pkg/proxy/metrics:all-srcs",
"//pkg/proxy/userspace:all-srcs", "//pkg/proxy/userspace:all-srcs",
"//pkg/proxy/util:all-srcs", "//pkg/proxy/util:all-srcs",

View File

@@ -14,6 +14,7 @@ go_library(
"//pkg/features:go_default_library", "//pkg/features:go_default_library",
"//pkg/proxy:go_default_library", "//pkg/proxy:go_default_library",
"//pkg/proxy/healthcheck:go_default_library", "//pkg/proxy/healthcheck:go_default_library",
"//pkg/proxy/metaproxier:go_default_library",
"//pkg/proxy/metrics:go_default_library", "//pkg/proxy/metrics:go_default_library",
"//pkg/proxy/util:go_default_library", "//pkg/proxy/util:go_default_library",
"//pkg/util/async:go_default_library", "//pkg/util/async:go_default_library",

View File

@@ -43,6 +43,7 @@ import (
"k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/proxy" "k8s.io/kubernetes/pkg/proxy"
"k8s.io/kubernetes/pkg/proxy/healthcheck" "k8s.io/kubernetes/pkg/proxy/healthcheck"
"k8s.io/kubernetes/pkg/proxy/metaproxier"
"k8s.io/kubernetes/pkg/proxy/metrics" "k8s.io/kubernetes/pkg/proxy/metrics"
utilproxy "k8s.io/kubernetes/pkg/proxy/util" utilproxy "k8s.io/kubernetes/pkg/proxy/util"
"k8s.io/kubernetes/pkg/util/async" "k8s.io/kubernetes/pkg/util/async"
@@ -333,6 +334,42 @@ func NewProxier(ipt utiliptables.Interface,
return proxier, nil return proxier, nil
} }
// NewDualStackProxier creates a MetaProxier instance, with IPv4 and IPv6 proxies.
func NewDualStackProxier(
ipt [2]utiliptables.Interface,
sysctl utilsysctl.Interface,
exec utilexec.Interface,
syncPeriod time.Duration,
minSyncPeriod time.Duration,
masqueradeAll bool,
masqueradeBit int,
clusterCIDR [2]string,
hostname string,
nodeIP [2]net.IP,
recorder record.EventRecorder,
healthzServer healthcheck.ProxierHealthUpdater,
nodePortAddresses []string,
) (proxy.Provider, error) {
// Create an ipv4 instance of the single-stack proxier
ipv4Proxier, err := NewProxier(ipt[0], sysctl,
exec, syncPeriod, minSyncPeriod,
masqueradeAll, masqueradeBit, clusterCIDR[0], hostname, nodeIP[0],
recorder, healthzServer, nodePortAddresses)
if err != nil {
return nil, fmt.Errorf("unable to create ipv4 proxier: %v", err)
}
ipv6Proxier, err := NewProxier(ipt[1], sysctl,
exec, syncPeriod, minSyncPeriod,
masqueradeAll, masqueradeBit, clusterCIDR[1], hostname, nodeIP[1],
recorder, healthzServer, nodePortAddresses)
if err != nil {
return nil, fmt.Errorf("unable to create ipv6 proxier: %v", err)
}
return metaproxier.NewMetaProxier(ipv4Proxier, ipv6Proxier), nil // TODO move meta-proxier to mode-neutral package
}
type iptablesJumpChain struct { type iptablesJumpChain struct {
table utiliptables.Table table utiliptables.Table
dstChain utiliptables.Chain dstChain utiliptables.Chain

View File

@@ -45,7 +45,6 @@ go_library(
srcs = [ srcs = [
"graceful_termination.go", "graceful_termination.go",
"ipset.go", "ipset.go",
"meta_proxier.go",
"netlink.go", "netlink.go",
"netlink_linux.go", "netlink_linux.go",
"netlink_unsupported.go", "netlink_unsupported.go",
@@ -56,8 +55,8 @@ go_library(
deps = [ deps = [
"//pkg/features:go_default_library", "//pkg/features:go_default_library",
"//pkg/proxy:go_default_library", "//pkg/proxy:go_default_library",
"//pkg/proxy/config:go_default_library",
"//pkg/proxy/healthcheck:go_default_library", "//pkg/proxy/healthcheck:go_default_library",
"//pkg/proxy/metaproxier:go_default_library",
"//pkg/proxy/metrics:go_default_library", "//pkg/proxy/metrics:go_default_library",
"//pkg/proxy/util:go_default_library", "//pkg/proxy/util:go_default_library",
"//pkg/util/async:go_default_library", "//pkg/util/async:go_default_library",

View File

@@ -46,6 +46,7 @@ import (
"k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/proxy" "k8s.io/kubernetes/pkg/proxy"
"k8s.io/kubernetes/pkg/proxy/healthcheck" "k8s.io/kubernetes/pkg/proxy/healthcheck"
"k8s.io/kubernetes/pkg/proxy/metaproxier"
"k8s.io/kubernetes/pkg/proxy/metrics" "k8s.io/kubernetes/pkg/proxy/metrics"
utilproxy "k8s.io/kubernetes/pkg/proxy/util" utilproxy "k8s.io/kubernetes/pkg/proxy/util"
"k8s.io/kubernetes/pkg/util/async" "k8s.io/kubernetes/pkg/util/async"
@@ -515,7 +516,7 @@ func NewDualStackProxier(
// Return a meta-proxier that dispatch calls between the two // Return a meta-proxier that dispatch calls between the two
// single-stack proxier instances // single-stack proxier instances
return NewMetaProxier(ipv4Proxier, ipv6Proxier), nil return metaproxier.NewMetaProxier(ipv4Proxier, ipv6Proxier), nil
} }
func filterCIDRs(wantIPv6 bool, cidrs []string) []string { func filterCIDRs(wantIPv6 bool, cidrs []string) []string {

View File

@@ -0,0 +1,30 @@
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["meta_proxier.go"],
importpath = "k8s.io/kubernetes/pkg/proxy/metaproxier",
deps = [
"//pkg/proxy:go_default_library",
"//pkg/proxy/config:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/discovery/v1beta1:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/k8s.io/utils/net:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package ipvs package metaproxier
import ( import (
"fmt" "fmt"