Skip to content

Commit

Permalink
Add logic for webhooks in main.go
Browse files Browse the repository at this point in the history
It also fixes:
- Fix admission.Validator value in variable declaration error
- Fix functional tests and pre-commit
- Fix tls.crt no such file or directory
- Use METRICS_PORT to 33080 and HEALTH_PORT to 33081

Signed-off-by: Chandan Kumar <[email protected]>
  • Loading branch information
raukadah committed Dec 6, 2024
1 parent 9174b90 commit 9e88d17
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 206 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ crd-schema-check: manifests
# before deploying with OLM again for other untrappable signals.
SKIP_CERT ?=false
.PHONY: run-with-webhook
run-with-webhook: export METRICS_PORT?=8080
run-with-webhook: export HEALTH_PORT?=8081
run-with-webhook: export METRICS_PORT?=33080
run-with-webhook: export HEALTH_PORT?=33081
run-with-webhook: manifests generate fmt vet ## Run a controller from your host.
/bin/bash hack/clean_local_webhook.sh
/bin/bash hack/run_with_local_webhook.sh
25 changes: 25 additions & 0 deletions api/v1beta1/common_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 ctrl "sigs.k8s.io/controller-runtime"

func (r *Watcher) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
27 changes: 7 additions & 20 deletions api/v1beta1/watcher_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ 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"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

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

func (r *Watcher) 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-watcher,mutating=true,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watchers,verbs=create;update,versions=v1beta1,name=mwatcher.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &Watcher{}
Expand All @@ -42,34 +34,29 @@ var _ webhook.Defaulter = &Watcher{}
func (r *Watcher) Default() {
watcherlog.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-watcher,mutating=false,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watchers,verbs=create;update,versions=v1beta1,name=vwatcher.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &Watcher{}

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

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

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

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

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

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
13 changes: 7 additions & 6 deletions api/v1beta1/watcherapi_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand Down Expand Up @@ -51,25 +52,25 @@ func (r *WatcherAPI) Default() {
var _ webhook.Validator = &WatcherAPI{}

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

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

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

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

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

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
13 changes: 7 additions & 6 deletions api/v1beta1/watcherapplier_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand Down Expand Up @@ -51,25 +52,25 @@ func (r *WatcherApplier) Default() {
var _ webhook.Validator = &WatcherApplier{}

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

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

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

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

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

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
13 changes: 7 additions & 6 deletions api/v1beta1/watcherdecisionengine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand Down Expand Up @@ -51,25 +52,25 @@ func (r *WatcherDecisionEngine) Default() {
var _ webhook.Validator = &WatcherDecisionEngine{}

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

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

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

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

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

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
141 changes: 0 additions & 141 deletions api/v1beta1/webhook_suite_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ patches:
# [WEBHOOK] To enable webhook, uncomment the following section
# the following config is for teaching kustomize how to do kustomization for CRDs.

#configurations:
#- kustomizeconfig.yaml
configurations:
- kustomizeconfig.yaml
4 changes: 2 additions & 2 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ resources:
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
Expand All @@ -34,7 +34,7 @@ patches:

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- path: manager_webhook_patch.yaml
- path: manager_webhook_patch.yaml

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
Expand Down
Loading

0 comments on commit 9e88d17

Please sign in to comment.