Skip to content

Commit

Permalink
Merge pull request kubernetes#106551 from sunzhaochang/fix-lock-podco…
Browse files Browse the repository at this point in the history
…nfig

Fix missing of RLock in SeenAllSources
  • Loading branch information
k8s-ci-robot authored Jun 29, 2022
2 parents 7f920da + e833c64 commit 2555fa7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/kubelet/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (c *PodConfig) SeenAllSources(seenSources sets.String) bool {
if c.pods == nil {
return false
}
c.sourcesLock.Lock()
defer c.sourcesLock.Unlock()
klog.V(5).InfoS("Looking for sources, have seen", "sources", c.sources.List(), "seenSources", seenSources)
return seenSources.HasAll(c.sources.List()...) && c.pods.seenSources(c.sources.List()...)
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/kubelet/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"reflect"
"sort"
"strconv"
"sync"
"testing"
"time"

"k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
Expand Down Expand Up @@ -456,3 +458,29 @@ func TestPodUpdateLabels(t *testing.T) {
expectPodUpdate(t, ch, CreatePodUpdate(kubetypes.UPDATE, TestSource, pod))

}

func TestPodConfigRace(t *testing.T) {
eventBroadcaster := record.NewBroadcaster()
config := NewPodConfig(PodConfigNotificationIncremental, eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "kubelet"}))
seenSources := sets.NewString(TestSource)
var wg sync.WaitGroup
const iterations = 100
wg.Add(2)

go func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
defer wg.Done()
for i := 0; i < iterations; i++ {
config.Channel(ctx, strconv.Itoa(i))
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
config.SeenAllSources(seenSources)
}
}()

wg.Wait()
}

0 comments on commit 2555fa7

Please sign in to comment.