From 6f322d72e84328e91c3cf69a0837a7a2934a715a Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Fri, 30 Jul 2021 09:25:25 -0700 Subject: [PATCH] Ignore special errors in codegen for events --- .../v1/customresourcedefinition/reconciler.go | 10 ++++++++-- .../customresourcedefinition/reconciler.go | 10 ++++++++-- .../apps/v1/deployment/reconciler.go | 10 ++++++++-- .../reconciler/core/v1/namespace/reconciler.go | 10 ++++++++-- .../reconciler/core/v1/secret/reconciler.go | 10 ++++++++-- .../generators/reconciler_reconciler.go | 18 ++++++++++++++++-- 6 files changed, 56 insertions(+), 12 deletions(-) diff --git a/client/injection/apiextensions/reconciler/apiextensions/v1/customresourcedefinition/reconciler.go b/client/injection/apiextensions/reconciler/apiextensions/v1/customresourcedefinition/reconciler.go index 61e0b05b68..4198d8bf50 100644 --- a/client/injection/apiextensions/reconciler/apiextensions/v1/customresourcedefinition/reconciler.go +++ b/client/injection/apiextensions/reconciler/apiextensions/v1/customresourcedefinition/reconciler.go @@ -297,8 +297,14 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error { return nil } - logger.Errorw("Returned an error", zap.Error(reconcileEvent)) - r.Recorder.Event(resource, corev1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + if controller.IsSkipKey(reconcileEvent) { + // This is a wrapped error, don't emit an event. + } else if ok, _ := controller.IsRequeueKey(reconcileEvent); ok { + // This is a wrapped error, don't emit an event. + } else { + logger.Errorw("Returned an error", zap.Error(reconcileEvent)) + r.Recorder.Event(resource, corev1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + } return reconcileEvent } diff --git a/client/injection/apiextensions/reconciler/apiextensions/v1beta1/customresourcedefinition/reconciler.go b/client/injection/apiextensions/reconciler/apiextensions/v1beta1/customresourcedefinition/reconciler.go index e470182046..4b71379c89 100644 --- a/client/injection/apiextensions/reconciler/apiextensions/v1beta1/customresourcedefinition/reconciler.go +++ b/client/injection/apiextensions/reconciler/apiextensions/v1beta1/customresourcedefinition/reconciler.go @@ -297,8 +297,14 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error { return nil } - logger.Errorw("Returned an error", zap.Error(reconcileEvent)) - r.Recorder.Event(resource, v1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + if controller.IsSkipKey(reconcileEvent) { + // This is a wrapped error, don't emit an event. + } else if ok, _ := controller.IsRequeueKey(reconcileEvent); ok { + // This is a wrapped error, don't emit an event. + } else { + logger.Errorw("Returned an error", zap.Error(reconcileEvent)) + r.Recorder.Event(resource, v1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + } return reconcileEvent } diff --git a/client/injection/kube/reconciler/apps/v1/deployment/reconciler.go b/client/injection/kube/reconciler/apps/v1/deployment/reconciler.go index 43ef90341d..7f2baee83c 100644 --- a/client/injection/kube/reconciler/apps/v1/deployment/reconciler.go +++ b/client/injection/kube/reconciler/apps/v1/deployment/reconciler.go @@ -297,8 +297,14 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error { return nil } - logger.Errorw("Returned an error", zap.Error(reconcileEvent)) - r.Recorder.Event(resource, corev1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + if controller.IsSkipKey(reconcileEvent) { + // This is a wrapped error, don't emit an event. + } else if ok, _ := controller.IsRequeueKey(reconcileEvent); ok { + // This is a wrapped error, don't emit an event. + } else { + logger.Errorw("Returned an error", zap.Error(reconcileEvent)) + r.Recorder.Event(resource, corev1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + } return reconcileEvent } diff --git a/client/injection/kube/reconciler/core/v1/namespace/reconciler.go b/client/injection/kube/reconciler/core/v1/namespace/reconciler.go index a85973728a..5861729d06 100644 --- a/client/injection/kube/reconciler/core/v1/namespace/reconciler.go +++ b/client/injection/kube/reconciler/core/v1/namespace/reconciler.go @@ -296,8 +296,14 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error { return nil } - logger.Errorw("Returned an error", zap.Error(reconcileEvent)) - r.Recorder.Event(resource, v1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + if controller.IsSkipKey(reconcileEvent) { + // This is a wrapped error, don't emit an event. + } else if ok, _ := controller.IsRequeueKey(reconcileEvent); ok { + // This is a wrapped error, don't emit an event. + } else { + logger.Errorw("Returned an error", zap.Error(reconcileEvent)) + r.Recorder.Event(resource, v1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + } return reconcileEvent } diff --git a/client/injection/kube/reconciler/core/v1/secret/reconciler.go b/client/injection/kube/reconciler/core/v1/secret/reconciler.go index 20e15a8be3..9de74d1954 100644 --- a/client/injection/kube/reconciler/core/v1/secret/reconciler.go +++ b/client/injection/kube/reconciler/core/v1/secret/reconciler.go @@ -264,8 +264,14 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error { return nil } - logger.Errorw("Returned an error", zap.Error(reconcileEvent)) - r.Recorder.Event(resource, v1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + if controller.IsSkipKey(reconcileEvent) { + // This is a wrapped error, don't emit an event. + } else if ok, _ := controller.IsRequeueKey(reconcileEvent); ok { + // This is a wrapped error, don't emit an event. + } else { + logger.Errorw("Returned an error", zap.Error(reconcileEvent)) + r.Recorder.Event(resource, v1.EventTypeWarning, "InternalError", reconcileEvent.Error()) + } return reconcileEvent } diff --git a/codegen/cmd/injection-gen/generators/reconciler_reconciler.go b/codegen/cmd/injection-gen/generators/reconciler_reconciler.go index 15fb4259f5..50602cd0bf 100644 --- a/codegen/cmd/injection-gen/generators/reconciler_reconciler.go +++ b/codegen/cmd/injection-gen/generators/reconciler_reconciler.go @@ -210,6 +210,14 @@ func (g *reconcilerReconcilerGenerator) GenerateType(c *generator.Context, t *ty Package: "knative.dev/pkg/reconciler", Name: "DoObserveFinalizeKind", }), + "controllerIsSkipKey": c.Universe.Function(types.Name{ + Package: "knative.dev/pkg/controller", + Name: "IsSkipKey", + }), + "controllerIsRequeueKey": c.Universe.Function(types.Name{ + Package: "knative.dev/pkg/controller", + Name: "IsRequeueKey", + }), } sw.Do(reconcilerInterfaceFactory, m) @@ -517,8 +525,14 @@ func (r *reconcilerImpl) Reconcile(ctx {{.contextContext|raw}}, key string) erro return nil } - logger.Errorw("Returned an error", zap.Error(reconcileEvent)) - r.Recorder.Event(resource, {{.corev1EventTypeWarning|raw}}, "InternalError", reconcileEvent.Error()) + if {{ .controllerIsSkipKey|raw }}(reconcileEvent) { + // This is a wrapped error, don't emit an event. + } else if ok, _ := {{ .controllerIsRequeueKey|raw }}(reconcileEvent); ok { + // This is a wrapped error, don't emit an event. + } else { + logger.Errorw("Returned an error", zap.Error(reconcileEvent)) + r.Recorder.Event(resource, {{.corev1EventTypeWarning|raw}}, "InternalError", reconcileEvent.Error()) + } return reconcileEvent }