Skip to content

Commit f6d4378

Browse files
agent-platform-auto-pr[bot]kacper-murzynxlucas
authored
[Backport 7.66.x] Fix data race in autodiscovery kube services provider (#36683)
Co-authored-by: kacper-murzyn <89013263+kacper-murzyn@users.noreply.github.com> Co-authored-by: xlucas <xavier.lucas@datadoghq.com>
1 parent ea3032f commit f6d4378

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

comp/core/autodiscovery/providers/kube_services.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"strings"
1414

15+
"go.uber.org/atomic"
1516
v1 "k8s.io/api/core/v1"
1617
"k8s.io/apimachinery/pkg/labels"
1718
listersv1 "k8s.io/client-go/listers/core/v1"
@@ -35,7 +36,7 @@ const (
3536
// KubeServiceConfigProvider implements the ConfigProvider interface for the apiserver.
3637
type KubeServiceConfigProvider struct {
3738
lister listersv1.ServiceLister
38-
upToDate bool
39+
upToDate *atomic.Bool
3940
configErrors map[string]ErrorMsgSet
4041
telemetryStore *telemetry.Store
4142
}
@@ -58,6 +59,7 @@ func NewKubeServiceConfigProvider(_ *pkgconfigsetup.ConfigurationProviders, tele
5859
lister: servicesInformer.Lister(),
5960
configErrors: make(map[string]ErrorMsgSet),
6061
telemetryStore: telemetryStore,
62+
upToDate: atomic.NewBool(false),
6163
}
6264

6365
if _, err := servicesInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
@@ -91,7 +93,7 @@ func (k *KubeServiceConfigProvider) Collect(ctx context.Context) ([]integration.
9193
if err != nil {
9294
return nil, err
9395
}
94-
k.upToDate = true
96+
k.upToDate.Store(true)
9597

9698
return k.parseServiceAnnotations(services, pkgconfigsetup.Datadog())
9799
}
@@ -100,13 +102,13 @@ func (k *KubeServiceConfigProvider) Collect(ctx context.Context) ([]integration.
100102
//
101103
//nolint:revive // TODO(CINT) Fix revive linter
102104
func (k *KubeServiceConfigProvider) IsUpToDate(ctx context.Context) (bool, error) {
103-
return k.upToDate, nil
105+
return k.upToDate.Load(), nil
104106
}
105107

106108
func (k *KubeServiceConfigProvider) invalidate(obj interface{}) {
107109
if obj != nil {
108110
log.Trace("Invalidating configs on new/deleted service")
109-
k.upToDate = false
111+
k.upToDate.Store(false)
110112
}
111113
}
112114

@@ -122,7 +124,7 @@ func (k *KubeServiceConfigProvider) invalidateIfChanged(old, obj interface{}) {
122124
castedOld, ok := old.(*v1.Service)
123125
if !ok {
124126
log.Errorf("Expected a *v1.Service type, got: %T", old)
125-
k.upToDate = false
127+
k.upToDate.Store(false)
126128
return
127129
}
128130
// Quick exit if resversion did not change
@@ -132,7 +134,7 @@ func (k *KubeServiceConfigProvider) invalidateIfChanged(old, obj interface{}) {
132134
// Compare annotations
133135
if valuesDiffer(castedObj.Annotations, castedOld.Annotations, kubeServiceAnnotationPrefix) {
134136
log.Trace("Invalidating configs on service change")
135-
k.upToDate = false
137+
k.upToDate.Store(false)
136138
return
137139
}
138140
}

comp/core/autodiscovery/providers/kube_services_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
"go.uber.org/atomic"
1718
v1 "k8s.io/api/core/v1"
1819
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1920
"k8s.io/apimachinery/pkg/runtime"
@@ -247,6 +248,7 @@ func TestParseKubeServiceAnnotations(t *testing.T) {
247248

248249
provider := KubeServiceConfigProvider{
249250
telemetryStore: telemetryStore,
251+
upToDate: atomic.NewBool(false),
250252
}
251253
cfgs, _ := provider.parseServiceAnnotations([]*v1.Service{tc.service}, cfg)
252254
assert.EqualValues(t, tc.expectedOut, cfgs)
@@ -337,7 +339,7 @@ func TestInvalidateIfChanged(t *testing.T) {
337339
} {
338340
t.Run("", func(t *testing.T) {
339341
ctx := context.Background()
340-
provider := &KubeServiceConfigProvider{upToDate: true}
342+
provider := &KubeServiceConfigProvider{upToDate: atomic.NewBool(true)}
341343
provider.invalidateIfChanged(tc.old, tc.obj)
342344

343345
upToDate, err := provider.IsUpToDate(ctx)
@@ -475,6 +477,7 @@ func TestGetConfigErrors_KubeServices(t *testing.T) {
475477
lister: lister,
476478
configErrors: test.currentErrors,
477479
telemetryStore: telemetryStore,
480+
upToDate: atomic.NewBool(false),
478481
}
479482

480483
configs, err := provider.Collect(context.TODO())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Each section from every release note are combined when the
2+
# CHANGELOG-DCA.rst is rendered. So the text needs to be worded so that
3+
# it does not depend on any information only available in another
4+
# section. This may mean repeating some details, but each section
5+
# must be readable independently of the other.
6+
#
7+
# Each section note must be formatted as reStructuredText.
8+
---
9+
fixes:
10+
- |
11+
Fix data race in autodiscovery kube services provider.

0 commit comments

Comments
 (0)