Repository: knative/pkg
Pull request #2100 (2021-04-22 19:34:20)

Allow errors.As to work with reconciler.Events
# Changes

- :gift: `reconciler.Event`s that wrap errors can be used with the [apimachinery errors package](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/errors), which uses [errors.As](https://github.com/kubernetes/apimachinery/blob/v0.21.0/pkg/api/errors/errors.go#L649-L654) to compare error types.

/kind enhancement

https://github.com/knative/eventing/issues/5146 involves a bug where we are [trying](https://github.com/knative/eventing/blob/7775e372bdc033da163cfb01194785d11a4f7d93/pkg/reconciler/subscription/subscription.go#L120) to ignore `IsNotFound`-type errors in our finalizer, but right now Events can never be used with these apimachinery predicates.

**Release Note**

```release-note
- reconciler.Events can be cast to their underlying error types
```

knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **tcnghia** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @tcnghia` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[reconciler/OWNERS](https://github.com/knative/pkg/blob/main/reconciler/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["tcnghia"]} -->
knative-prow-robot: @benmoss: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-knative-pkg-unit-tests | 1b71d4193a8be3a0838228cf25aa5b0f498b20d6 | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_pkg/2100/pull-knative-pkg-unit-tests/1385316075901554688) | `/test pull-knative-pkg-unit-tests`

[Full PR test history](https://gubernator.knative.dev/pr/knative_pkg/2100). [Your PR dashboard](https://gubernator.knative.dev/pr/benmoss).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2100#pullrequestreview-642599882" title="LGTM">vaikas</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[reconciler/OWNERS](https://github.com/knative/pkg/blob/main/reconciler/OWNERS)~~ [vaikas]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
knative-prow-robot: New changes are detected. LGTM label has been removed.
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2100#pullrequestreview-642644738" title="Approved">n3wscott</a>*, *<a href="https://github.com/knative/pkg/pull/2100#pullrequestreview-642599882" title="LGTM">vaikas</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[reconciler/OWNERS](https://github.com/knative/pkg/blob/main/reconciler/OWNERS)~~ [n3wscott,vaikas]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
knative-prow-robot: New changes are detected. LGTM label has been removed.
codecov[bot]: # [Codecov](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) Report
> Merging [#2100](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (6749e2d) into [main](https://codecov.io/gh/knative/pkg/commit/1afd049936225f9f16d126b2bf1764b61a7dfac0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (1afd049) will **increase** coverage by `0.07%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 6749e2d differs from pull request most recent head b5e0246. Consider uploading reports for the commit b5e0246 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/knative/pkg/pull/2100/graphs/tree.svg?width=650&height=150&src=pr&token=rlqpDnfxbO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)

```diff
@@            Coverage Diff             @@
##             main    #2100      +/-   ##
==========================================
+ Coverage   67.31%   67.39%   +0.07%     
==========================================
  Files         215      215              
  Lines        9095     9117      +22     
==========================================
+ Hits         6122     6144      +22     
  Misses       2698     2698              
  Partials      275      275              
```


| [Impacted Files](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) | Coverage Δ | |
|---|---|---|
| [reconciler/events.go](https://codecov.io/gh/knative/pkg/pull/2100/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-cmVjb25jaWxlci9ldmVudHMuZ28=) | `100.00% <100.00%> (ø)` | |
| [reconciler/filter.go](https://codecov.io/gh/knative/pkg/pull/2100/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-cmVjb25jaWxlci9maWx0ZXIuZ28=) | `100.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Last update [1afd049...b5e0246](https://codecov.io/gh/knative/pkg/pull/2100?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).

n3wscott: /lgtm

Patch:
diff --git a/reconciler/events.go b/reconciler/events.go
index 611ba2dc1..ecad3a0fa 100644
--- a/reconciler/events.go
+++ b/reconciler/events.go
@@ -87,6 +87,12 @@ func (e *ReconcilerEvent) Is(target error) bool {
 	return errors.Is(err, target)
 }
 
+// As allows ReconcilerEvents to be treated as regular error types.
+func (e *ReconcilerEvent) As(target interface{}) bool {
+	err := fmt.Errorf(e.Format, e.Args...)
+	return errors.As(err, target)
+}
+
 // Error returns the string that is formed by using the format string with the
 // provided args.
 func (e *ReconcilerEvent) Error() string {


Test patch:
diff --git a/reconciler/events_test.go b/reconciler/events_test.go
index 47c4bf70e..d2761711f 100644
--- a/reconciler/events_test.go
+++ b/reconciler/events_test.go
@@ -19,6 +19,7 @@ package reconciler
 import (
 	"errors"
 	"io"
+	"net"
 	"testing"
 
 	"github.com/google/go-cmp/cmp"
@@ -87,6 +88,13 @@ func TestNew_As(t *testing.T) {
 	}
 }
 
+func TestNewWrappedErrors_As(t *testing.T) {
+	err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is a wrapped error, %w", &net.AddrError{})
+	if netErr := new(net.Error); !EventAs(err, netErr) {
+		t.Error("Event expected to be a wrapped net.AddrError but was not")
+	}
+}
+
 func TestNil_As(t *testing.T) {
 	var err error