Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/pipelinerun/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pipelinerun

import (
"context"
"errors"
"sync"
"time"

Expand Down Expand Up @@ -68,6 +69,11 @@ func (t *Tracker) Monitor(allowed []string) <-chan []taskrunpkg.Run {

genericInformer, _ := factory.ForResource(*gvr)
informer := genericInformer.Informer()

// Set a custom watch error handler that ignores context.Canceled errors
// to prevent "Failed to watch" log messages when the informer is stopped intentionally
_ = informer.SetWatchErrorHandlerWithContext(watchErrorHandler)
Copy link
Member

Choose a reason for hiding this comment

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

Unused return value

The error return from SetWatchErrorHandlerWithContext is ignored here. This is probably fine since it's a configuration step during initialization, but if you wanted to be more defensive, you could log any errors:

if err := informer.SetWatchErrorHandlerWithContext(watchErrorHandler); err != nil {
    // Log or handle the error
}

That said, the current approach is acceptable for simplicity.


mu := &sync.Mutex{}
stopC := make(chan struct{})
trC := make(chan []taskrunpkg.Run)
Expand Down Expand Up @@ -154,7 +160,15 @@ func pipelinerunOpts(name string) func(opts *metav1.ListOptions) {
return func(opts *metav1.ListOptions) {
opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", name).String()
}
}

// watchErrorHandler is a custom watch error handler that filters out context.Canceled errors
// to prevent "Failed to watch" log messages when the informer is stopped intentionally.
// Other errors are passed to the default handler.
func watchErrorHandler(ctx context.Context, r *cache.Reflector, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

💡 Code Duplication (Optional)

This watchErrorHandler function is duplicated in both pkg/pipelinerun/tracker.go and pkg/pods/pod.go.

Options:

  1. Keep as-is (acceptable) - The function is simple (4 lines) and keeping it local to each package avoids introducing dependencies
  2. Extract to shared utility - Could move to something like pkg/utils/watch/error_handler.go if you're concerned about maintainability

Given the simplicity, the current approach is perfectly reasonable. This is more of an FYI than a required change.

if !errors.Is(err, context.Canceled) {
cache.DefaultWatchErrorHandler(ctx, r, err)
}
}

// handles changes to pipelinerun and pushes the Run information to the
Expand Down
27 changes: 27 additions & 0 deletions pkg/pipelinerun/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package pipelinerun

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -239,3 +241,28 @@ func startPipelineRun(t *testing.T, data pipelinetest.Data, prStatus ...v1.Pipel
Dynamic: dynamic,
}
}

func TestTracker_watchErrorHandler(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Excellent test coverage!

Love that you're testing both direct context.Canceled and wrapped versions with errors.Join(). This ensures the errors.Is() logic works correctly in all cases.

The approach of passing nil for the reflector is clever since the handler should filter these errors before reaching the code that would use it.

tests := []struct {
name string
err error
}{
{
name: "context.Canceled should be filtered",
err: context.Canceled,
},
{
name: "wrapped context.Canceled should be filtered",
err: errors.Join(errors.New("watch failed"), context.Canceled),
},
}

for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
// Call watchErrorHandler with context.Canceled errors
// These should be filtered (not passed to DefaultWatchErrorHandler)
// so passing nil reflector is safe
watchErrorHandler(context.Background(), nil, tt.err)
})
}
}
17 changes: 16 additions & 1 deletion pkg/pods/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pods

import (
"context"
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -115,7 +116,12 @@ func (p *Pod) watcher(stopC chan struct{}, result *podResult, mu *sync.Mutex) {
}
}

_, err := factory.Core().V1().Pods().Informer().AddEventHandler(
informer := factory.Core().V1().Pods().Informer()
// Set a custom watch error handler that ignores context.Canceled errors
// to prevent "Failed to watch" log messages when the informer is stopped intentionally
_ = informer.SetWatchErrorHandlerWithContext(watchErrorHandler)

_, err := informer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
select {
Expand Down Expand Up @@ -156,6 +162,15 @@ func podOpts(name string) func(opts *metav1.ListOptions) {
}
}

// watchErrorHandler is a custom watch error handler that filters out context.Canceled errors
// to prevent "Failed to watch" log messages when the informer is stopped intentionally.
// Other errors are passed to the default handler.
func watchErrorHandler(ctx context.Context, r *cache.Reflector, err error) {
if !errors.Is(err, context.Canceled) {
cache.DefaultWatchErrorHandler(ctx, r, err)
}
}

func checkPodStatus(obj interface{}) (*corev1.Pod, error) {
pod, ok := obj.(*corev1.Pod)
if !ok {
Expand Down
27 changes: 27 additions & 0 deletions pkg/pods/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package pods

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -212,3 +214,28 @@ func simulateDeleteWatch(t *testing.T, initial *corev1.Pod, later *corev1.Pod) k

return clients.Kube
}

func Test_watchErrorHandler(t *testing.T) {
tests := []struct {
name string
err error
}{
{
name: "context.Canceled should be filtered",
err: context.Canceled,
},
{
name: "wrapped context.Canceled should be filtered",
err: errors.Join(errors.New("watch failed"), context.Canceled),
},
}

for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
// Call watchErrorHandler with context.Canceled errors
// These should be filtered (not passed to DefaultWatchErrorHandler)
// so passing nil reflector is safe
watchErrorHandler(context.Background(), nil, tt.err)
})
}
}
Loading