mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
chore: improve log messages (#8442)
Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
parent
828807bddb
commit
ca62b37886
1 changed files with 27 additions and 28 deletions
|
@ -163,50 +163,49 @@ func (c *mutateExistingController) ProcessUR(ur *kyvernov1beta1.UpdateRequest) e
|
||||||
patched, parentGVR, patchedSubresource := r.PatchedTarget()
|
patched, parentGVR, patchedSubresource := r.PatchedTarget()
|
||||||
switch r.Status() {
|
switch r.Status() {
|
||||||
case engineapi.RuleStatusFail, engineapi.RuleStatusError, engineapi.RuleStatusWarn:
|
case engineapi.RuleStatusFail, engineapi.RuleStatusError, engineapi.RuleStatusWarn:
|
||||||
err := fmt.Errorf("failed to mutate existing resource, rule response%v: %s", r.Status(), r.Message())
|
err := fmt.Errorf("failed to mutate existing resource, rule %s, response %v: %s", r.Name(), r.Status(), r.Message())
|
||||||
logger.Error(err, "")
|
logger.Error(err, "")
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
c.report(err, policy, rule.Name, patched)
|
c.report(err, policy, rule.Name, patched)
|
||||||
|
|
||||||
case engineapi.RuleStatusSkip:
|
case engineapi.RuleStatusSkip:
|
||||||
logger.Info("mutate existing rule skipped", "rule", r.Name(), "message", r.Message())
|
err := fmt.Errorf("mutate existing rule skipped, rule %s, response %v: %s", r.Name(), r.Status(), r.Message())
|
||||||
|
logger.Error(err, "")
|
||||||
c.report(err, policy, rule.Name, patched)
|
c.report(err, policy, rule.Name, patched)
|
||||||
|
|
||||||
case engineapi.RuleStatusPass:
|
case engineapi.RuleStatusPass:
|
||||||
patchedNew := patched
|
patchedNew := patched
|
||||||
if patchedNew == nil {
|
if patchedNew == nil {
|
||||||
logger.Error(ErrEmptyPatch, "", "rule", r.Name(), "message", r.Message())
|
logger.Error(ErrEmptyPatch, "", "rule", r.Name(), "message", r.Message())
|
||||||
errs = append(errs, err)
|
errs = append(errs, ErrEmptyPatch)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Status() == engineapi.RuleStatusPass {
|
patchedNew.SetResourceVersion(patched.GetResourceVersion())
|
||||||
patchedNew.SetResourceVersion(patched.GetResourceVersion())
|
var updateErr error
|
||||||
var updateErr error
|
if patchedSubresource == "status" {
|
||||||
if patchedSubresource == "status" {
|
_, updateErr = c.client.UpdateStatusResource(context.TODO(), patchedNew.GetAPIVersion(), patchedNew.GetKind(), patchedNew.GetNamespace(), patchedNew.Object, false)
|
||||||
_, updateErr = c.client.UpdateStatusResource(context.TODO(), patchedNew.GetAPIVersion(), patchedNew.GetKind(), patchedNew.GetNamespace(), patchedNew.Object, false)
|
} else if patchedSubresource != "" {
|
||||||
} else if patchedSubresource != "" {
|
parentResourceGVR := parentGVR
|
||||||
parentResourceGVR := parentGVR
|
parentResourceGV := schema.GroupVersion{Group: parentResourceGVR.Group, Version: parentResourceGVR.Version}
|
||||||
parentResourceGV := schema.GroupVersion{Group: parentResourceGVR.Group, Version: parentResourceGVR.Version}
|
parentResourceGVK, err := c.client.Discovery().GetGVKFromGVR(parentResourceGV.WithResource(parentResourceGVR.Resource))
|
||||||
parentResourceGVK, err := c.client.Discovery().GetGVKFromGVR(parentResourceGV.WithResource(parentResourceGVR.Resource))
|
if err != nil {
|
||||||
if err != nil {
|
logger.Error(err, "failed to get GVK from GVR", "GVR", parentResourceGVR)
|
||||||
logger.Error(err, "failed to get GVK from GVR", "GVR", parentResourceGVR)
|
errs = append(errs, err)
|
||||||
errs = append(errs, err)
|
continue
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, updateErr = c.client.UpdateResource(context.TODO(), parentResourceGV.String(), parentResourceGVK.Kind, patchedNew.GetNamespace(), patchedNew.Object, false, patchedSubresource)
|
|
||||||
} else {
|
|
||||||
_, updateErr = c.client.UpdateResource(context.TODO(), patchedNew.GetAPIVersion(), patchedNew.GetKind(), patchedNew.GetNamespace(), patchedNew.Object, false)
|
|
||||||
}
|
}
|
||||||
if updateErr != nil {
|
_, updateErr = c.client.UpdateResource(context.TODO(), parentResourceGV.String(), parentResourceGVK.Kind, patchedNew.GetNamespace(), patchedNew.Object, false, patchedSubresource)
|
||||||
errs = append(errs, updateErr)
|
} else {
|
||||||
logger.WithName(rule.Name).Error(updateErr, "failed to update target resource", "namespace", patchedNew.GetNamespace(), "name", patchedNew.GetName())
|
_, updateErr = c.client.UpdateResource(context.TODO(), patchedNew.GetAPIVersion(), patchedNew.GetKind(), patchedNew.GetNamespace(), patchedNew.Object, false)
|
||||||
} else {
|
|
||||||
logger.WithName(rule.Name).V(4).Info("successfully mutated existing resource", "namespace", patchedNew.GetNamespace(), "name", patchedNew.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
c.report(updateErr, policy, rule.Name, patched)
|
|
||||||
}
|
}
|
||||||
|
if updateErr != nil {
|
||||||
|
errs = append(errs, updateErr)
|
||||||
|
logger.WithName(rule.Name).Error(updateErr, "failed to update target resource", "namespace", patchedNew.GetNamespace(), "name", patchedNew.GetName())
|
||||||
|
} else {
|
||||||
|
logger.WithName(rule.Name).V(4).Info("successfully mutated existing resource", "namespace", patchedNew.GetNamespace(), "name", patchedNew.GetName())
|
||||||
|
}
|
||||||
|
|
||||||
|
c.report(updateErr, policy, rule.Name, patched)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue