2022-12-15 09:34:44 +01:00
|
|
|
package exception
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
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."
|
2024-10-22 09:07:11 +03:00
|
|
|
polexNamespaceFlag = "The exceptionNamespace flag is not set"
|
2023-01-23 10:48:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ValidationOptions struct {
|
|
|
|
Enabled bool
|
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
2022-12-15 09:34:44 +01:00
|
|
|
// Validate checks policy exception is valid
|
2025-02-05 17:01:11 +02:00
|
|
|
func ValidateNamespace(ctx context.Context, logger logr.Logger, polexNs string, opts ValidationOptions) []string {
|
2023-01-23 10:48:54 +01:00
|
|
|
var warnings []string
|
|
|
|
if !opts.Enabled {
|
|
|
|
warnings = append(warnings, disabledPolex)
|
2024-10-22 09:07:11 +03:00
|
|
|
} else if opts.Namespace == "" {
|
|
|
|
warnings = append(warnings, polexNamespaceFlag)
|
2025-02-05 17:01:11 +02:00
|
|
|
} else if opts.Namespace != "*" && opts.Namespace != polexNs {
|
2023-01-23 10:48:54 +01:00
|
|
|
warnings = append(warnings, namespacesDontMatch)
|
|
|
|
}
|
2025-02-05 17:01:11 +02:00
|
|
|
return warnings
|
2022-12-15 09:34:44 +01:00
|
|
|
}
|