-
Notifications
You must be signed in to change notification settings - Fork 110
[b/r] Add singleton webhook for OpenStackBackupConfig #1904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 1 commit into
openstack-k8s-operators:main
from
stuggi:backup_restore_webhook
Apr 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
internal/webhook/backup/v1beta1/openstackbackupconfig_webhook.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| 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 contains webhooks for backup API resources. | ||
| package v1beta1 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
|
||
| backupv1beta1 "github.com/openstack-k8s-operators/openstack-operator/api/backup/v1beta1" | ||
| ) | ||
|
|
||
| var openstackbackupconfiglog = logf.Log.WithName("openstackbackupconfig-resource") | ||
|
|
||
| var backupConfigWebhookClient client.Client | ||
|
|
||
| // SetupOpenStackBackupConfigWebhookWithManager registers the webhook for OpenStackBackupConfig in the manager. | ||
| func SetupOpenStackBackupConfigWebhookWithManager(mgr ctrl.Manager) error { | ||
| if backupConfigWebhookClient == nil { | ||
| backupConfigWebhookClient = mgr.GetClient() | ||
| } | ||
|
|
||
| return ctrl.NewWebhookManagedBy(mgr).For(&backupv1beta1.OpenStackBackupConfig{}). | ||
| WithValidator(&OpenStackBackupConfigCustomValidator{}). | ||
| Complete() | ||
| } | ||
|
|
||
| // +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 | ||
|
|
||
| // OpenStackBackupConfigCustomValidator struct is responsible for validating the OpenStackBackupConfig resource | ||
| // when it is created, updated, or deleted. | ||
| // | ||
| // NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods, | ||
| // as this struct is used only for temporary operations and does not need to be deeply copied. | ||
| type OpenStackBackupConfigCustomValidator struct{} | ||
|
|
||
| var _ webhook.CustomValidator = &OpenStackBackupConfigCustomValidator{} | ||
|
|
||
| // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type OpenStackBackupConfig. | ||
| func (v *OpenStackBackupConfigCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
| backupConfig, ok := obj.(*backupv1beta1.OpenStackBackupConfig) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected an OpenStackBackupConfig object but got %T", obj) | ||
| } | ||
| openstackbackupconfiglog.Info("Validation for OpenStackBackupConfig upon creation", "name", backupConfig.GetName()) | ||
|
|
||
| configList, err := backupv1beta1.GetOpenStackBackupConfigs(ctx, backupConfig.Namespace, backupConfigWebhookClient) | ||
| if err != nil { | ||
| return nil, apierrors.NewForbidden( | ||
| schema.GroupResource{ | ||
| Group: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Group, | ||
| Resource: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Kind, | ||
| }, backupConfig.GetName(), &field.Error{ | ||
| Type: field.ErrorTypeForbidden, | ||
| Detail: err.Error(), | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| if len(configList.Items) >= 1 { | ||
| return nil, apierrors.NewForbidden( | ||
| schema.GroupResource{ | ||
| Group: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Group, | ||
| Resource: backupv1beta1.GroupVersion.WithKind("OpenStackBackupConfig").Kind, | ||
| }, backupConfig.GetName(), &field.Error{ | ||
| Type: field.ErrorTypeForbidden, | ||
| Detail: "Only one OpenStackBackupConfig instance is supported per namespace.", | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type OpenStackBackupConfig. | ||
| func (v *OpenStackBackupConfigCustomValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) { | ||
| backupConfig, ok := newObj.(*backupv1beta1.OpenStackBackupConfig) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected an OpenStackBackupConfig object for the newObj but got %T", newObj) | ||
| } | ||
| openstackbackupconfiglog.Info("Validation for OpenStackBackupConfig upon update", "name", backupConfig.GetName()) | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type OpenStackBackupConfig. | ||
| func (v *OpenStackBackupConfigCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
| backupConfig, ok := obj.(*backupv1beta1.OpenStackBackupConfig) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected an OpenStackBackupConfig object but got %T", obj) | ||
| } | ||
| openstackbackupconfiglog.Info("Validation for OpenStackBackupConfig upon deletion", "name", backupConfig.GetName()) | ||
|
|
||
| return nil, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really important, but is there some other type of general error that would make more sense here instead of
Forbidden? Or is this just the pattern we've used in webhook error handling across all operators?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its the same as we also use in other places, like the osversion https://github.com/openstack-k8s-operators/openstack-operator/blob/main/api/core/v1beta1/openstackversion_webhook.go#L97 . maybe NewConflict is better. maybe we follow up to make them all the same.