Skip to content

Commit

Permalink
Merge pull request #2639 from alvaroaleman/deprecate-validator
Browse files Browse the repository at this point in the history
⚠️ Deprecate admission.Validator and admission.Defaulter
  • Loading branch information
k8s-ci-robot committed Jan 7, 2024
2 parents 5804716 + 1c704b9 commit 5c21730
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 68 deletions.
61 changes: 0 additions & 61 deletions examples/crd/pkg/resource.go
Expand Up @@ -17,14 +17,8 @@ limitations under the License.
package pkg

import (
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// ChaosPodSpec defines the desired state of ChaosPod
Expand Down Expand Up @@ -62,61 +56,6 @@ type ChaosPodList struct {
Items []ChaosPod `json:"items"`
}

// +kubebuilder:webhook:path=/validate-chaosapps-metamagical-io-v1-chaospod,mutating=false,failurePolicy=fail,groups=chaosapps.metamagical.io,resources=chaospods,verbs=create;update,versions=v1,name=vchaospod.kb.io

var _ webhook.Validator = &ChaosPod{}

// ValidateCreate implements webhookutil.validator so a webhook will be registered for the type
func (c *ChaosPod) ValidateCreate() (admission.Warnings, error) {
log.Info("validate create", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
}
return nil, nil
}

// ValidateUpdate implements webhookutil.validator so a webhook will be registered for the type
func (c *ChaosPod) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
log.Info("validate update", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
}

oldC, ok := old.(*ChaosPod)
if !ok {
return nil, fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
}
if c.Spec.NextStop.After(oldC.Spec.NextStop.Add(time.Hour)) {
return nil, fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
}
return nil, nil
}

// ValidateDelete implements webhookutil.validator so a webhook will be registered for the type
func (c *ChaosPod) ValidateDelete() (admission.Warnings, error) {
log.Info("validate delete", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
}
return nil, nil
}

// +kubebuilder:webhook:path=/mutate-chaosapps-metamagical-io-v1-chaospod,mutating=true,failurePolicy=fail,groups=chaosapps.metamagical.io,resources=chaospods,verbs=create;update,versions=v1,name=mchaospod.kb.io

var _ webhook.Defaulter = &ChaosPod{}

// Default implements webhookutil.defaulter so a webhook will be registered for the type
func (c *ChaosPod) Default() {
log.Info("default", "name", c.Name)

if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
c.Spec.NextStop = metav1.Time{Time: time.Now().Add(time.Minute)}
}
}

func init() {
SchemeBuilder.Register(&ChaosPod{}, &ChaosPodList{})
}
6 changes: 0 additions & 6 deletions pkg/builder/example_webhook_test.go
Expand Up @@ -24,16 +24,10 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

examplegroup "sigs.k8s.io/controller-runtime/examples/crd/pkg"
)

// examplegroup.ChaosPod has implemented both admission.Defaulter and
// admission.Validator interfaces.
var _ admission.Defaulter = &examplegroup.ChaosPod{}
var _ admission.Validator = &examplegroup.ChaosPod{}

// This example use webhook builder to create a simple webhook that is managed
// by a manager for CRD ChaosPod. And then start the manager.
func ExampleWebhookBuilder() {
Expand Down
2 changes: 2 additions & 0 deletions pkg/webhook/admission/defaulter.go
Expand Up @@ -27,12 +27,14 @@ import (
)

// Defaulter defines functions for setting defaults on resources.
// Deprecated: Ue CustomDefaulter instead.
type Defaulter interface {
runtime.Object
Default()
}

// DefaultingWebhookFor creates a new Webhook for Defaulting the provided type.
// Deprecated: Use WithCustomDefaulter instead.
func DefaultingWebhookFor(scheme *runtime.Scheme, defaulter Defaulter) *Webhook {
return &Webhook{
Handler: &mutatingHandler{defaulter: defaulter, decoder: NewDecoder(scheme)},
Expand Down
2 changes: 2 additions & 0 deletions pkg/webhook/admission/validator.go
Expand Up @@ -33,6 +33,7 @@ type Warnings []string
// Validator defines functions for validating an operation.
// The custom resource kind which implements this interface can validate itself.
// To validate the custom resource with another specific struct, use CustomValidator instead.
// Deprecated: Use CustomValidator instead.
type Validator interface {
runtime.Object

Expand All @@ -53,6 +54,7 @@ type Validator interface {
}

// ValidatingWebhookFor creates a new Webhook for validating the provided type.
// Deprecated: Use WithCustomValidator instead.
func ValidatingWebhookFor(scheme *runtime.Scheme, validator Validator) *Webhook {
return &Webhook{
Handler: &validatingHandler{validator: validator, decoder: NewDecoder(scheme)},
Expand Down
1 change: 0 additions & 1 deletion pkg/webhook/admission/validator_custom.go
Expand Up @@ -30,7 +30,6 @@ import (
// CustomValidator defines functions for validating an operation.
// The object to be validated is passed into methods as a parameter.
type CustomValidator interface {

// ValidateCreate validates the object on creation.
// The optional warnings will be added to the response as warning messages.
// Return an error if the object is invalid.
Expand Down
2 changes: 2 additions & 0 deletions pkg/webhook/alias.go
Expand Up @@ -24,9 +24,11 @@ import (
// define some aliases for common bits of the webhook functionality

// Defaulter defines functions for setting defaults on resources.
// Deprecated: Use CustomDefaulter instead.
type Defaulter = admission.Defaulter

// Validator defines functions for validating an operation.
// Deprecated: Use CustomValidator instead.
type Validator = admission.Validator

// CustomDefaulter defines functions for setting defaults on resources.
Expand Down

0 comments on commit 5c21730

Please sign in to comment.