From 123a4f5128bae6deb5dc428a22ef577f57f53635 Mon Sep 17 00:00:00 2001 From: shuting Date: Tue, 26 Apr 2022 13:40:05 +0800 Subject: [PATCH] bug: fix nil pointer when generating events (#3677) * fix nil pointer when generating events Signed-off-by: ShutingZhao * rename error message of form ErrFoo Signed-off-by: ShutingZhao --- pkg/background/common/report.go | 21 +++++++++++++++++---- pkg/background/generate/generate.go | 2 +- pkg/background/mutate/mutate.go | 7 +++---- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pkg/background/common/report.go b/pkg/background/common/report.go index a9f4296ca8..b61bd18362 100644 --- a/pkg/background/common/report.go +++ b/pkg/background/common/report.go @@ -3,12 +3,20 @@ package common import ( "fmt" + "github.com/go-logr/logr" "github.com/kyverno/kyverno/pkg/event" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) -func FailedEvents(err error, policy, rule string, source event.Source, resource unstructured.Unstructured) []event.Info { - re := newEvent(policy, rule, source, resource) +var ErrEmptyPatch error = fmt.Errorf("empty resource to patch") + +func FailedEvents(err error, policy, rule string, source event.Source, resource *unstructured.Unstructured, logger logr.Logger) []event.Info { + if resource == nil { + logger.WithName("FailedEvents").Error(ErrEmptyPatch, "", "rule", rule) + return nil + } + + re := newEvent(policy, rule, source, *resource) re.Reason = event.PolicyFailed.String() re.Message = fmt.Sprintf("policy %s/%s failed to apply to %s/%s/%s: %v", policy, rule, resource.GetKind(), resource.GetNamespace(), resource.GetName(), err) @@ -16,8 +24,13 @@ func FailedEvents(err error, policy, rule string, source event.Source, resource return []event.Info{re} } -func SucceedEvents(policy, rule string, source event.Source, resource unstructured.Unstructured) []event.Info { - re := newEvent(policy, rule, source, resource) +func SucceedEvents(policy, rule string, source event.Source, resource *unstructured.Unstructured, logger logr.Logger) []event.Info { + if resource == nil { + logger.WithName("SucceedEvents").Error(ErrEmptyPatch, "", "rule", rule) + return nil + } + + re := newEvent(policy, rule, source, *resource) re.Reason = event.PolicyApplied.String() re.Message = fmt.Sprintf("policy %s/%s applied to %s/%s/%s successfully", policy, rule, resource.GetKind(), resource.GetNamespace(), resource.GetName()) diff --git a/pkg/background/generate/generate.go b/pkg/background/generate/generate.go index 88149767c8..7749e7d660 100644 --- a/pkg/background/generate/generate.go +++ b/pkg/background/generate/generate.go @@ -167,7 +167,7 @@ func (c *GenerateController) ProcessGR(gr *urkyverno.UpdateRequest) error { } // 3 - Report failure Events - events := common.FailedEvents(err, gr.Spec.Policy, "", event.GeneratePolicyController, *resource) + events := common.FailedEvents(err, gr.Spec.Policy, "", event.GeneratePolicyController, resource, logger) c.eventGen.Add(events...) } diff --git a/pkg/background/mutate/mutate.go b/pkg/background/mutate/mutate.go index 9ab8384e7f..28fa8a87f2 100644 --- a/pkg/background/mutate/mutate.go +++ b/pkg/background/mutate/mutate.go @@ -128,8 +128,7 @@ func (c *MutateExistingController) ProcessUR(ur *urkyverno.UpdateRequest) error } if patchedNew == nil { - err := fmt.Errorf("empty resource to patch") - logger.Error(err, "", "rule", r.Name, "message", r.Message) + logger.Error(common.ErrEmptyPatch, "", "rule", r.Name, "message", r.Message) errs = append(errs, err) continue } @@ -173,9 +172,9 @@ func (c *MutateExistingController) report(err error, policy, rule string, target } if err != nil { - events = common.FailedEvents(err, policy, rule, event.MutateExistingController, *target) + events = common.FailedEvents(err, policy, rule, event.MutateExistingController, target, c.log) } else { - events = common.SucceedEvents(policy, rule, event.MutateExistingController, *target) + events = common.SucceedEvents(policy, rule, event.MutateExistingController, target, c.log) } c.eventGen.Add(events...)