Skip to content

Commit

Permalink
Generate webhook for WatcherApplier
Browse files Browse the repository at this point in the history
Below is the command used to generate the same:
```
operator-sdk create webhook --group watcher --version v1beta1 --kind WatcherApplier --programmatic-validation --defaulting
```

Signed-off-by: Chandan Kumar <[email protected]>
  • Loading branch information
raukadah committed Nov 26, 2024
1 parent d999de6 commit 038503a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ resources:
kind: WatcherApplier
path: github.com/openstack-k8s-operators/watcher-operator/api/v1beta1
version: v1beta1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
version: "3"
75 changes: 75 additions & 0 deletions api/v1beta1/watcherapplier_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2024.
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 (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var watcherapplierlog = logf.Log.WithName("watcherapplier-resource")

func (r *WatcherApplier) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

//+kubebuilder:webhook:path=/mutate-watcher-openstack-org-v1beta1-watcherapplier,mutating=true,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watcherappliers,verbs=create;update,versions=v1beta1,name=mwatcherapplier.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &WatcherApplier{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *WatcherApplier) Default() {
watcherapplierlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-watcher-openstack-org-v1beta1-watcherapplier,mutating=false,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watcherappliers,verbs=create;update,versions=v1beta1,name=vwatcherapplier.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &WatcherApplier{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *WatcherApplier) ValidateCreate() error {
watcherapplierlog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *WatcherApplier) ValidateUpdate(old runtime.Object) error {
watcherapplierlog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *WatcherApplier) ValidateDelete() error {
watcherapplierlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
}
3 changes: 3 additions & 0 deletions api/v1beta1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ var _ = BeforeSuite(func() {
err = (&WatcherDecisionEngine{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&WatcherApplier{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down

0 comments on commit 038503a

Please sign in to comment.