|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package v1beta1 contains webhooks for backup API resources. |
| 18 | +package v1beta1 |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 27 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 33 | + |
| 34 | + backupv1beta1 "github.com/openstack-k8s-operators/openstack-operator/api/backup/v1beta1" |
| 35 | +) |
| 36 | + |
| 37 | +var openstackbackupconfiglog = logf.Log.WithName("openstackbackupconfig-resource") |
| 38 | + |
| 39 | +var backupConfigWebhookClient client.Client |
| 40 | + |
| 41 | +// SetupOpenStackBackupConfigWebhookWithManager registers the webhook for OpenStackBackupConfig in the manager. |
| 42 | +func SetupOpenStackBackupConfigWebhookWithManager(mgr ctrl.Manager) error { |
| 43 | + if backupConfigWebhookClient == nil { |
| 44 | + backupConfigWebhookClient = mgr.GetClient() |
| 45 | + } |
| 46 | + |
| 47 | + return ctrl.NewWebhookManagedBy(mgr).For(&backupv1beta1.OpenStackBackupConfig{}). |
| 48 | + WithValidator(&OpenStackBackupConfigCustomValidator{}). |
| 49 | + Complete() |
| 50 | +} |
| 51 | + |
| 52 | +// +kubebuilder:webhook:path=/validate-backup-openstack-org-v1beta1-openstackbackupconfig,mutating=false,failurePolicy=fail,sideEffects=None,groups=backup.openstack.org,resources=openstackbackupconfigs,verbs=create;update,versions=v1beta1,name=vopenstackbackupconfig-v1beta1.kb.io,admissionReviewVersions=v1 |
| 53 | + |
| 54 | +// OpenStackBackupConfigCustomValidator struct is responsible for validating the OpenStackBackupConfig resource |
| 55 | +// when it is created, updated, or deleted. |
| 56 | +// |
| 57 | +// NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods, |
| 58 | +// as this struct is used only for temporary operations and does not need to be deeply copied. |
| 59 | +type OpenStackBackupConfigCustomValidator struct{} |
| 60 | + |
| 61 | +var _ webhook.CustomValidator = &OpenStackBackupConfigCustomValidator{} |
| 62 | + |
| 63 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type OpenStackBackupConfig. |
| 64 | +func (v *OpenStackBackupConfigCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 65 | + backupConfig, ok := obj.(*backupv1beta1.OpenStackBackupConfig) |
| 66 | + if !ok { |
| 67 | + return nil, fmt.Errorf("expected an OpenStackBackupConfig object but got %T", obj) |
| 68 | + } |
| 69 | + openstackbackupconfiglog.Info("Validation for OpenStackBackupConfig upon creation", "name", backupConfig.GetName()) |
| 70 | + |
| 71 | + configList, err := backupv1beta1.GetOpenStackBackupConfigs(ctx, backupConfig.Namespace, backupConfigWebhookClient) |
| 72 | + if err != nil { |
| 73 | + return nil, apierrors.NewForbidden( |
| 74 | + schema.GroupResource{ |
| 75 | + Group: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Group, |
| 76 | + Resource: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Kind, |
| 77 | + }, backupConfig.GetName(), &field.Error{ |
| 78 | + Type: field.ErrorTypeForbidden, |
| 79 | + Detail: err.Error(), |
| 80 | + }, |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + if len(configList.Items) >= 1 { |
| 85 | + return nil, apierrors.NewForbidden( |
| 86 | + schema.GroupResource{ |
| 87 | + Group: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Group, |
| 88 | + Resource: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Kind, |
| 89 | + }, backupConfig.GetName(), &field.Error{ |
| 90 | + Type: field.ErrorTypeForbidden, |
| 91 | + Detail: "Only one OpenStackBackupConfig instance is supported per namespace.", |
| 92 | + }, |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + return nil, nil |
| 97 | +} |
| 98 | + |
| 99 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type OpenStackBackupConfig. |
| 100 | +func (v *OpenStackBackupConfigCustomValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) { |
| 101 | + backupConfig, ok := newObj.(*backupv1beta1.OpenStackBackupConfig) |
| 102 | + if !ok { |
| 103 | + return nil, fmt.Errorf("expected an OpenStackBackupConfig object for the newObj but got %T", newObj) |
| 104 | + } |
| 105 | + openstackbackupconfiglog.Info("Validation for OpenStackBackupConfig upon update", "name", backupConfig.GetName()) |
| 106 | + |
| 107 | + return nil, nil |
| 108 | +} |
| 109 | + |
| 110 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type OpenStackBackupConfig. |
| 111 | +func (v *OpenStackBackupConfigCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 112 | + backupConfig, ok := obj.(*backupv1beta1.OpenStackBackupConfig) |
| 113 | + if !ok { |
| 114 | + return nil, fmt.Errorf("expected an OpenStackBackupConfig object but got %T", obj) |
| 115 | + } |
| 116 | + openstackbackupconfiglog.Info("Validation for OpenStackBackupConfig upon deletion", "name", backupConfig.GetName()) |
| 117 | + |
| 118 | + return nil, nil |
| 119 | +} |
0 commit comments