Fetch storedPeerList after read from Kubernetes

This avoids a race condition where the peer has added itself to the
list and the annotation since we read storedPeerList.
This commit is contained in:
Bryan Boreham committed 2019-04-02 09:11:27 +00:00
1 parent 15b9b7416a
commit eb7ba691ee
2 files changed
+7 -3

No files matched your search

+6 -2
View File
@@ -115,7 +115,7 @@ func reclaimRemovedPeers(kube kubernetes.Interface, cml *configMapAnnotations, m
common.Log.Warnln("[kube-peers] not removing myself", peer) common.Log.Warnln("[kube-peers] not removing myself", peer)
continue continue
} }
changed, err := reclaimPeer(weave, cml, storedPeerList, peer.PeerName, myPeerName) changed, err := reclaimPeer(weave, cml, peer.PeerName, myPeerName)
if err != nil { if err != nil {
return err return err
} }
@@ -135,7 +135,7 @@ func reclaimRemovedPeers(kube kubernetes.Interface, cml *configMapAnnotations, m
// actions the reclaim. // actions the reclaim.
// Return a bool to show whether we attempted to change anything, // Return a bool to show whether we attempted to change anything,
// and an error if something went wrong. // and an error if something went wrong.
func reclaimPeer(weave weaveClient, cml *configMapAnnotations, storedPeerList *peerList, peerName string, myPeerName string) (changed bool, err error) { func reclaimPeer(weave weaveClient, cml *configMapAnnotations, peerName string, myPeerName string) (changed bool, err error) {
common.Log.Debugln("[kube-peers] Preparing to remove disappeared peer", peerName) common.Log.Debugln("[kube-peers] Preparing to remove disappeared peer", peerName)
okToRemove := false okToRemove := false
nonExistentPeer := false nonExistentPeer := false
@@ -153,6 +153,10 @@ func reclaimPeer(weave weaveClient, cml *configMapAnnotations, storedPeerList *p
if existingAnnotation == myPeerName { if existingAnnotation == myPeerName {
okToRemove = true okToRemove = true
} else { } else {
storedPeerList, err := cml.GetPeerList()
if err != nil {
return false, err
}
// handle an edge case where peer claimed to own the action to reclaim but no longer // handle an edge case where peer claimed to own the action to reclaim but no longer
// exists hence lock persists foever // exists hence lock persists foever
if !storedPeerList.contains(existingAnnotation) { if !storedPeerList.contains(existingAnnotation) {
+1 -1
View File
@@ -137,7 +137,7 @@ func TestPeerListFuzz(t *testing.T) {
found := storedPeerList.contains(peerName(i)) found := storedPeerList.contains(peerName(i))
require.True(t, found, "peer %d not found in stored list", i) require.True(t, found, "peer %d not found in stored list", i)
_, err = reclaimPeer(mockWeave{}, cml, storedPeerList, peerName(i), fmt.Sprintf("deleter-%d", i)) _, err = reclaimPeer(mockWeave{}, cml, peerName(i), fmt.Sprintf("deleter-%d", i))
require.NoError(t, err) require.NoError(t, err)
storedPeerList, err = cml.GetPeerList() storedPeerList, err = cml.GetPeerList()