Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s/watchers: Fix race condition in init functions #23170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/k8s/watchers/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package watchers
import (
"context"
"errors"
"sync/atomic"

k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -24,12 +25,13 @@ import (
func (k *K8sWatcher) namespacesInit() {
apiGroup := k8sAPIGroupNamespaceV1Core

synced := false
var synced atomic.Bool
synced.Store(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is probably redundant, zero value should be false

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I opted for explicitness so that when someone fetches all references of the variable, they see an explicit "false" (next to an explicit "true"). It makes it slightly easier to tell how this variable is used.


k.blockWaitGroupToSyncResources(
k.stop,
nil,
func() bool { return synced },
func() bool { return synced.Load() },
apiGroup,
)
k.k8sAPIGroups.AddAPI(apiGroup)
Expand All @@ -54,7 +56,7 @@ func (k *K8sWatcher) namespacesInit() {
var err error
switch event.Kind {
case resource.Sync:
synced = true
synced.Store(true)
case resource.Upsert:
err = nsUpdater.update(event.Object)
k.K8sEventProcessed(metricNS, resources.MetricUpdate, err == nil)
Expand Down
10 changes: 6 additions & 4 deletions pkg/k8s/watchers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package watchers

import (
"context"
"sync/atomic"

v1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -43,19 +44,20 @@ func nodeEventsAreEqual(oldNode, newNode *v1.Node) bool {

func (k *K8sWatcher) NodesInit(k8sClient client.Clientset) {
k.nodesInitOnce.Do(func() {
synced := false
var synced atomic.Bool
synced.Store(false)
swg := lock.NewStoppableWaitGroup()
k.blockWaitGroupToSyncResources(
k.stop,
swg,
func() bool { return synced },
func() bool { return synced.Load() },
k8sAPIGroupNodeV1Core,
)
go k.nodeEventLoop(&synced, swg)
})
}

func (k *K8sWatcher) nodeEventLoop(synced *bool, swg *lock.StoppableWaitGroup) {
func (k *K8sWatcher) nodeEventLoop(synced *atomic.Bool, swg *lock.StoppableWaitGroup) {
apiGroup := k8sAPIGroupNodeV1Core
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -72,7 +74,7 @@ func (k *K8sWatcher) nodeEventLoop(synced *bool, swg *lock.StoppableWaitGroup) {
}
switch event.Kind {
case resource.Sync:
*synced = true
synced.Store(true)
case resource.Upsert:
newNode := event.Object
if oldNode == nil {
Expand Down
10 changes: 6 additions & 4 deletions pkg/k8s/watchers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package watchers

import (
"context"
"sync/atomic"

"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/k8s/resource"
Expand All @@ -15,21 +16,22 @@ import (
)

func (k *K8sWatcher) servicesInit() {
synced := false
var synced atomic.Bool
synced.Store(false)
swgSvcs := lock.NewStoppableWaitGroup()

k.blockWaitGroupToSyncResources(
k.stop,
swgSvcs,
func() bool { return synced },
func() bool { return synced.Load() },
resources.K8sAPIGroupServiceV1Core,
)
go k.serviceEventLoop(&synced, swgSvcs)

k.k8sAPIGroups.AddAPI(resources.K8sAPIGroupServiceV1Core)
}

func (k *K8sWatcher) serviceEventLoop(synced *bool, swg *lock.StoppableWaitGroup) {
func (k *K8sWatcher) serviceEventLoop(synced *atomic.Bool, swg *lock.StoppableWaitGroup) {
apiGroup := resources.K8sAPIGroupServiceV1Core
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -46,7 +48,7 @@ func (k *K8sWatcher) serviceEventLoop(synced *bool, swg *lock.StoppableWaitGroup
var err error
switch event.Kind {
case resource.Sync:
*synced = true
synced.Store(true)
case resource.Upsert:
svc := event.Object
k.K8sEventReceived(apiGroup, resources.MetricService, resources.MetricUpdate, true, false)
Expand Down