2022-12-15 09:34:44 +01:00
|
|
|
package exception
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
kyvernov2alpha1 "github.com/kyverno/kyverno/api/kyverno/v2alpha1"
|
|
|
|
)
|
|
|
|
|
2023-01-23 10:48:54 +01:00
|
|
|
const (
|
|
|
|
namespacesDontMatch = "PolicyException resource namespace must match the defined namespace."
|
|
|
|
disabledPolex = "PolicyException resources would not be processed until it is enabled."
|
|
|
|
)
|
|
|
|
|
|
|
|
type ValidationOptions struct {
|
|
|
|
Enabled bool
|
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
2022-12-15 09:34:44 +01:00
|
|
|
// Validate checks policy exception is valid
|
2023-01-23 10:48:54 +01:00
|
|
|
func Validate(ctx context.Context, logger logr.Logger, polex *kyvernov2alpha1.PolicyException, opts ValidationOptions) ([]string, error) {
|
|
|
|
var warnings []string
|
|
|
|
if !opts.Enabled {
|
|
|
|
warnings = append(warnings, disabledPolex)
|
|
|
|
} else if opts.Namespace != "" && opts.Namespace != polex.Namespace {
|
|
|
|
warnings = append(warnings, namespacesDontMatch)
|
|
|
|
}
|
2022-12-15 09:34:44 +01:00
|
|
|
errs := polex.Validate()
|
2023-01-23 10:48:54 +01:00
|
|
|
return warnings, errs.ToAggregate()
|
2022-12-15 09:34:44 +01:00
|
|
|
}
|