Skip to content

Commit

Permalink
feat: update webhook validator function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
rriski committed Jan 24, 2024
1 parent 05c25e4 commit 8f09ce8
Show file tree
Hide file tree
Showing 19 changed files with 323 additions and 281 deletions.
34 changes: 18 additions & 16 deletions api/v1alpha1/cassandra_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package v1alpha1

import (
"context"
"errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -30,41 +32,41 @@ func (in *Cassandra) Default() {

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-cassandra,mutating=false,failurePolicy=fail,groups=aiven.io,resources=cassandras,versions=v1alpha1,name=vcassandra.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Validator = &Cassandra{}
var _ webhook.CustomValidator = &Cassandra{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *Cassandra) ValidateCreate() error {
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Cassandra) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
cassandralog.Info("validate create", "name", in.Name)

return in.Spec.Validate()
return nil, in.Spec.Validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *Cassandra) ValidateUpdate(old runtime.Object) error {
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Cassandra) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
cassandralog.Info("validate update", "name", in.Name)

if in.Spec.Project != old.(*Cassandra).Spec.Project {
return errors.New("cannot update a Cassandra service, project field is immutable and cannot be updated")
if in.Spec.Project != oldObj.(*Cassandra).Spec.Project {
return nil, errors.New("cannot update a Cassandra service, project field is immutable and cannot be updated")
}

if in.Spec.ConnInfoSecretTarget.Name != old.(*Cassandra).Spec.ConnInfoSecretTarget.Name {
return errors.New("cannot update a Cassandra service, connInfoSecretTarget.name field is immutable and cannot be updated")
if in.Spec.ConnInfoSecretTarget.Name != oldObj.(*Cassandra).Spec.ConnInfoSecretTarget.Name {
return nil, errors.New("cannot update a Cassandra service, connInfoSecretTarget.name field is immutable and cannot be updated")
}

return in.Spec.Validate()
return nil, in.Spec.Validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *Cassandra) ValidateDelete() error {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Cassandra) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
cassandralog.Info("validate delete", "name", in.Name)

if in.Spec.TerminationProtection != nil && *in.Spec.TerminationProtection {
return errors.New("cannot delete Cassandra service, termination protection is on")
return nil, errors.New("cannot delete Cassandra service, termination protection is on")
}

if in.Spec.ProjectVPCID != "" && in.Spec.ProjectVPCRef != nil {
return errors.New("cannot use both projectVpcId and projectVPCRef")
return nil, errors.New("cannot use both projectVpcId and projectVPCRef")
}

return nil
return nil, nil
}
34 changes: 18 additions & 16 deletions api/v1alpha1/clickhouse_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package v1alpha1

import (
"context"
"errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -31,41 +33,41 @@ func (in *Clickhouse) Default() {

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-clickhouse,mutating=false,failurePolicy=fail,groups=aiven.io,resources=clickhouses,versions=v1alpha1,name=vclickhouse.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Validator = &Clickhouse{}
var _ webhook.CustomValidator = &Clickhouse{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *Clickhouse) ValidateCreate() error {
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Clickhouse) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
clickhouselog.Info("validate create", "name", in.Name)

return in.Spec.Validate()
return nil, in.Spec.Validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *Clickhouse) ValidateUpdate(old runtime.Object) error {
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Clickhouse) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
clickhouselog.Info("validate update", "name", in.Name)

if in.Spec.Project != old.(*Clickhouse).Spec.Project {
return errors.New("cannot update a Clickhouse service, project field is immutable and cannot be updated")
if in.Spec.Project != oldObj.(*Clickhouse).Spec.Project {
return nil, errors.New("cannot update a Clickhouse service, project field is immutable and cannot be updated")
}

if in.Spec.ConnInfoSecretTarget.Name != old.(*Clickhouse).Spec.ConnInfoSecretTarget.Name {
return errors.New("cannot update a Clickhouse service, connInfoSecretTarget.name field is immutable and cannot be updated")
if in.Spec.ConnInfoSecretTarget.Name != oldObj.(*Clickhouse).Spec.ConnInfoSecretTarget.Name {
return nil, errors.New("cannot update a Clickhouse service, connInfoSecretTarget.name field is immutable and cannot be updated")
}

return in.Spec.Validate()
return nil, in.Spec.Validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *Clickhouse) ValidateDelete() error {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Clickhouse) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
clickhouselog.Info("validate delete", "name", in.Name)

if in.Spec.TerminationProtection != nil && *in.Spec.TerminationProtection {
return errors.New("cannot delete Clickhouse service, termination protection is on")
return nil, errors.New("cannot delete Clickhouse service, termination protection is on")
}

if in.Spec.ProjectVPCID != "" && in.Spec.ProjectVPCRef != nil {
return errors.New("cannot use both projectVpcId and projectVPCRef")
return nil, errors.New("cannot use both projectVpcId and projectVPCRef")
}

return nil
return nil, nil
}
23 changes: 13 additions & 10 deletions api/v1alpha1/clickhouseuser_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package v1alpha1

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -29,24 +32,24 @@ func (in *ClickhouseUser) Default() {

//+kubebuilder:webhook:path=/validate-aiven-io-v1alpha1-clickhouseuser,mutating=false,failurePolicy=fail,sideEffects=None,groups=aiven.io,resources=clickhouseusers,verbs=create;update,versions=v1alpha1,name=vclickhouseuser.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &ClickhouseUser{}
var _ webhook.CustomValidator = &ClickhouseUser{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *ClickhouseUser) ValidateCreate() error {
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *ClickhouseUser) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
clickhouseuserlog.Info("validate create", "name", in.Name)
return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *ClickhouseUser) ValidateUpdate(old runtime.Object) error {
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *ClickhouseUser) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
clickhouseuserlog.Info("validate update", "name", in.Name)

return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *ClickhouseUser) ValidateDelete() error {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (in *ClickhouseUser) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
clickhouseuserlog.Info("validate delete", "name", in.Name)

return nil
return nil, nil
}
34 changes: 18 additions & 16 deletions api/v1alpha1/connectionpool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package v1alpha1

import (
"context"
"errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -35,37 +37,37 @@ func (in *ConnectionPool) Default() {

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-connectionpool,mutating=false,failurePolicy=fail,groups=aiven.io,resources=connectionpools,versions=v1alpha1,name=vconnectionpool.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Validator = &ConnectionPool{}
var _ webhook.CustomValidator = &ConnectionPool{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *ConnectionPool) ValidateCreate() error {
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *ConnectionPool) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
connectionpoollog.Info("validate create", "name", in.Name)

return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *ConnectionPool) ValidateUpdate(old runtime.Object) error {
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *ConnectionPool) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
connectionpoollog.Info("validate update", "name", in.Name)

if in.Spec.Project != old.(*ConnectionPool).Spec.Project {
return errors.New("cannot update a ConnectionPool, project field is immutable and cannot be updated")
if in.Spec.Project != oldObj.(*ConnectionPool).Spec.Project {
return nil, errors.New("cannot update a ConnectionPool, project field is immutable and cannot be updated")
}

if in.Spec.ServiceName != old.(*ConnectionPool).Spec.ServiceName {
return errors.New("cannot update a ConnectionPool, serviceName field is immutable and cannot be updated")
if in.Spec.ServiceName != oldObj.(*ConnectionPool).Spec.ServiceName {
return nil, errors.New("cannot update a ConnectionPool, serviceName field is immutable and cannot be updated")
}

if in.Spec.ConnInfoSecretTarget.Name != old.(*ConnectionPool).Spec.ConnInfoSecretTarget.Name {
return errors.New("cannot update a ConnectionPool, connInfoSecretTarget.name field is immutable and cannot be updated")
if in.Spec.ConnInfoSecretTarget.Name != oldObj.(*ConnectionPool).Spec.ConnInfoSecretTarget.Name {
return nil, errors.New("cannot update a ConnectionPool, connInfoSecretTarget.name field is immutable and cannot be updated")
}

return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *ConnectionPool) ValidateDelete() error {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (in *ConnectionPool) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
connectionpoollog.Info("validate delete", "name", in.Name)

return nil
return nil, nil
}
40 changes: 21 additions & 19 deletions api/v1alpha1/database_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package v1alpha1

import (
"context"
"errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand Down Expand Up @@ -41,44 +43,44 @@ func (in *Database) Default() {

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-database,mutating=false,failurePolicy=fail,groups=aiven.io,resources=databases,versions=v1alpha1,name=vdatabase.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Validator = &Database{}
var _ webhook.CustomValidator = &Database{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *Database) ValidateCreate() error {
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Database) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
databaselog.Info("validate create", "name", in.Name)

return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *Database) ValidateUpdate(old runtime.Object) error {
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Database) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
databaselog.Info("validate update", "name", in.Name)

if in.Spec.Project != old.(*Database).Spec.Project {
return errors.New("cannot update a Database, project field is immutable and cannot be updated")
if in.Spec.Project != oldObj.(*Database).Spec.Project {
return nil, errors.New("cannot update a Database, project field is immutable and cannot be updated")
}

if in.Spec.ServiceName != old.(*Database).Spec.ServiceName {
return errors.New("cannot update a Database, service_name field is immutable and cannot be updated")
if in.Spec.ServiceName != oldObj.(*Database).Spec.ServiceName {
return nil, errors.New("cannot update a Database, service_name field is immutable and cannot be updated")
}

if in.Spec.LcCollate != old.(*Database).Spec.LcCollate {
return errors.New("cannot update a Database, lc_collate field is immutable and cannot be updated")
if in.Spec.LcCollate != oldObj.(*Database).Spec.LcCollate {
return nil, errors.New("cannot update a Database, lc_collate field is immutable and cannot be updated")
}

if in.Spec.LcCtype != old.(*Database).Spec.LcCtype {
return errors.New("cannot update a Database, lc_ctype field is immutable and cannot be updated")
if in.Spec.LcCtype != oldObj.(*Database).Spec.LcCtype {
return nil, errors.New("cannot update a Database, lc_ctype field is immutable and cannot be updated")
}

return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *Database) ValidateDelete() error {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (in *Database) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
databaselog.Info("validate delete", "name", in.Name)

if in.Spec.TerminationProtection != nil && *in.Spec.TerminationProtection {
return errors.New("cannot delete Database, termination protection is on")
return nil, errors.New("cannot delete Database, termination protection is on")
}
return nil
return nil, nil
}
Loading

0 comments on commit 8f09ce8

Please sign in to comment.