Skip to content

Commit f3d83a0

Browse files
committed
⚠️ Deprecate admission.Validator and admission.Defaulter
`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.
1 parent 1b80b96 commit f3d83a0

File tree

5 files changed

+6
-62
lines changed

5 files changed

+6
-62
lines changed

examples/crd/pkg/resource.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@ limitations under the License.
1717
package pkg
1818

1919
import (
20-
"fmt"
21-
"time"
22-
2320
corev1 "k8s.io/api/core/v1"
2421
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25-
"k8s.io/apimachinery/pkg/runtime"
26-
"sigs.k8s.io/controller-runtime/pkg/webhook"
27-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2822
)
2923

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

65-
// +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
66-
67-
var _ webhook.Validator = &ChaosPod{}
68-
69-
// ValidateCreate implements webhookutil.validator so a webhook will be registered for the type
70-
func (c *ChaosPod) ValidateCreate() (admission.Warnings, error) {
71-
log.Info("validate create", "name", c.Name)
72-
73-
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
74-
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
75-
}
76-
return nil, nil
77-
}
78-
79-
// ValidateUpdate implements webhookutil.validator so a webhook will be registered for the type
80-
func (c *ChaosPod) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
81-
log.Info("validate update", "name", c.Name)
82-
83-
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
84-
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
85-
}
86-
87-
oldC, ok := old.(*ChaosPod)
88-
if !ok {
89-
return nil, fmt.Errorf("expect old object to be a %T instead of %T", oldC, old)
90-
}
91-
if c.Spec.NextStop.After(oldC.Spec.NextStop.Add(time.Hour)) {
92-
return nil, fmt.Errorf("it is not allowed to delay.spec.nextStop for more than 1 hour")
93-
}
94-
return nil, nil
95-
}
96-
97-
// ValidateDelete implements webhookutil.validator so a webhook will be registered for the type
98-
func (c *ChaosPod) ValidateDelete() (admission.Warnings, error) {
99-
log.Info("validate delete", "name", c.Name)
100-
101-
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
102-
return nil, fmt.Errorf(".spec.nextStop must be later than current time")
103-
}
104-
return nil, nil
105-
}
106-
107-
// +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
108-
109-
var _ webhook.Defaulter = &ChaosPod{}
110-
111-
// Default implements webhookutil.defaulter so a webhook will be registered for the type
112-
func (c *ChaosPod) Default() {
113-
log.Info("default", "name", c.Name)
114-
115-
if c.Spec.NextStop.Before(&metav1.Time{Time: time.Now()}) {
116-
c.Spec.NextStop = metav1.Time{Time: time.Now().Add(time.Minute)}
117-
}
118-
}
119-
12059
func init() {
12160
SchemeBuilder.Register(&ChaosPod{}, &ChaosPodList{})
12261
}

pkg/webhook/admission/defaulter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ import (
2727
)
2828

2929
// Defaulter defines functions for setting defaults on resources.
30+
// Deprecated: Ue CustomDefaulter instead.
3031
type Defaulter interface {
3132
runtime.Object
3233
Default()
3334
}
3435

3536
// DefaultingWebhookFor creates a new Webhook for Defaulting the provided type.
37+
// Deprecated: Use WithCustomDefaulter instead.
3638
func DefaultingWebhookFor(scheme *runtime.Scheme, defaulter Defaulter) *Webhook {
3739
return &Webhook{
3840
Handler: &mutatingHandler{defaulter: defaulter, decoder: NewDecoder(scheme)},

pkg/webhook/admission/validator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Warnings []string
3333
// Validator defines functions for validating an operation.
3434
// The custom resource kind which implements this interface can validate itself.
3535
// To validate the custom resource with another specific struct, use CustomValidator instead.
36+
// Deprecated: Use CustomValidator instead.
3637
type Validator interface {
3738
runtime.Object
3839

@@ -53,6 +54,7 @@ type Validator interface {
5354
}
5455

5556
// ValidatingWebhookFor creates a new Webhook for validating the provided type.
57+
// Deprecated: Use WithCustomValidator instead.
5658
func ValidatingWebhookFor(scheme *runtime.Scheme, validator Validator) *Webhook {
5759
return &Webhook{
5860
Handler: &validatingHandler{validator: validator, decoder: NewDecoder(scheme)},

pkg/webhook/admission/validator_custom.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
// CustomValidator defines functions for validating an operation.
3131
// The object to be validated is passed into methods as a parameter.
3232
type CustomValidator interface {
33-
3433
// ValidateCreate validates the object on creation.
3534
// The optional warnings will be added to the response as warning messages.
3635
// Return an error if the object is invalid.

pkg/webhook/alias.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
// define some aliases for common bits of the webhook functionality
2525

2626
// Defaulter defines functions for setting defaults on resources.
27+
// Deprecated: Use CustomDefaulter instead.
2728
type Defaulter = admission.Defaulter
2829

2930
// Validator defines functions for validating an operation.
31+
// Deprecated: Use CustomValidator instead.
3032
type Validator = admission.Validator
3133

3234
// CustomDefaulter defines functions for setting defaults on resources.

0 commit comments

Comments
 (0)