2023-02-06 06:49:47 +01:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2023-10-09 10:27:25 +03:00
|
|
|
kyvernov2beta1 "github.com/kyverno/kyverno/api/kyverno/v2beta1"
|
2023-02-06 06:49:47 +01:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
)
|
|
|
|
|
2023-11-13 17:43:25 +02:00
|
|
|
// GetPolicyExceptions get all exceptions that match both the policy and the rule.
|
|
|
|
func (e *engine) GetPolicyExceptions(
|
2023-02-06 06:49:47 +01:00
|
|
|
policy kyvernov1.PolicyInterface,
|
|
|
|
rule string,
|
2023-11-13 17:43:25 +02:00
|
|
|
) ([]kyvernov2beta1.PolicyException, error) {
|
|
|
|
var exceptions []kyvernov2beta1.PolicyException
|
|
|
|
if e.exceptionSelector == nil {
|
|
|
|
return exceptions, nil
|
2023-02-06 06:49:47 +01:00
|
|
|
}
|
2023-11-13 17:43:25 +02:00
|
|
|
polexs, err := e.exceptionSelector.List(labels.Everything())
|
2023-02-06 06:49:47 +01:00
|
|
|
if err != nil {
|
2023-11-13 17:43:25 +02:00
|
|
|
return exceptions, err
|
2023-02-06 06:49:47 +01:00
|
|
|
}
|
|
|
|
policyName, err := cache.MetaNamespaceKeyFunc(policy)
|
|
|
|
if err != nil {
|
2023-11-13 17:43:25 +02:00
|
|
|
return exceptions, fmt.Errorf("failed to compute policy key: %w", err)
|
2023-02-06 06:49:47 +01:00
|
|
|
}
|
|
|
|
for _, polex := range polexs {
|
|
|
|
if polex.Contains(policyName, rule) {
|
2023-11-13 17:43:25 +02:00
|
|
|
exceptions = append(exceptions, *polex)
|
2023-02-06 06:49:47 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 17:43:25 +02:00
|
|
|
return exceptions, nil
|
2023-02-06 06:49:47 +01:00
|
|
|
}
|