Skip to content

Commit a6a1720

Browse files
committed
feat (processor/k8sattributes): wait for synced when starting
1 parent 5f4d198 commit a6a1720

File tree

7 files changed

+60
-16
lines changed

7 files changed

+60
-16
lines changed

.chloggen/k8sattributes-block.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: processor/k8sattributes
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Block when starting util the metadata have been synced, to fix that some data couldn't be associated with metadata when the agent was just started.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: []
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

processor/k8sattributesprocessor/client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ func (f *fakeClient) GetNode(nodeName string) (*kube.Node, bool) {
7070
}
7171

7272
// Start is a noop for FakeClient.
73-
func (f *fakeClient) Start() {
73+
func (f *fakeClient) Start() error {
7474
if f.Informer != nil {
75-
f.Informer.Run(f.StopCh)
75+
go f.Informer.Run(f.StopCh)
7676
}
77+
return nil
7778
}
7879

7980
// Stop is a noop for FakeClient.

processor/k8sattributesprocessor/internal/kube/client.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package kube // import "github.com/open-telemetry/opentelemetry-collector-contri
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"regexp"
1011
"strings"
@@ -189,50 +190,61 @@ func New(set component.TelemetrySettings, apiCfg k8sconfig.APIConfig, rules Extr
189190
}
190191

191192
// Start registers pod event handlers and starts watching the kubernetes cluster for pod changes.
192-
func (c *WatchClient) Start() {
193-
_, err := c.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
193+
func (c *WatchClient) Start() error {
194+
synced := make([]cache.InformerSynced, 0)
195+
reg, err := c.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
194196
AddFunc: c.handlePodAdd,
195197
UpdateFunc: c.handlePodUpdate,
196198
DeleteFunc: c.handlePodDelete,
197199
})
198200
if err != nil {
199-
c.logger.Error("error adding event handler to pod informer", zap.Error(err))
201+
return err
200202
}
203+
synced = append(synced, reg.HasSynced)
201204
go c.informer.Run(c.stopCh)
202205

203-
_, err = c.namespaceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
206+
reg, err = c.namespaceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
204207
AddFunc: c.handleNamespaceAdd,
205208
UpdateFunc: c.handleNamespaceUpdate,
206209
DeleteFunc: c.handleNamespaceDelete,
207210
})
208211
if err != nil {
209-
c.logger.Error("error adding event handler to namespace informer", zap.Error(err))
212+
return err
210213
}
214+
synced = append(synced, reg.HasSynced)
211215
go c.namespaceInformer.Run(c.stopCh)
212216

213217
if c.Rules.DeploymentName || c.Rules.DeploymentUID {
214-
_, err = c.replicasetInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
218+
reg, err = c.replicasetInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
215219
AddFunc: c.handleReplicaSetAdd,
216220
UpdateFunc: c.handleReplicaSetUpdate,
217221
DeleteFunc: c.handleReplicaSetDelete,
218222
})
219223
if err != nil {
220-
c.logger.Error("error adding event handler to replicaset informer", zap.Error(err))
224+
return err
221225
}
226+
synced = append(synced, reg.HasSynced)
222227
go c.replicasetInformer.Run(c.stopCh)
223228
}
224229

225230
if c.nodeInformer != nil {
226-
_, err = c.nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
231+
reg, err = c.nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
227232
AddFunc: c.handleNodeAdd,
228233
UpdateFunc: c.handleNodeUpdate,
229234
DeleteFunc: c.handleNodeDelete,
230235
})
231236
if err != nil {
232-
c.logger.Error("error adding event handler to node informer", zap.Error(err))
237+
return err
233238
}
239+
synced = append(synced, reg.HasSynced)
234240
go c.nodeInformer.Run(c.stopCh)
235241
}
242+
243+
if !cache.WaitForCacheSync(c.stopCh, synced...) {
244+
return errors.New("failed to wait for caches to sync")
245+
}
246+
247+
return nil
236248
}
237249

238250
// Stop signals the the k8s watcher/informer to stop watching for new events.

processor/k8sattributesprocessor/internal/kube/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func TestClientStartStop(t *testing.T) {
181181
done := make(chan struct{})
182182
assert.False(t, fctr.HasStopped())
183183
go func() {
184-
c.Start()
184+
assert.NoError(t, c.Start())
185185
close(done)
186186
}()
187187
c.Stop()

processor/k8sattributesprocessor/internal/kube/fake_informer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (f *FakeInformer) AddEventHandler(handler cache.ResourceEventHandler) (cach
4040
}
4141

4242
func (f *FakeInformer) AddEventHandlerWithResyncPeriod(_ cache.ResourceEventHandler, _ time.Duration) (cache.ResourceEventHandlerRegistration, error) {
43-
return nil, nil
43+
return f, nil
4444
}
4545

4646
func (f *FakeInformer) RemoveEventHandler(_ cache.ResourceEventHandlerRegistration) error {
@@ -164,7 +164,7 @@ func (f *NoOpInformer) AddEventHandler(handler cache.ResourceEventHandler) (cach
164164
return f.AddEventHandlerWithResyncPeriod(handler, time.Second)
165165
}
166166
func (f *NoOpInformer) AddEventHandlerWithResyncPeriod(_ cache.ResourceEventHandler, _ time.Duration) (cache.ResourceEventHandlerRegistration, error) {
167-
return nil, nil
167+
return f, nil
168168
}
169169

170170
func (f *NoOpInformer) RemoveEventHandler(_ cache.ResourceEventHandlerRegistration) error {

processor/k8sattributesprocessor/internal/kube/kube.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type Client interface {
9191
GetPod(PodIdentifier) (*Pod, bool)
9292
GetNamespace(string) (*Namespace, bool)
9393
GetNode(string) (*Node, bool)
94-
Start()
94+
Start() error
9595
Stop()
9696
}
9797

processor/k8sattributesprocessor/processor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ func (kp *kubernetesprocessor) Start(_ context.Context, _ component.Host) error
7171
}
7272
}
7373
if !kp.passthroughMode {
74-
go kp.kc.Start()
74+
err := kp.kc.Start()
75+
if err != nil {
76+
kp.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
77+
return nil
78+
}
7579
}
7680
return nil
7781
}

0 commit comments

Comments
 (0)