Skip to content

Commit

Permalink
⚠️ Deprecate admission.Validator and admission.Defaulter
Browse files Browse the repository at this point in the history
`admission.Validator/Defaulter` require to import controller-runtime
into api packages. This is not recommended, as api packages should
themselves not have any dependencies except to other api packages in
order to make importing them easy without dependency issues and
possibility incompatibilities.

This change marks the two as deprecated. We are not going to remove them
yet to give folks some time to migrate off them.
  • Loading branch information
alvaroaleman committed Jan 6, 2024
1 parent 1b80b96 commit f3d83a0
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 62 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{})
}
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 f3d83a0

Please sign in to comment.