mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-15 17:51:20 +00:00
15cf69f737
* refactor: improve engine logger management Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * logger Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package internal
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/go-logr/logr"
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
)
|
|
|
|
func LoggerWithPolicyContext(logger logr.Logger, policyContext engineapi.PolicyContext) logr.Logger {
|
|
logger = LoggerWithPolicy(logger, policyContext.Policy())
|
|
logger = LoggerWithResource(logger, "new", policyContext.NewResource())
|
|
logger = LoggerWithResource(logger, "old", policyContext.OldResource())
|
|
return logger
|
|
}
|
|
|
|
func LoggerWithPolicy(logger logr.Logger, policy kyvernov1.PolicyInterface) logr.Logger {
|
|
return logger.WithValues(
|
|
"policy.name", policy.GetName(),
|
|
"policy.namespace", policy.GetNamespace(),
|
|
"policy.apply", policy.GetSpec().GetApplyRules(),
|
|
)
|
|
}
|
|
|
|
func LoggerWithResource(logger logr.Logger, prefix string, resource unstructured.Unstructured) logr.Logger {
|
|
if reflect.DeepEqual(resource, unstructured.Unstructured{}) {
|
|
return logger
|
|
}
|
|
return logger.WithValues(
|
|
prefix+".kind", resource.GetKind(),
|
|
prefix+".namespace", resource.GetNamespace(),
|
|
prefix+".name", resource.GetName(),
|
|
)
|
|
}
|
|
|
|
func LoggerWithRule(logger logr.Logger, rule kyvernov1.Rule) logr.Logger {
|
|
return logger.WithValues("rule.name", rule.Name)
|
|
}
|