Skip to content

Commit

Permalink
add locking to pod cache (istio#1379)
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

add locking to pod cache

**What this PR does / why we need it**:
PodCache has no mutex protecting its internal state. Add RWMutex to fix this.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes istio#1377 

**Special notes for your reviewer**:

**Release note**:

```release-note
Fix race in Pilot PodCache
```
  • Loading branch information
rkpagadala authored and istio-merge-robot committed Oct 2, 2017
1 parent ee78725 commit a6538a0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pilot/platform/kube/cache.go
Expand Up @@ -15,13 +15,16 @@
package kube

import (
"sync"

"k8s.io/api/core/v1"

"istio.io/pilot/model"
)

// PodCache is an eventually consistent pod cache
type PodCache struct {
rwMu sync.RWMutex
cacheHandler

// keys maintains stable pod IP to name key mapping
Expand All @@ -36,6 +39,9 @@ func newPodCache(ch cacheHandler) *PodCache {
}

ch.handler.Append(func(obj interface{}, ev model.Event) error {
out.rwMu.Lock()
defer out.rwMu.Unlock()

pod := *obj.(*v1.Pod)
ip := pod.Status.PodIP
if len(ip) > 0 {
Expand All @@ -53,6 +59,9 @@ func newPodCache(ch cacheHandler) *PodCache {

// getPodByIp returns the pod or nil if pod not found or an error occurred
func (pc *PodCache) getPodByIP(addr string) (*v1.Pod, bool) {
pc.rwMu.RLock()
defer pc.rwMu.RUnlock()

key, exists := pc.keys[addr]
if !exists {
return nil, false
Expand Down

0 comments on commit a6538a0

Please sign in to comment.