kube-proxy/ipvs get local addr just once per sync

This commit is contained in:
Gao Zheng 2019-11-12 19:40:21 +08:00
parent 73b2c82b28
commit 6b36a60c75

View File

@ -899,6 +899,33 @@ func (proxier *Proxier) OnEndpointSlicesSynced() {
// EntryInvalidErr indicates if an ipset entry is invalid or not // EntryInvalidErr indicates if an ipset entry is invalid or not
const EntryInvalidErr = "error adding entry %s to ipset %s" 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. // This is where all of the ipvs calls happen.
// assumes proxier.mu is held // assumes proxier.mu is held
func (proxier *Proxier) syncProxyRules() { func (proxier *Proxier) syncProxyRules() {
@ -919,6 +946,11 @@ 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 := 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, // 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.
@ -955,7 +987,7 @@ func (proxier *Proxier) syncProxyRules() {
proxier.createAndLinkeKubeChain() proxier.createAndLinkeKubeChain()
// 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
@ -1100,11 +1132,10 @@ func (proxier *Proxier) syncProxyRules() {
// Capture externalIPs. // Capture externalIPs.
for _, externalIP := range svcInfo.ExternalIPStrings() { for _, externalIP := range svcInfo.ExternalIPStrings() {
if local, err := utilproxy.IsLocalIP(externalIP); err != nil { if len(localAddrs) == 0 {
klog.Errorf("can't determine if IP is local, assuming not: %v", err) klog.Errorf("couldn't find any local IPs, assuming %s is not local", externalIP)
// We do not start listening on SCTP ports, according to our agreement in the } else if (svcInfo.Protocol() != v1.ProtocolSCTP) && ipExists(net.ParseIP(externalIP), localAddrs) {
// SCTP support KEP // We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP
} else if local && (svcInfo.Protocol() != v1.ProtocolSCTP) {
lp := utilproxy.LocalPort{ lp := utilproxy.LocalPort{
Description: "externalIP for " + svcNameString, Description: "externalIP for " + svcNameString,
IP: externalIP, IP: externalIP,