mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Merge pull request #98083 from JornShen/optimize_proxier_duplicate_localaddrset
optimize proxier duplicate localaddrset
This commit is contained in:
commit
e89e7b4ed1
@ -828,21 +828,6 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
klog.V(2).InfoS("syncProxyRules complete", "elapsed", time.Since(start))
|
klog.V(2).InfoS("syncProxyRules complete", "elapsed", time.Since(start))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
localAddrs, err := utilproxy.GetLocalAddrs()
|
|
||||||
if err != nil {
|
|
||||||
klog.ErrorS(err, "Failed to get local addresses during proxy sync, assuming external IPs are not local")
|
|
||||||
} else if len(localAddrs) == 0 {
|
|
||||||
klog.InfoS("No local addresses found, assuming all external IPs are not local")
|
|
||||||
}
|
|
||||||
|
|
||||||
localAddrSet := utilnet.IPSet{}
|
|
||||||
localAddrSet.Insert(localAddrs...)
|
|
||||||
|
|
||||||
nodeAddresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
|
|
||||||
if err != nil {
|
|
||||||
klog.ErrorS(err, "Failed to get node ip address matching nodeport cidrs, services with nodeport may not work as intended", "CIDRs", proxier.nodePortAddresses)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We assume that if this was called, we really want to sync them,
|
// We assume that if this was called, we really want to sync them,
|
||||||
// even if nothing changed in the meantime. In other words, callers are
|
// even if nothing changed in the meantime. In other words, callers are
|
||||||
// responsible for detecting no-op changes and not calling this function.
|
// responsible for detecting no-op changes and not calling this function.
|
||||||
@ -903,7 +888,7 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
// This will be a map of chain name to chain with rules as stored in iptables-save/iptables-restore
|
// This will be a map of chain name to chain with rules as stored in iptables-save/iptables-restore
|
||||||
existingFilterChains := make(map[utiliptables.Chain][]byte)
|
existingFilterChains := make(map[utiliptables.Chain][]byte)
|
||||||
proxier.existingFilterChainsData.Reset()
|
proxier.existingFilterChainsData.Reset()
|
||||||
err = proxier.iptables.SaveInto(utiliptables.TableFilter, proxier.existingFilterChainsData)
|
err := proxier.iptables.SaveInto(utiliptables.TableFilter, proxier.existingFilterChainsData)
|
||||||
if err != nil { // if we failed to get any rules
|
if err != nil { // if we failed to get any rules
|
||||||
klog.ErrorS(err, "Failed to execute iptables-save, syncing all rules")
|
klog.ErrorS(err, "Failed to execute iptables-save, syncing all rules")
|
||||||
} else { // otherwise parse the output
|
} else { // otherwise parse the output
|
||||||
@ -1007,6 +992,12 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
proxier.endpointChainsNumber += len(proxier.endpointsMap[svcName])
|
proxier.endpointChainsNumber += len(proxier.endpointsMap[svcName])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
localAddrSet := utilproxy.GetLocalAddrSet()
|
||||||
|
nodeAddresses, err := utilproxy.GetNodeAddresses(proxier.nodePortAddresses, proxier.networkInterfacer)
|
||||||
|
if err != nil {
|
||||||
|
klog.ErrorS(err, "Failed to get node ip address matching nodeport cidrs, services with nodeport may not work as intended", "CIDRs", proxier.nodePortAddresses)
|
||||||
|
}
|
||||||
|
|
||||||
// Build rules for each service.
|
// Build rules for each service.
|
||||||
for svcName, svc := range proxier.serviceMap {
|
for svcName, svc := range proxier.serviceMap {
|
||||||
svcInfo, ok := svc.(*serviceInfo)
|
svcInfo, ok := svc.(*serviceInfo)
|
||||||
|
@ -1039,16 +1039,6 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
klog.V(4).Infof("syncProxyRules took %v", time.Since(start))
|
klog.V(4).Infof("syncProxyRules took %v", time.Since(start))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
localAddrs, err := utilproxy.GetLocalAddrs()
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("Failed to get local addresses during proxy sync: %v, assuming external IPs are not local", err)
|
|
||||||
} else if len(localAddrs) == 0 {
|
|
||||||
klog.Warning("No local addresses found, assuming all external IPs are not local")
|
|
||||||
}
|
|
||||||
|
|
||||||
localAddrSet := utilnet.IPSet{}
|
|
||||||
localAddrSet.Insert(localAddrs...)
|
|
||||||
|
|
||||||
// We assume that if this was called, we really want to sync them,
|
// We assume that if this was called, we really want to sync them,
|
||||||
// even if nothing changed in the meantime. In other words, callers are
|
// even if nothing changed in the meantime. In other words, callers are
|
||||||
// responsible for detecting no-op changes and not calling this function.
|
// responsible for detecting no-op changes and not calling this function.
|
||||||
@ -1085,7 +1075,7 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
proxier.createAndLinkKubeChain()
|
proxier.createAndLinkKubeChain()
|
||||||
|
|
||||||
// make sure dummy interface exists in the system where ipvs Proxier will bind service address on it
|
// make sure dummy interface exists in the system where ipvs Proxier will bind service address on it
|
||||||
_, err = proxier.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
|
_, err := proxier.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Failed to create dummy interface: %s, error: %v", DefaultDummyDevice, err)
|
klog.Errorf("Failed to create dummy interface: %s, error: %v", DefaultDummyDevice, err)
|
||||||
return
|
return
|
||||||
@ -1161,6 +1151,8 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
// reset slice to filtered entries
|
// reset slice to filtered entries
|
||||||
nodeIPs = nodeIPs[:idx]
|
nodeIPs = nodeIPs[:idx]
|
||||||
|
|
||||||
|
localAddrSet := utilproxy.GetLocalAddrSet()
|
||||||
|
|
||||||
// Build IPVS rules for each service.
|
// Build IPVS rules for each service.
|
||||||
for svcName, svc := range proxier.serviceMap {
|
for svcName, svc := range proxier.serviceMap {
|
||||||
svcInfo, ok := svc.(*serviceInfo)
|
svcInfo, ok := svc.(*serviceInfo)
|
||||||
|
@ -395,16 +395,7 @@ func (proxier *Proxier) syncProxyRules() {
|
|||||||
proxier.unmergeService(change.previous, existingPorts)
|
proxier.unmergeService(change.previous, existingPorts)
|
||||||
}
|
}
|
||||||
|
|
||||||
localAddrs, err := utilproxy.GetLocalAddrs()
|
proxier.localAddrs = utilproxy.GetLocalAddrSet()
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("Failed to get local addresses during proxy sync: %s, assuming IPs are not local", err)
|
|
||||||
} else if len(localAddrs) == 0 {
|
|
||||||
klog.Warning("No local addresses were found, assuming all external IPs are not local")
|
|
||||||
}
|
|
||||||
|
|
||||||
localAddrSet := netutils.IPSet{}
|
|
||||||
localAddrSet.Insert(localAddrs...)
|
|
||||||
proxier.localAddrs = localAddrSet
|
|
||||||
|
|
||||||
proxier.ensurePortals()
|
proxier.ensurePortals()
|
||||||
proxier.cleanupStaleStickySessions()
|
proxier.cleanupStaleStickySessions()
|
||||||
@ -769,7 +760,7 @@ func (proxier *Proxier) openPortal(service proxy.ServicePortName, info *ServiceI
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (proxier *Proxier) openOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) error {
|
func (proxier *Proxier) openOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) error {
|
||||||
if proxier.localAddrs.Len() > 0 && proxier.localAddrs.Has(portal.ip) {
|
if proxier.localAddrs.Has(portal.ip) {
|
||||||
err := proxier.claimNodePort(portal.ip, portal.port, protocol, name)
|
err := proxier.claimNodePort(portal.ip, portal.port, protocol, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -945,7 +936,7 @@ func (proxier *Proxier) closePortal(service proxy.ServicePortName, info *Service
|
|||||||
|
|
||||||
func (proxier *Proxier) closeOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) []error {
|
func (proxier *Proxier) closeOnePortal(portal portal, protocol v1.Protocol, proxyIP net.IP, proxyPort int, name proxy.ServicePortName) []error {
|
||||||
el := []error{}
|
el := []error{}
|
||||||
if proxier.localAddrs.Len() > 0 && proxier.localAddrs.Has(portal.ip) {
|
if proxier.localAddrs.Has(portal.ip) {
|
||||||
if err := proxier.releaseNodePort(portal.ip, portal.port, protocol, name); err != nil {
|
if err := proxier.releaseNodePort(portal.ip, portal.port, protocol, name); err != nil {
|
||||||
el = append(el, err)
|
el = append(el, err)
|
||||||
}
|
}
|
||||||
|
@ -157,6 +157,21 @@ func GetLocalAddrs() ([]net.IP, error) {
|
|||||||
return localAddrs, nil
|
return localAddrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLocalAddrSet return a local IPSet.
|
||||||
|
// If failed to get local addr, will assume no local ips.
|
||||||
|
func GetLocalAddrSet() utilnet.IPSet {
|
||||||
|
localAddrs, err := GetLocalAddrs()
|
||||||
|
if err != nil {
|
||||||
|
klog.ErrorS(err, "Failed to get local addresses assuming no local IPs", err)
|
||||||
|
} else if len(localAddrs) == 0 {
|
||||||
|
klog.InfoS("No local addresses were found")
|
||||||
|
}
|
||||||
|
|
||||||
|
localAddrSet := utilnet.IPSet{}
|
||||||
|
localAddrSet.Insert(localAddrs...)
|
||||||
|
return localAddrSet
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldSkipService checks if a given service should skip proxying
|
// ShouldSkipService checks if a given service should skip proxying
|
||||||
func ShouldSkipService(service *v1.Service) bool {
|
func ShouldSkipService(service *v1.Service) bool {
|
||||||
// if ClusterIP is "None" or empty, skip proxying
|
// if ClusterIP is "None" or empty, skip proxying
|
||||||
|
Loading…
Reference in New Issue
Block a user