Skip to content

Commit

Permalink
refactoring webhook validate funcs signature
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed May 30, 2023
1 parent 86c7dd2 commit cdb91cd
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 114 deletions.
19 changes: 10 additions & 9 deletions pkg/apis/agent/v1alpha1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

ulog "github.com/elastic/cloud-on-k8s/v2/pkg/utils/log"
)
Expand Down Expand Up @@ -45,25 +46,25 @@ func (a *Agent) GetWarnings() []string {

// ValidateCreate is called by the validating webhook to validate the create operation.
// Satisfies the webhook.Validator interface.
func (a *Agent) ValidateCreate() error {
func (a *Agent) ValidateCreate() (admission.Warnings, error) {
validationLog.V(1).Info("Validate create", "name", a.Name)
return a.validate(nil)
}

// ValidateDelete is called by the validating webhook to validate the delete operation.
// Satisfies the webhook.Validator interface.
func (a *Agent) ValidateDelete() error {
func (a *Agent) ValidateDelete() (admission.Warnings, error) {
validationLog.V(1).Info("Validate delete", "name", a.Name)
return nil
return nil, nil
}

// ValidateUpdate is called by the validating webhook to validate the update operation.
// Satisfies the webhook.Validator interface.
func (a *Agent) ValidateUpdate(old runtime.Object) error {
func (a *Agent) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
validationLog.V(1).Info("Validate update", "name", a.Name)
oldObj, ok := old.(*Agent)
if !ok {
return errors.New("cannot cast old object to Agent type")
return nil, errors.New("cannot cast old object to Agent type")
}

return a.validate(oldObj)
Expand All @@ -74,7 +75,7 @@ func (a *Agent) WebhookPath() string {
return webhookPath
}

func (a *Agent) validate(old *Agent) error {
func (a *Agent) validate(old *Agent) (admission.Warnings, error) {
var errors field.ErrorList
if old != nil {
for _, uc := range updateChecks {
Expand All @@ -84,7 +85,7 @@ func (a *Agent) validate(old *Agent) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, a.Name, errors)
return nil, apierrors.NewInvalid(groupKind, a.Name, errors)
}
}

Expand All @@ -95,7 +96,7 @@ func (a *Agent) validate(old *Agent) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, a.Name, errors)
return nil, apierrors.NewInvalid(groupKind, a.Name, errors)
}
return nil
return nil, nil
}
19 changes: 10 additions & 9 deletions pkg/apis/apm/v1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

commonv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/common/v1"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/version"
Expand Down Expand Up @@ -50,25 +51,25 @@ var _ webhook.Validator = &ApmServer{}

// ValidateCreate is called by the validating webhook to validate the create operation.
// Satisfies the webhook.Validator interface.
func (as *ApmServer) ValidateCreate() error {
func (as *ApmServer) ValidateCreate() (admission.Warnings, error) {
validationLog.V(1).Info("Validate create", "name", as.Name)
return as.validate(nil)
}

// ValidateDelete is called by the validating webhook to validate the delete operation.
// Satisfies the webhook.Validator interface.
func (as *ApmServer) ValidateDelete() error {
func (as *ApmServer) ValidateDelete() (admission.Warnings, error) {
validationLog.V(1).Info("Validate delete", "name", as.Name)
return nil
return nil, nil
}

// ValidateUpdate is called by the validating webhook to validate the update operation.
// Satisfies the webhook.Validator interface.
func (as *ApmServer) ValidateUpdate(old runtime.Object) error {
func (as *ApmServer) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
validationLog.V(1).Info("Validate update", "name", as.Name)
oldObj, ok := old.(*ApmServer)
if !ok {
return errors.New("cannot cast old object to ApmServer type")
return nil, errors.New("cannot cast old object to ApmServer type")
}

return as.validate(oldObj)
Expand All @@ -79,7 +80,7 @@ func (as *ApmServer) WebhookPath() string {
return webhookPath
}

func (as *ApmServer) validate(old *ApmServer) error {
func (as *ApmServer) validate(old *ApmServer) (admission.Warnings, error) {
var errors field.ErrorList
if old != nil {
for _, uc := range updateChecks {
Expand All @@ -89,7 +90,7 @@ func (as *ApmServer) validate(old *ApmServer) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, as.Name, errors)
return nil, apierrors.NewInvalid(groupKind, as.Name, errors)
}
}

Expand All @@ -100,9 +101,9 @@ func (as *ApmServer) validate(old *ApmServer) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, as.Name, errors)
return nil, apierrors.NewInvalid(groupKind, as.Name, errors)
}
return nil
return nil, nil
}

func checkNoUnknownFields(as *ApmServer) field.ErrorList {
Expand Down
19 changes: 10 additions & 9 deletions pkg/apis/apm/v1beta1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

commonv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/common/v1"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/version"
Expand Down Expand Up @@ -44,25 +45,25 @@ var _ webhook.Validator = &ApmServer{}

// ValidateCreate is called by the validating webhook to validate the create operation.
// Satisfies the webhook.Validator interface.
func (as *ApmServer) ValidateCreate() error {
func (as *ApmServer) ValidateCreate() (admission.Warnings, error) {
validationLog.V(1).Info("Validate create", "name", as.Name)
return as.validate(nil)
}

// ValidateDelete is called by the validating webhook to validate the delete operation.
// Satisfies the webhook.Validator interface.
func (as *ApmServer) ValidateDelete() error {
func (as *ApmServer) ValidateDelete() (admission.Warnings, error) {
validationLog.V(1).Info("Validate delete", "name", as.Name)
return nil
return nil, nil
}

// ValidateUpdate is called by the validating webhook to validate the update operation.
// Satisfies the webhook.Validator interface.
func (as *ApmServer) ValidateUpdate(old runtime.Object) error {
func (as *ApmServer) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
validationLog.V(1).Info("Validate update", "name", as.Name)
oldObj, ok := old.(*ApmServer)
if !ok {
return errors.New("cannot cast old object to ApmServer type")
return nil, errors.New("cannot cast old object to ApmServer type")
}

return as.validate(oldObj)
Expand All @@ -73,7 +74,7 @@ func (as *ApmServer) WebhookPath() string {
return webhookPath
}

func (as *ApmServer) validate(old *ApmServer) error {
func (as *ApmServer) validate(old *ApmServer) (admission.Warnings, error) {
var errors field.ErrorList
if old != nil {
for _, uc := range updateChecks {
Expand All @@ -83,7 +84,7 @@ func (as *ApmServer) validate(old *ApmServer) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, as.Name, errors)
return nil, apierrors.NewInvalid(groupKind, as.Name, errors)
}
}

Expand All @@ -94,9 +95,9 @@ func (as *ApmServer) validate(old *ApmServer) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, as.Name, errors)
return nil, apierrors.NewInvalid(groupKind, as.Name, errors)
}
return nil
return nil, nil
}

func checkNoUnknownFields(as *ApmServer) field.ErrorList {
Expand Down
19 changes: 10 additions & 9 deletions pkg/apis/beat/v1beta1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

ulog "github.com/elastic/cloud-on-k8s/v2/pkg/utils/log"
)
Expand All @@ -32,25 +33,25 @@ var _ webhook.Validator = &Beat{}

// ValidateCreate is called by the validating webhook to validate the create operation.
// Satisfies the webhook.Validator interface.
func (b *Beat) ValidateCreate() error {
func (b *Beat) ValidateCreate() (admission.Warnings, error) {
validationLog.V(1).Info("Validate create", "name", b.Name)
return b.validate(nil)
}

// ValidateDelete is called by the validating webhook to validate the delete operation.
// Satisfies the webhook.Validator interface.
func (b *Beat) ValidateDelete() error {
func (b *Beat) ValidateDelete() (admission.Warnings, error) {
validationLog.V(1).Info("Validate delete", "name", b.Name)
return nil
return nil, nil
}

// ValidateUpdate is called by the validating webhook to validate the update operation.
// Satisfies the webhook.Validator interface.
func (b *Beat) ValidateUpdate(old runtime.Object) error {
func (b *Beat) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
validationLog.V(1).Info("Validate update", "name", b.Name)
oldObj, ok := old.(*Beat)
if !ok {
return errors.New("cannot cast old object to Beat type")
return nil, errors.New("cannot cast old object to Beat type")
}

return b.validate(oldObj)
Expand All @@ -61,7 +62,7 @@ func (b *Beat) WebhookPath() string {
return webhookPath
}

func (b *Beat) validate(old *Beat) error {
func (b *Beat) validate(old *Beat) (admission.Warnings, error) {
var errors field.ErrorList
if old != nil {
for _, uc := range updateChecks {
Expand All @@ -71,7 +72,7 @@ func (b *Beat) validate(old *Beat) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, b.Name, errors)
return nil, apierrors.NewInvalid(groupKind, b.Name, errors)
}
}

Expand All @@ -82,7 +83,7 @@ func (b *Beat) validate(old *Beat) error {
}

if len(errors) > 0 {
return apierrors.NewInvalid(groupKind, b.Name, errors)
return nil, apierrors.NewInvalid(groupKind, b.Name, errors)
}
return nil
return nil, nil
}
19 changes: 10 additions & 9 deletions pkg/apis/elasticsearch/v1beta1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

ulog "github.com/elastic/cloud-on-k8s/v2/pkg/utils/log"
)
Expand All @@ -29,23 +30,23 @@ var _ webhook.Validator = &Elasticsearch{}

// ValidateCreate is called by the validating webhook to validate the create operation.
// Satisfies the webhook.Validator interface.
func (es *Elasticsearch) ValidateCreate() error {
func (es *Elasticsearch) ValidateCreate() (admission.Warnings, error) {
eslog.V(1).Info("validate create", "name", es.Name)
return es.validateElasticsearch()
}

// ValidateDelete is required to implement webhook.Validator, but we do not actually validate deletes.
func (es *Elasticsearch) ValidateDelete() error {
return nil
func (es *Elasticsearch) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

// ValidateUpdate is called by the validating webhook to validate the update operation.
// Satisfies the webhook.Validator interface.
func (es *Elasticsearch) ValidateUpdate(old runtime.Object) error {
func (es *Elasticsearch) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
eslog.V(1).Info("validate update", "name", es.Name)
oldEs, ok := old.(*Elasticsearch)
if !ok {
return errors.New("cannot cast old object to Elasticsearch type")
return nil, errors.New("cannot cast old object to Elasticsearch type")
}

var errs field.ErrorList
Expand All @@ -55,7 +56,7 @@ func (es *Elasticsearch) ValidateUpdate(old runtime.Object) error {
}
}
if len(errs) > 0 {
return apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "elasticsearch.k8s.elastic.co", Kind: "Elasticsearch"},
es.Name, errs)
}
Expand All @@ -67,13 +68,13 @@ func (es *Elasticsearch) WebhookPath() string {
return webhookPath
}

func (es *Elasticsearch) validateElasticsearch() error {
func (es *Elasticsearch) validateElasticsearch() (admission.Warnings, error) {
if errs := es.check(validations); len(errs) > 0 {
return apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "elasticsearch.k8s.elastic.co", Kind: "Elasticsearch"},
es.Name,
errs,
)
}
return nil
return nil, nil
}
Loading

0 comments on commit cdb91cd

Please sign in to comment.