mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
bug: fix nil pointer when generating events (#3677)
* fix nil pointer when generating events Signed-off-by: ShutingZhao <shuting@nirmata.com> * rename error message of form ErrFoo Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
parent
4cbfecc0d9
commit
123a4f5128
3 changed files with 21 additions and 9 deletions
|
@ -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())
|
||||
|
|
|
@ -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...)
|
||||
}
|
||||
|
||||
|
|
|
@ -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...)
|
||||
|
|
Loading…
Add table
Reference in a new issue