Skip to content

Commit 639f14d

Browse files
committed
Use contextual logging for cloudevents
Signed-off-by: Jian Qiu <jqiu@redhat.com>
1 parent b1be73e commit 639f14d

29 files changed

Lines changed: 178 additions & 123 deletions

pkg/cloudevents/clients/options/generic.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ func (o *GenericClientOptions[T]) WatcherStore() store.ClientWatcherStore[T] {
9999
}
100100

101101
func (o *GenericClientOptions[T]) AgentClient(ctx context.Context) (generic.CloudEventsClient[T], error) {
102+
logger := klog.FromContext(ctx)
103+
102104
if len(o.clientID) == 0 {
103105
return nil, fmt.Errorf("client id is required")
104106
}
@@ -140,14 +142,14 @@ func (o *GenericClientOptions[T]) AgentClient(ctx context.Context) (generic.Clou
140142
return
141143
case <-cloudEventsClient.ReconnectedChan():
142144
if !o.resync {
143-
klog.V(4).Infof("resync is disabled, do nothing")
145+
logger.V(4).Info("resync is disabled, do nothing")
144146
continue
145147
}
146148

147149
// when receiving a client reconnected signal, we resync all sources for this agent
148150
// TODO after supporting multiple sources, we should only resync agent known sources
149151
if err := cloudEventsClient.Resync(ctx, types.SourceAll); err != nil {
150-
klog.Errorf("failed to send resync request, %v", err)
152+
logger.Error(err, "failed to send resync request")
151153
}
152154
}
153155
}
@@ -161,7 +163,7 @@ func (o *GenericClientOptions[T]) AgentClient(ctx context.Context) (generic.Clou
161163
go func() {
162164
if store.WaitForStoreInit(ctx, o.watcherStore.HasInitiated) {
163165
if err := cloudEventsClient.Resync(ctx, types.SourceAll); err != nil {
164-
klog.Errorf("failed to send resync request, %v", err)
166+
logger.Error(err, "failed to send resync request")
165167
}
166168
}
167169
}()
@@ -170,6 +172,8 @@ func (o *GenericClientOptions[T]) AgentClient(ctx context.Context) (generic.Clou
170172
}
171173

172174
func (o *GenericClientOptions[T]) SourceClient(ctx context.Context) (generic.CloudEventsClient[T], error) {
175+
logger := klog.FromContext(ctx)
176+
173177
if len(o.clientID) == 0 {
174178
return nil, fmt.Errorf("client id is required")
175179
}
@@ -211,13 +215,13 @@ func (o *GenericClientOptions[T]) SourceClient(ctx context.Context) (generic.Clo
211215
return
212216
case <-cloudEventsClient.ReconnectedChan():
213217
if !o.resync {
214-
klog.V(4).Infof("resync is disabled, do nothing")
218+
logger.V(4).Info("resync is disabled, do nothing")
215219
continue
216220
}
217221

218222
// when receiving a client reconnected signal, we resync all clusters for this source
219223
if err := cloudEventsClient.Resync(ctx, types.ClusterAll); err != nil {
220-
klog.Errorf("failed to send resync request, %v", err)
224+
logger.Error(err, "failed to send resync request")
221225
}
222226
}
223227
}
@@ -231,7 +235,7 @@ func (o *GenericClientOptions[T]) SourceClient(ctx context.Context) (generic.Clo
231235
go func() {
232236
if store.WaitForStoreInit(ctx, o.watcherStore.HasInitiated) {
233237
if err := cloudEventsClient.Resync(ctx, types.ClusterAll); err != nil {
234-
klog.Errorf("failed to send resync request, %v", err)
238+
logger.Error(err, "failed to send resync request")
235239
}
236240
}
237241
}()

pkg/cloudevents/clients/store/informer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package store
22

33
import (
4+
"context"
45
"fmt"
56

67
"k8s.io/apimachinery/pkg/api/meta"
@@ -50,7 +51,9 @@ func (s *AgentInformerWatcherStore[T]) Delete(resource runtime.Object) error {
5051
return nil
5152
}
5253

53-
func (s *AgentInformerWatcherStore[T]) HandleReceivedResource(action types.ResourceAction, resource T) error {
54+
func (s *AgentInformerWatcherStore[T]) HandleReceivedResource(ctx context.Context, action types.ResourceAction, resource T) error {
55+
logger := klog.FromContext(ctx)
56+
5457
switch action {
5558
case types.Added:
5659
newObj, err := utils.ToRuntimeObject(resource)
@@ -75,7 +78,8 @@ func (s *AgentInformerWatcherStore[T]) HandleReceivedResource(action types.Resou
7578

7679
// prevent the resource from being updated if it is deleting
7780
if !lastObj.GetDeletionTimestamp().IsZero() {
78-
klog.Warningf("the resource %s/%s is deleting, ignore the update", newObj.GetNamespace(), newObj.GetName())
81+
logger.Info("the resource is deleting, ignore the update",
82+
"resourceNamespace", newObj.GetNamespace(), "resourceName", newObj.GetName())
7983
return nil
8084
}
8185

pkg/cloudevents/clients/store/informer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ func TestWatch(t *testing.T) {
189189
}
190190
}()
191191

192-
if err := watchStore.HandleReceivedResource(types.Added, &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test0"}}); err != nil {
192+
if err := watchStore.HandleReceivedResource(ctx, types.Added, &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test0"}}); err != nil {
193193
t.Error(err)
194194
}
195-
if err := watchStore.HandleReceivedResource(types.Modified, &clusterv1.ManagedCluster{
195+
if err := watchStore.HandleReceivedResource(ctx, types.Modified, &clusterv1.ManagedCluster{
196196
ObjectMeta: metav1.ObjectMeta{
197197
Name: "test1",
198198
},
@@ -201,7 +201,7 @@ func TestWatch(t *testing.T) {
201201
}}); err != nil {
202202
t.Error(err)
203203
}
204-
if err := watchStore.HandleReceivedResource(types.Deleted, &clusterv1.ManagedCluster{
204+
if err := watchStore.HandleReceivedResource(ctx, types.Deleted, &clusterv1.ManagedCluster{
205205
ObjectMeta: metav1.ObjectMeta{
206206
Name: "test1",
207207
DeletionTimestamp: &metav1.Time{Time: time.Now()},

pkg/cloudevents/clients/store/interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type ClientWatcherStore[T generic.ResourceObject] interface {
3434
GetWatcher(namespace string, opts metav1.ListOptions) (watch.Interface, error)
3535

3636
// HandleReceivedResource handles the client received resource events.
37-
HandleReceivedResource(action types.ResourceAction, resource T) error
37+
HandleReceivedResource(ctx context.Context, action types.ResourceAction, resource T) error
3838

3939
// Add will be called by resource client when adding resources. The implementation is based on the specific
4040
// watcher store, in some case, it does not need to update a store, but just send a watch event.
@@ -63,6 +63,7 @@ type ClientWatcherStore[T generic.ResourceObject] interface {
6363
}
6464

6565
func WaitForStoreInit(ctx context.Context, cacheSyncs ...StoreInitiated) bool {
66+
logger := klog.FromContext(ctx)
6667
err := wait.PollUntilContextCancel(
6768
ctx,
6869
syncedPollPeriod,
@@ -77,7 +78,7 @@ func WaitForStoreInit(ctx context.Context, cacheSyncs ...StoreInitiated) bool {
7778
},
7879
)
7980
if err != nil {
80-
klog.Errorf("stop WaitForStoreInit, %v", err)
81+
logger.Error(err, "stop WaitForStoreInit")
8182
return false
8283
}
8384

pkg/cloudevents/clients/store/simplestore.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package store
22

33
import (
4+
"context"
45
"fmt"
56

67
"k8s.io/apimachinery/pkg/api/meta"
@@ -50,7 +51,9 @@ func (s *SimpleStore[T]) HasInitiated() bool {
5051
return true
5152
}
5253

53-
func (s *SimpleStore[T]) HandleReceivedResource(action types.ResourceAction, resource T) error {
54+
func (s *SimpleStore[T]) HandleReceivedResource(ctx context.Context, action types.ResourceAction, resource T) error {
55+
logger := klog.FromContext(ctx)
56+
5457
switch action {
5558
case types.Added:
5659
newObj, err := utils.ToRuntimeObject(resource)
@@ -75,7 +78,8 @@ func (s *SimpleStore[T]) HandleReceivedResource(action types.ResourceAction, res
7578

7679
// prevent the resource from being updated if it is deleting
7780
if !lastObj.GetDeletionTimestamp().IsZero() {
78-
klog.Warningf("the resource %s/%s is deleting, ignore the update", newObj.GetNamespace(), newObj.GetName())
81+
logger.Info("the resource is deleting, ignore the update",
82+
"resourceNamespace", newObj.GetNamespace(), "resourceName", newObj.GetName())
7983
return nil
8084
}
8185

pkg/cloudevents/clients/store/simplestore_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package store
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

@@ -108,7 +109,7 @@ func TestHandleReceivedResource(t *testing.T) {
108109

109110
for _, c := range cases {
110111
t.Run(c.name, func(t *testing.T) {
111-
err := store.HandleReceivedResource(c.action, c.received)
112+
err := store.HandleReceivedResource(context.Background(), c.action, c.received)
112113
if err != nil {
113114
t.Error(err)
114115
}

pkg/cloudevents/clients/work/agent/client/manifestwork.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func (c *ManifestWorkAgentClient) DeleteCollection(ctx context.Context, opts met
7676
}
7777

7878
func (c *ManifestWorkAgentClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*workv1.ManifestWork, error) {
79-
klog.V(4).Infof("getting manifestwork %s/%s", c.namespace, name)
79+
logger := klog.FromContext(ctx)
80+
81+
logger.V(4).Info("getting manifestwork", "manifestWorkNamespace", c.namespace, "manifestWorkName", name)
8082
work, exists, err := c.watcherStore.Get(c.namespace, name)
8183
if err != nil {
8284
returnErr := errors.NewInternalError(err)
@@ -94,7 +96,8 @@ func (c *ManifestWorkAgentClient) Get(ctx context.Context, name string, opts met
9496
}
9597

9698
func (c *ManifestWorkAgentClient) List(ctx context.Context, opts metav1.ListOptions) (*workv1.ManifestWorkList, error) {
97-
klog.V(4).Infof("list manifestworks from cluster %s", c.namespace)
99+
logger := klog.FromContext(ctx)
100+
logger.V(4).Info("list manifestworks from cluster", "cluster", c.namespace)
98101
works, err := c.watcherStore.List(c.namespace, opts)
99102
if err != nil {
100103
returnErr := errors.NewInternalError(err)
@@ -112,7 +115,8 @@ func (c *ManifestWorkAgentClient) List(ctx context.Context, opts metav1.ListOpti
112115
}
113116

114117
func (c *ManifestWorkAgentClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
115-
klog.V(4).Infof("watch manifestworks from cluster %s", c.namespace)
118+
logger := klog.FromContext(ctx)
119+
logger.V(4).Info("watch manifestworks from cluster", "cluster", c.namespace)
116120
watcher, err := c.watcherStore.GetWatcher(c.namespace, opts)
117121
if err != nil {
118122
returnErr := errors.NewInternalError(err)
@@ -125,7 +129,8 @@ func (c *ManifestWorkAgentClient) Watch(ctx context.Context, opts metav1.ListOpt
125129
}
126130

127131
func (c *ManifestWorkAgentClient) Patch(ctx context.Context, name string, pt kubetypes.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *workv1.ManifestWork, err error) {
128-
klog.V(4).Infof("patching manifestwork %s/%s", c.namespace, name)
132+
logger := klog.FromContext(ctx)
133+
logger.V(4).Info("patching manifestwork", "manifestWorkNamespace", c.namespace, "manifestWorkName", name)
129134
lastWork, exists, err := c.watcherStore.Get(c.namespace, name)
130135
if err != nil {
131136
returnErr := errors.NewInternalError(err)

pkg/cloudevents/clients/work/source/client/manifestwork.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func (c *ManifestWorkSourceClient) DeleteCollection(ctx context.Context, opts me
185185
}
186186

187187
func (c *ManifestWorkSourceClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*workv1.ManifestWork, error) {
188-
klog.V(4).Infof("getting manifestwork %s", name)
188+
logger := klog.FromContext(ctx)
189+
logger.V(4).Info("getting manifestwork", "manifestWorkName", name)
189190
work, exists, err := c.watcherStore.Get(c.namespace, name)
190191
if err != nil {
191192
returnErr := errors.NewInternalError(err)
@@ -203,7 +204,8 @@ func (c *ManifestWorkSourceClient) Get(ctx context.Context, name string, opts me
203204
}
204205

205206
func (c *ManifestWorkSourceClient) List(ctx context.Context, opts metav1.ListOptions) (*workv1.ManifestWorkList, error) {
206-
klog.V(4).Infof("list manifestworks")
207+
logger := klog.FromContext(ctx)
208+
logger.V(4).Info("list manifestworks")
207209
works, err := c.watcherStore.List(c.namespace, opts)
208210
if err != nil {
209211
returnErr := errors.NewInternalError(err)
@@ -233,7 +235,8 @@ func (c *ManifestWorkSourceClient) Watch(ctx context.Context, opts metav1.ListOp
233235
}
234236

235237
func (c *ManifestWorkSourceClient) Patch(ctx context.Context, name string, pt kubetypes.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *workv1.ManifestWork, err error) {
236-
klog.V(4).Infof("patching manifestwork %s", name)
238+
logger := klog.FromContext(ctx)
239+
logger.V(4).Info("patching manifestwork", "manifestWorkName", name)
237240

238241
if len(subresources) != 0 {
239242
msg := fmt.Sprintf("unsupported to update subresources %v", subresources)

pkg/cloudevents/clients/work/store/base.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package store
22

33
import (
4+
"context"
45
"fmt"
56
"strconv"
67
"time"
@@ -27,7 +28,7 @@ type baseSourceStore struct {
2728
receivedWorks workqueue.TypedRateLimitingInterface[*workv1.ManifestWork]
2829
}
2930

30-
func (bs *baseSourceStore) HandleReceivedResource(action types.ResourceAction, work *workv1.ManifestWork) error {
31+
func (bs *baseSourceStore) HandleReceivedResource(_ context.Context, action types.ResourceAction, work *workv1.ManifestWork) error {
3132
switch action {
3233
case types.StatusModified:
3334
bs.receivedWorks.Add(work)
@@ -50,26 +51,26 @@ func newWorkProcessor(works workqueue.TypedRateLimitingInterface[*workv1.Manifes
5051
}
5152
}
5253

53-
func (b *workProcessor) run(stopCh <-chan struct{}) {
54+
func (b *workProcessor) run(ctx context.Context) {
5455
defer b.works.ShutDown()
5556

5657
// start a goroutine to handle the works from the queue
5758
// the .Until will re-kick the runWorker one second after the runWorker completes
58-
go wait.Until(b.runWorker, time.Second, stopCh)
59+
go wait.UntilWithContext(ctx, b.runWorker, time.Second)
5960

6061
// wait until we're told to stop
61-
<-stopCh
62+
<-ctx.Done()
6263
}
6364

64-
func (b *workProcessor) runWorker() {
65+
func (b *workProcessor) runWorker(ctx context.Context) {
6566
// hot loop until we're told to stop. processNextEvent will automatically wait until there's work available, so
6667
// we don't worry about secondary waits
67-
for b.processNextWork() {
68+
for b.processNextWork(ctx) {
6869
}
6970
}
7071

7172
// processNextWork deals with one key off the queue.
72-
func (b *workProcessor) processNextWork() bool {
73+
func (b *workProcessor) processNextWork(ctx context.Context) bool {
7374
// pull the next event item from queue.
7475
// events queue blocks until it can return an item to be processed
7576
key, quit := b.works.Get()
@@ -79,7 +80,7 @@ func (b *workProcessor) processNextWork() bool {
7980
}
8081
defer b.works.Done(key)
8182

82-
if err := b.handleWork(key); err != nil {
83+
if err := b.handleWork(ctx, key); err != nil {
8384
// we failed to handle the work, we should requeue the item to work on later
8485
// this method will add a backoff to avoid hotlooping on particular items
8586
b.works.AddRateLimited(key)
@@ -91,8 +92,9 @@ func (b *workProcessor) processNextWork() bool {
9192
return true
9293
}
9394

94-
func (b *workProcessor) handleWork(work *workv1.ManifestWork) error {
95-
lastWork := b.getWork(work.UID)
95+
func (b *workProcessor) handleWork(ctx context.Context, work *workv1.ManifestWork) error {
96+
logger := klog.FromContext(ctx).WithValues("manifestWorkNamespace", work.Namespace, "manifestWorkName", work.Name)
97+
lastWork := b.getWork(ctx, work.UID)
9698
if lastWork == nil {
9799
// the work is not found from the local cache and it has been deleted by the agent,
98100
// ignore this work.
@@ -116,20 +118,20 @@ func (b *workProcessor) handleWork(work *workv1.ManifestWork) error {
116118

117119
lastResourceVersion, err := strconv.Atoi(lastWork.ResourceVersion)
118120
if err != nil {
119-
klog.Errorf("invalid resource version for work %s/%s, %v", lastWork.Namespace, lastWork.Name, err)
121+
logger.Error(err, "invalid resource version for work")
120122
return nil
121123
}
122124

123125
resourceVersion, err := strconv.Atoi(work.ResourceVersion)
124126
if err != nil {
125-
klog.Errorf("invalid resource version for work %s/%s, %v", lastWork.Namespace, lastWork.Name, err)
127+
logger.Error(err, "invalid resource version for work")
126128
return nil
127129
}
128130

129131
// the current work's version is maintained on source and the agent's work is newer than source, ignore
130132
if lastResourceVersion != 0 && resourceVersion > lastResourceVersion {
131-
klog.Warningf("the work %s/%s resource version %d is great than its generation %d, ignore",
132-
lastWork.Namespace, lastWork.Name, resourceVersion, lastResourceVersion)
133+
logger.Info("the work resource version is great than its generation, ignore",
134+
"agentResourceVersion", resourceVersion, "sourceResourceVersion", lastResourceVersion)
133135
return nil
134136
}
135137

@@ -140,13 +142,13 @@ func (b *workProcessor) handleWork(work *workv1.ManifestWork) error {
140142
sequenceID := work.Annotations[common.CloudEventsSequenceIDAnnotationKey]
141143
greater, err := utils.CompareSnowflakeSequenceIDs(lastSequenceID, sequenceID)
142144
if err != nil {
143-
klog.Errorf("invalid sequenceID for work %s/%s, %v", lastWork.Namespace, lastWork.Name, err)
145+
logger.Error(err, "invalid sequenceID for work")
144146
return nil
145147
}
146148

147149
if !greater {
148-
klog.Warningf("the work %s/%s current sequenceID %s is less than its last %s, ignore",
149-
lastWork.Namespace, lastWork.Name, sequenceID, lastSequenceID)
150+
logger.Info("the work current sequenceID is less than its last, ignore",
151+
"currentSequenceID", sequenceID, "lastSequenceID", lastSequenceID)
150152
return nil
151153
}
152154

@@ -163,10 +165,11 @@ func (b *workProcessor) handleWork(work *workv1.ManifestWork) error {
163165
return b.store.Update(updatedWork)
164166
}
165167

166-
func (b *workProcessor) getWork(uid kubetypes.UID) *workv1.ManifestWork {
168+
func (b *workProcessor) getWork(ctx context.Context, uid kubetypes.UID) *workv1.ManifestWork {
169+
logger := klog.FromContext(ctx)
167170
works, err := b.store.ListAll()
168171
if err != nil {
169-
klog.Errorf("failed to lists works, %v", err)
172+
logger.Error(err, "failed to lists works")
170173
return nil
171174
}
172175

0 commit comments

Comments
 (0)