Skip to content

Commit

Permalink
feat: add formalized log (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiying-lin authored Sep 8, 2023
1 parent 90dd8a1 commit 061e2b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/clusterresourceplacement/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r *Reconciler) fetchClusterScopedResources(selector fleetv1beta1.ClusterRe
}
restMapping, err := r.RestMapper.RESTMapping(gk, selector.Version)
if err != nil {
return nil, controller.NewUserError(fmt.Errorf("failed to get GVR of the selector: %w", err))
return nil, controller.NewUserError(fmt.Errorf("invalid placement %s, failed to get GVR of the selector: %w", placeName, err))
}
gvr := restMapping.Resource
gvk := schema.GroupVersionKind{
Expand All @@ -133,7 +133,7 @@ func (r *Reconciler) fetchClusterScopedResources(selector fleetv1beta1.ClusterRe
Kind: selector.Kind,
}
if !r.InformerManager.IsClusterScopedResources(gvk) {
return nil, controller.NewUserError(fmt.Errorf("%+v is not a cluster scoped resource", restMapping.Resource))
return nil, controller.NewUserError(fmt.Errorf("invalid placement %s: %+v is not a cluster scoped resource", placeName, restMapping.Resource))
}
if !r.InformerManager.IsInformerSynced(gvr) {
return nil, controller.NewExpectedBehaviorError(fmt.Errorf("informer cache for %+v is not synced yet", restMapping.Resource))
Expand Down
5 changes: 4 additions & 1 deletion pkg/utils/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var (

// NewUnexpectedBehaviorError returns ErrUnexpectedBehavior type error when err is not nil.
func NewUnexpectedBehaviorError(err error) error {
// TODO(zhiying) emit error metrics or well defined logs
if err != nil {
klog.ErrorS(err, "Unexpected behavior identified by the controller")
return fmt.Errorf("%w: %v", ErrUnexpectedBehavior, err.Error())
}
return nil
Expand All @@ -56,6 +56,7 @@ func NewUnexpectedBehaviorError(err error) error {
// NewExpectedBehaviorError returns ErrExpectedBehavior type error when err is not nil.
func NewExpectedBehaviorError(err error) error {
if err != nil {
klog.ErrorS(err, "Expected behavior which can be recovered by itself")
return fmt.Errorf("%w: %v", ErrExpectedBehavior, err.Error())
}
return nil
Expand All @@ -67,6 +68,7 @@ func NewAPIServerError(fromCache bool, err error) error {
if fromCache && isUnexpectedCacheError(err) {
return NewUnexpectedBehaviorError(err)
}
klog.ErrorS(err, "Error returned by the API server", "fromCache", fromCache, "reason", apierrors.ReasonForError(err))
return fmt.Errorf("%w: %v", ErrAPIServerError, err.Error())
}
return nil
Expand All @@ -81,6 +83,7 @@ func isUnexpectedCacheError(err error) bool {
// NewUserError returns ErrUserError type error when err is not nil.
func NewUserError(err error) error {
if err != nil {
klog.ErrorS(err, "Failed to process the request due to a client error")
return fmt.Errorf("%w: %v", ErrUserError, err.Error())
}
return nil
Expand Down

0 comments on commit 061e2b1

Please sign in to comment.