1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

create failure events on errors (#5988)

Signed-off-by: ShutingZhao <shuting@nirmata.com>

Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
shuting 2023-01-13 18:22:03 +08:00 committed by GitHub
parent 330709a7b4
commit b66b06b70b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -170,9 +170,10 @@ func (h *handlers) executePolicy(ctx context.Context, logger logr.Logger, policy
if err := h.client.DeleteResource(ctx, resource.GetAPIVersion(), resource.GetKind(), namespace, name, false); err != nil {
debug.Error(err, "failed to delete resource")
errs = append(errs, err)
h.createEvent(policy, resource, err)
} else {
debug.Info("deleted")
h.createEvent(policy, resource)
h.createEvent(policy, resource, nil)
}
}
}
@ -181,9 +182,7 @@ func (h *handlers) executePolicy(ctx context.Context, logger logr.Logger, policy
return multierr.Combine(errs...)
}
func (h *handlers) createEvent(policy kyvernov2alpha1.CleanupPolicyInterface, resource unstructured.Unstructured) {
msg := fmt.Sprintf("successfully cleaned up the target resource %v/%v/%v", resource.GetKind(), resource.GetNamespace(), resource.GetName())
func (h *handlers) createEvent(policy kyvernov2alpha1.CleanupPolicyInterface, resource unstructured.Unstructured, err error) {
var cleanuppol runtime.Object
if policy.GetNamespace() == "" {
cleanuppol = policy.(*kyvernov2alpha1.ClusterCleanupPolicy)
@ -191,5 +190,12 @@ func (h *handlers) createEvent(policy kyvernov2alpha1.CleanupPolicyInterface, re
cleanuppol = policy.(*kyvernov2alpha1.CleanupPolicy)
}
h.recorder.Event(cleanuppol, corev1.EventTypeNormal, event.PolicyApplied.String(), msg)
switch err == nil {
case true:
msg := fmt.Sprintf("successfully cleaned up the target resource %v/%v/%v", resource.GetKind(), resource.GetNamespace(), resource.GetName())
h.recorder.Event(cleanuppol, corev1.EventTypeNormal, event.PolicyApplied.String(), msg)
case false:
msg := fmt.Sprintf("failed to clean up the target resource %v/%v/%v: %v", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err.Error())
h.recorder.Event(cleanuppol, corev1.EventTypeWarning, event.PolicyError.String(), msg)
}
}