Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions pkg/kubelet/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ type PodConfig struct {
// contains the list of all configured sources
sourcesLock sync.Mutex
sources sets.Set[string]

podReady
}

// podReady holds the initPodReady flag and its lock
type podReady struct {
// initPodReady is flag to check Pod ready status
initPodReady bool
// podReadyLock is used to guard initPodReady flag
podReadyLock sync.RWMutex
}

// NewPodConfig creates an object that can merge many configuration sources into a stream
Expand Down Expand Up @@ -106,16 +96,13 @@ func (c *PodConfig) Channel(ctx context.Context, source string) chan<- interface
// SeenAllSources returns true if seenSources contains all sources in the
// config, and also this config has received a SET message from each source.
func (c *PodConfig) SeenAllSources(seenSources sets.Set[string]) bool {
c.podReadyLock.RLock()
defer c.podReadyLock.RUnlock()
return c.initPodReady
}

// SetInitPodReady is used to safely set initPodReady flag
func (c *PodConfig) SetInitPodReady(readyStatus bool) {
c.podReadyLock.Lock()
defer c.podReadyLock.Unlock()
c.initPodReady = readyStatus
if c.pods == nil {
return false
}
c.sourcesLock.Lock()
defer c.sourcesLock.Unlock()
klog.V(5).InfoS("Looking for sources, have seen", "sources", sets.List(c.sources), "seenSources", seenSources)
return seenSources.HasAll(sets.List(c.sources)...) && c.pods.seenSources(sets.List(c.sources)...)
Comment on lines +104 to +105

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve readability and avoid a minor performance hit, it's better to call sets.List(c.sources) only once and store its result in a local variable. The current implementation calls it twice if the V(5) log level is enabled, which involves creating and sorting a slice each time. While the performance impact is likely negligible given the small number of sources, factoring it out makes the code cleaner.

	sourcesList := sets.List(c.sources)
	klog.V(5).InfoS("Looking for sources, have seen", "sources", sourcesList, "seenSources", seenSources)
	return seenSources.HasAll(sourcesList...) && c.pods.seenSources(sourcesList...)

}

// Updates returns a channel of updates to the configuration, properly denormalized.
Expand Down Expand Up @@ -333,6 +320,12 @@ func (s *podStorage) markSourceSet(source string) {
s.sourcesSeen.Insert(source)
}

func (s *podStorage) seenSources(sources ...string) bool {
s.sourcesSeenLock.RLock()
defer s.sourcesSeenLock.RUnlock()
return s.sourcesSeen.HasAll(sources...)
}

func filterInvalidPods(pods []*v1.Pod, source string, recorder record.EventRecorder) (filtered []*v1.Pod) {
names := sets.Set[string]{}
for i, pod := range pods {
Expand Down