Skip to content

Fix usage of deprecated webhook.Validator interface #250

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/kind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
go-version: stable

- name: Download Unit Coverage Result
uses: actions/download-artifact@v4
Expand Down
42 changes: 29 additions & 13 deletions api/v1/policy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package v1

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -29,46 +30,61 @@ var (
func (r *Policy) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithValidator(&PolicyCustomValidator{}).
Complete()
}

//+kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1
// +kubebuilder:object:generate=false
type PolicyCustomValidator struct{}

var _ webhook.Validator = &Policy{}
var _ webhook.CustomValidator = &PolicyCustomValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Policy) ValidateCreate() (admission.Warnings, error) {
log := policylog.WithValues("policyName", r.Name, "policyNamespace", r.Namespace)
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (r *PolicyCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
policy, ok := obj.(*Policy)
if !ok {
return nil, fmt.Errorf("expected a Policy object but got %T", obj)
}

log := policylog.WithValues("policyName", policy.Name, "policyNamespace", policy.Namespace)
log.V(1).Info("Validate policy creation request")

err := r.validateName()
err := policy.validateName()
if err != nil {
return nil, err
}

err = r.validateRemediationAction()
err = policy.validateRemediationAction()
if err != nil {
return nil, err
}

return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Policy) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
log := policylog.WithValues("policyName", r.Name, "policyNamespace", r.Namespace)
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (r *PolicyCustomValidator) ValidateUpdate(
_ context.Context, _, newObj runtime.Object,
) (admission.Warnings, error) {
policy, ok := newObj.(*Policy)
if !ok {
return nil, fmt.Errorf("expected a Policy object but got %T", newObj)
}

log := policylog.WithValues("policyName", policy.Name, "policyNamespace", policy.Namespace)
log.V(1).Info("Validate policy update request")

err := r.validateRemediationAction()
err := policy.validateRemediationAction()
if err != nil {
return nil, err
}

return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Policy) ValidateDelete() (admission.Warnings, error) {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (r *PolicyCustomValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}

Expand Down