Skip to content

Commit

Permalink
style(syncer): log transform func error (#587)
Browse files Browse the repository at this point in the history
## What type of PR is this?

/kind style

## What this PR does / why we need it:

The transform func is called internally by the informer, the error will
be hidden if we don't log them.

## Which issue(s) this PR fixes:

Fixes #
  • Loading branch information
iamryanchia authored Aug 13, 2024
1 parent 48c7531 commit 2b90ce3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/syncer/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"fmt"
"reflect"
"text/template"
"time"

Expand All @@ -28,6 +29,7 @@ import (
"github.com/KusionStack/karpor/pkg/syncer/transform"
"github.com/KusionStack/karpor/pkg/syncer/utils"
sprig "github.com/Masterminds/sprig/v3"
"github.com/go-logr/logr"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -38,6 +40,7 @@ import (
"k8s.io/client-go/dynamic"
clientgocache "k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
ctrlhandler "sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -70,6 +73,8 @@ type informerSource struct {
ctx context.Context
cancel context.CancelFunc
stopped chan struct{}

logger logr.Logger
}

func (s *informerSource) Add(obj interface{}) error {
Expand Down Expand Up @@ -116,6 +121,7 @@ func NewSource(cluster string, client dynamic.Interface, rsr v1beta1.ResourceSyn
ResourceSyncRule: rsr,
client: client,
stopped: make(chan struct{}),
logger: ctrl.Log.WithName(fmt.Sprintf("%s-syncer-source", rsr.Resource)),
}
}

Expand Down Expand Up @@ -258,7 +264,13 @@ func (s *informerSource) parseTransformer() (clientgocache.TransformFunc, error)
return nil, errors.Wrap(err, "invalid transform template")
}

return func(obj interface{}) (interface{}, error) {
return func(obj interface{}) (ret interface{}, err error) {
defer func() {
if err != nil {
s.logger.Error(err, "error in transforming object", "actualType", reflect.TypeOf(obj))
}
}()

u, ok := obj.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("transform: object's type should be *unstructured.Unstructured")
Expand Down

0 comments on commit 2b90ce3

Please sign in to comment.