1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/utils/exceptions.go
Mariam Fahmy c0e0cea9f4
feat: compute policy exceptions as a part of the rule execution (#8713)
Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
2023-11-13 15:43:25 +00:00

47 lines
1.3 KiB
Go

package utils
import (
"github.com/go-logr/logr"
kyvernov2beta1 "github.com/kyverno/kyverno/api/kyverno/v2beta1"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
"github.com/kyverno/kyverno/pkg/utils/conditions"
matched "github.com/kyverno/kyverno/pkg/utils/match"
)
// MatchesException takes a list of exceptions and checks if there is an exception applies to the incoming resource.
// It returns the matched policy exception.
func MatchesException(
polexs []kyvernov2beta1.PolicyException,
policyContext engineapi.PolicyContext,
logger logr.Logger,
) *kyvernov2beta1.PolicyException {
gvk, subresource := policyContext.ResourceKind()
resource := policyContext.NewResource()
if resource.Object == nil {
resource = policyContext.OldResource()
}
for _, polex := range polexs {
err := matched.CheckMatchesResources(
resource,
polex.Spec.Match,
policyContext.NamespaceLabels(),
policyContext.AdmissionInfo(),
gvk,
subresource,
)
// if there's no error it means a match
if err == nil {
if polex.Spec.Conditions != nil {
passed, err := conditions.CheckAnyAllConditions(logger, policyContext.JSONContext(), *polex.Spec.Conditions)
if err != nil {
return nil
}
if !passed {
return nil
}
}
return &polex
}
}
return nil
}