From 6b36a60c7564a1f101ceed9d8af7f2f1126313a4 Mon Sep 17 00:00:00 2001 From: Gao Zheng Date: Tue, 12 Nov 2019 19:40:21 +0800 Subject: [PATCH] kube-proxy/ipvs get local addr just once per sync --- pkg/proxy/ipvs/proxier.go | 43 +++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index 3dbfd107e6c..28eeea1d1a2 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -899,6 +899,33 @@ func (proxier *Proxier) OnEndpointSlicesSynced() { // EntryInvalidErr indicates if an ipset entry is invalid or not const EntryInvalidErr = "error adding entry %s to ipset %s" +func getLocalAddrs() ([]net.IP, error) { + var localAddrs []net.IP + + addrs, err := net.InterfaceAddrs() + if err != nil { + return nil, err + } + + for _, addr := range addrs { + ip, _, err := net.ParseCIDR(addr.String()) + if err != nil { + return nil, err + } + localAddrs = append(localAddrs, ip) + } + return localAddrs, nil +} + +func ipExists(ip net.IP, addrs []net.IP) bool { + for _, addr := range addrs { + if ip.Equal(addr) { + return true + } + } + return false +} + // This is where all of the ipvs calls happen. // assumes proxier.mu is held func (proxier *Proxier) syncProxyRules() { @@ -919,6 +946,11 @@ func (proxier *Proxier) syncProxyRules() { klog.V(4).Infof("syncProxyRules took %v", time.Since(start)) }() + localAddrs, err := getLocalAddrs() + if err != nil { + klog.Errorf("Failed to get local addresses during proxy sync: %v", err) + } + // 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 // responsible for detecting no-op changes and not calling this function. @@ -955,7 +987,7 @@ func (proxier *Proxier) syncProxyRules() { proxier.createAndLinkeKubeChain() // 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 { klog.Errorf("Failed to create dummy interface: %s, error: %v", DefaultDummyDevice, err) return @@ -1100,11 +1132,10 @@ func (proxier *Proxier) syncProxyRules() { // Capture externalIPs. for _, externalIP := range svcInfo.ExternalIPStrings() { - if local, err := utilproxy.IsLocalIP(externalIP); err != nil { - klog.Errorf("can't determine if IP is local, assuming not: %v", err) - // We do not start listening on SCTP ports, according to our agreement in the - // SCTP support KEP - } else if local && (svcInfo.Protocol() != v1.ProtocolSCTP) { + if len(localAddrs) == 0 { + klog.Errorf("couldn't find any local IPs, assuming %s is not local", externalIP) + } else if (svcInfo.Protocol() != v1.ProtocolSCTP) && ipExists(net.ParseIP(externalIP), localAddrs) { + // We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP lp := utilproxy.LocalPort{ Description: "externalIP for " + svcNameString, IP: externalIP,