Merge pull request #107748 from cyclinder/fix_concurrent_map

kube-proxy ipvs: fix to prevent concurrent map read and map write
This commit is contained in:
Kubernetes Prow Robot 2022-01-26 05:10:01 -08:00 committed by GitHub
commit e6cbcaea15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -81,6 +81,8 @@ func (q *graceTerminateRSList) remove(rs *listItem) bool {
}
func (q *graceTerminateRSList) flushList(handler func(rsToDelete *listItem) (bool, error)) bool {
q.lock.Lock()
defer q.lock.Unlock()
success := true
for name, rs := range q.list {
deleted, err := handler(rs)
@ -90,7 +92,7 @@ func (q *graceTerminateRSList) flushList(handler func(rsToDelete *listItem) (boo
}
if deleted {
klog.InfoS("Removed real server from graceful delete real server list", "realServer", name)
q.remove(rs)
delete(q.list, rs.String())
}
}
return success

View File

@ -17,12 +17,13 @@ limitations under the License.
package ipvs
import (
"fmt"
netutils "k8s.io/utils/net"
"reflect"
"testing"
utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
utilipvstest "k8s.io/kubernetes/pkg/util/ipvs/testing"
netutils "k8s.io/utils/net"
)
func Test_GracefulDeleteRS(t *testing.T) {
@ -400,3 +401,36 @@ func Test_GracefulDeleteRS(t *testing.T) {
})
}
}
func Test_RaceTerminateRSList(t *testing.T) {
ipvs := &utilipvstest.FakeIPVS{}
gracefulTerminationManager := NewGracefulTerminationManager(ipvs)
go func() {
for i := 1; i <= 10; i++ {
for j := 1; i <= 100; j++ {
gracefulTerminationManager.rsList.add(makeListItem(i, j))
}
}
}()
if !gracefulTerminationManager.rsList.flushList(gracefulTerminationManager.deleteRsFunc) {
t.Error("failed to flush entries")
}
}
func makeListItem(i, j int) *listItem {
vs := fmt.Sprintf("%d.%d.%d.%d", 1, 1, i, i)
rs := fmt.Sprintf("%d.%d.%d.%d", 1, 1, i, j)
return &listItem{
VirtualServer: &utilipvs.VirtualServer{
Address: netutils.ParseIPSloppy(vs),
Protocol: "tcp",
Port: uint16(80),
},
RealServer: &utilipvs.RealServer{
Address: netutils.ParseIPSloppy(rs),
Port: uint16(80),
},
}
}