From 249996e62f01f0cf9b3fd872ce8f1f19c0fc0aec Mon Sep 17 00:00:00 2001 From: jornshen Date: Thu, 14 Jan 2021 16:15:16 +0800 Subject: [PATCH] use exist const to replace ipvs/proxier.go tcp,udp,sctp --- pkg/proxy/ipvs/proxier.go | 42 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index 2705b1657b9..70738bffd74 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -181,23 +181,25 @@ var ipsetWithIptablesChain = []struct { {kubeLoadBalancerSourceCIDRSet, string(KubeFireWallChain), "RETURN", "dst,dst,src", ""}, {kubeLoadBalancerSourceIPSet, string(KubeFireWallChain), "RETURN", "dst,dst,src", ""}, {kubeLoadBalancerLocalSet, string(KubeLoadBalancerChain), "RETURN", "dst,dst", ""}, - {kubeNodePortLocalSetTCP, string(KubeNodePortChain), "RETURN", "dst", "tcp"}, - {kubeNodePortSetTCP, string(KubeNodePortChain), string(KubeMarkMasqChain), "dst", "tcp"}, - {kubeNodePortLocalSetUDP, string(KubeNodePortChain), "RETURN", "dst", "udp"}, - {kubeNodePortSetUDP, string(KubeNodePortChain), string(KubeMarkMasqChain), "dst", "udp"}, - {kubeNodePortSetSCTP, string(KubeNodePortChain), string(KubeMarkMasqChain), "dst,dst", "sctp"}, - {kubeNodePortLocalSetSCTP, string(KubeNodePortChain), "RETURN", "dst,dst", "sctp"}, + {kubeNodePortLocalSetTCP, string(KubeNodePortChain), "RETURN", "dst", utilipset.ProtocolTCP}, + {kubeNodePortSetTCP, string(KubeNodePortChain), string(KubeMarkMasqChain), "dst", utilipset.ProtocolTCP}, + {kubeNodePortLocalSetUDP, string(KubeNodePortChain), "RETURN", "dst", utilipset.ProtocolUDP}, + {kubeNodePortSetUDP, string(KubeNodePortChain), string(KubeMarkMasqChain), "dst", utilipset.ProtocolUDP}, + {kubeNodePortSetSCTP, string(KubeNodePortChain), string(KubeMarkMasqChain), "dst,dst", utilipset.ProtocolSCTP}, + {kubeNodePortLocalSetSCTP, string(KubeNodePortChain), "RETURN", "dst,dst", utilipset.ProtocolSCTP}, } // In IPVS proxy mode, the following flags need to be set -const sysctlBridgeCallIPTables = "net/bridge/bridge-nf-call-iptables" -const sysctlVSConnTrack = "net/ipv4/vs/conntrack" -const sysctlConnReuse = "net/ipv4/vs/conn_reuse_mode" -const sysctlExpireNoDestConn = "net/ipv4/vs/expire_nodest_conn" -const sysctlExpireQuiescentTemplate = "net/ipv4/vs/expire_quiescent_template" -const sysctlForward = "net/ipv4/ip_forward" -const sysctlArpIgnore = "net/ipv4/conf/all/arp_ignore" -const sysctlArpAnnounce = "net/ipv4/conf/all/arp_announce" +const ( + sysctlBridgeCallIPTables = "net/bridge/bridge-nf-call-iptables" + sysctlVSConnTrack = "net/ipv4/vs/conntrack" + sysctlConnReuse = "net/ipv4/vs/conn_reuse_mode" + sysctlExpireNoDestConn = "net/ipv4/vs/expire_nodest_conn" + sysctlExpireQuiescentTemplate = "net/ipv4/vs/expire_quiescent_template" + sysctlForward = "net/ipv4/ip_forward" + sysctlArpIgnore = "net/ipv4/conf/all/arp_ignore" + sysctlArpAnnounce = "net/ipv4/conf/all/arp_announce" +) // Proxier is an ipvs based proxy for connections between a localhost:lport // and services that provide the actual backends. @@ -1480,7 +1482,7 @@ func (proxier *Proxier) syncProxyRules() { ) switch protocol { - case "tcp": + case utilipset.ProtocolTCP: nodePortSet = proxier.ipsetList[kubeNodePortSetTCP] entries = []*utilipset.Entry{{ // No need to provide ip info @@ -1488,7 +1490,7 @@ func (proxier *Proxier) syncProxyRules() { Protocol: protocol, SetType: utilipset.BitmapPort, }} - case "udp": + case utilipset.ProtocolUDP: nodePortSet = proxier.ipsetList[kubeNodePortSetUDP] entries = []*utilipset.Entry{{ // No need to provide ip info @@ -1496,7 +1498,7 @@ func (proxier *Proxier) syncProxyRules() { Protocol: protocol, SetType: utilipset.BitmapPort, }} - case "sctp": + case utilipset.ProtocolSCTP: nodePortSet = proxier.ipsetList[kubeNodePortSetSCTP] // Since hash ip:port is used for SCTP, all the nodeIPs to be used in the SCTP ipset entries. entries = []*utilipset.Entry{} @@ -1531,11 +1533,11 @@ func (proxier *Proxier) syncProxyRules() { if svcInfo.OnlyNodeLocalEndpoints() { var nodePortLocalSet *IPSet switch protocol { - case "tcp": + case utilipset.ProtocolTCP: nodePortLocalSet = proxier.ipsetList[kubeNodePortLocalSetTCP] - case "udp": + case utilipset.ProtocolUDP: nodePortLocalSet = proxier.ipsetList[kubeNodePortLocalSetUDP] - case "sctp": + case utilipset.ProtocolSCTP: nodePortLocalSet = proxier.ipsetList[kubeNodePortLocalSetSCTP] default: // It should never hit