2022-02-17 22:14:39 +00:00
|
|
|
/*
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package v1beta1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-17 06:36:05 +00:00
|
|
|
"errors"
|
2022-02-17 22:14:39 +00:00
|
|
|
"fmt"
|
2024-06-17 06:36:05 +00:00
|
|
|
"regexp"
|
2022-02-17 22:14:39 +00:00
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ admission.CustomValidator = &GenericStoreValidator{}
|
|
|
|
|
|
|
|
const (
|
|
|
|
errInvalidStore = "invalid store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GenericStoreValidator struct{}
|
|
|
|
|
|
|
|
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
|
2023-05-31 07:36:22 +00:00
|
|
|
func (r *GenericStoreValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
|
2022-02-17 22:14:39 +00:00
|
|
|
st, ok := obj.(GenericStore)
|
|
|
|
if !ok {
|
2024-08-26 09:10:58 +00:00
|
|
|
return nil, errors.New(errInvalidStore)
|
2022-02-17 22:14:39 +00:00
|
|
|
}
|
|
|
|
return validateStore(st)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
|
2023-05-31 07:36:22 +00:00
|
|
|
func (r *GenericStoreValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
|
2022-02-17 22:14:39 +00:00
|
|
|
st, ok := newObj.(GenericStore)
|
|
|
|
if !ok {
|
2024-08-26 09:10:58 +00:00
|
|
|
return nil, errors.New(errInvalidStore)
|
2022-02-17 22:14:39 +00:00
|
|
|
}
|
|
|
|
return validateStore(st)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
|
2023-05-31 07:36:22 +00:00
|
|
|
func (r *GenericStoreValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
|
|
|
|
return nil, nil
|
2022-02-17 22:14:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 07:36:22 +00:00
|
|
|
func validateStore(store GenericStore) (admission.Warnings, error) {
|
2024-06-17 06:36:05 +00:00
|
|
|
if err := validateConditions(store); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:14:39 +00:00
|
|
|
provider, err := GetProvider(store)
|
|
|
|
if err != nil {
|
2023-05-31 07:36:22 +00:00
|
|
|
return nil, err
|
2022-02-17 22:14:39 +00:00
|
|
|
}
|
2024-06-17 06:36:05 +00:00
|
|
|
|
2024-01-21 22:34:29 +00:00
|
|
|
return provider.ValidateStore(store)
|
2022-02-17 22:14:39 +00:00
|
|
|
}
|
2024-06-17 06:36:05 +00:00
|
|
|
|
|
|
|
func validateConditions(store GenericStore) error {
|
|
|
|
var errs error
|
|
|
|
for ci, condition := range store.GetSpec().Conditions {
|
|
|
|
for ri, r := range condition.NamespaceRegexes {
|
|
|
|
if _, err := regexp.Compile(r); err != nil {
|
|
|
|
errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|