Skip to content

Commit

Permalink
Adjust webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
adilGhaffarDev committed May 30, 2023
1 parent e2d88c1 commit 35a6bd4
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 91 deletions.
13 changes: 7 additions & 6 deletions api/v1beta1/metal3cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *Metal3Cluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -40,18 +41,18 @@ func (c *Metal3Cluster) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Cluster) ValidateCreate() error {
return c.validate()
func (c *Metal3Cluster) ValidateCreate() (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Cluster) ValidateUpdate(old runtime.Object) error {
return c.validate()
func (c *Metal3Cluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Cluster) ValidateDelete() error {
return nil
func (c *Metal3Cluster) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (c *Metal3Cluster) validate() error {
Expand Down
12 changes: 8 additions & 4 deletions api/v1beta1/metal3cluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ func TestMetal3ClusterValidation(t *testing.T) {
g := NewWithT(t)

if tt.expectErr {
g.Expect(tt.c.ValidateCreate()).NotTo(Succeed())
g.Expect(tt.c.ValidateUpdate(nil)).NotTo(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).To(HaveOccurred())
_, err = tt.c.ValidateUpdate(nil)
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(tt.c.ValidateCreate()).To(Succeed())
g.Expect(tt.c.ValidateUpdate(nil)).To(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).ToNot(HaveOccurred())
_, err = tt.c.ValidateUpdate(nil)
g.Expect(err).ToNot(HaveOccurred())
}
})
}
Expand Down
19 changes: 10 additions & 9 deletions api/v1beta1/metal3data_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *Metal3Data) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -40,7 +41,7 @@ func (c *Metal3Data) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Data) ValidateCreate() error {
func (c *Metal3Data) ValidateCreate() (admission.Warnings, error) {
allErrs := field.ErrorList{}
if (c.Spec.TemplateReference != "" && c.Name != c.Spec.TemplateReference+"-"+strconv.Itoa(c.Spec.Index)) ||
(c.Spec.TemplateReference == "" && c.Name != c.Spec.Template.Name+"-"+strconv.Itoa(c.Spec.Index)) {
Expand All @@ -64,17 +65,17 @@ func (c *Metal3Data) ValidateCreate() error {
}

if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("Metal3Data").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Metal3Data").GroupKind(), c.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Data) ValidateUpdate(old runtime.Object) error {
func (c *Metal3Data) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
allErrs := field.ErrorList{}
oldMetal3Data, ok := old.(*Metal3Data)
if !ok || oldMetal3Data == nil {
return apierrors.NewInternalError(errors.New("unable to convert existing object"))
return nil, apierrors.NewInternalError(errors.New("unable to convert existing object"))
}

if c.Spec.Index != oldMetal3Data.Spec.Index {
Expand Down Expand Up @@ -140,12 +141,12 @@ func (c *Metal3Data) ValidateUpdate(old runtime.Object) error {
}

if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("Metal3Data").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Metal3Data").GroupKind(), c.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Data) ValidateDelete() error {
return nil
func (c *Metal3Data) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
18 changes: 12 additions & 6 deletions api/v1beta1/metal3data_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,19 @@ func TestMetal3DataCreateValidation(t *testing.T) {
}

if tt.expectErr {
g.Expect(obj.ValidateCreate()).NotTo(Succeed())
_, err := obj.ValidateCreate()
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(obj.ValidateCreate()).To(Succeed())
_, err := obj.ValidateCreate()
g.Expect(err).ToNot(HaveOccurred())
}

obj.Spec.Index = -1
g.Expect(obj.ValidateCreate()).NotTo(Succeed())
_, err := obj.ValidateCreate()
g.Expect(err).To(HaveOccurred())

g.Expect(obj.ValidateDelete()).To(Succeed())
_, err = obj.ValidateDelete()
g.Expect(err).ToNot(HaveOccurred())
})
}
}
Expand Down Expand Up @@ -279,9 +283,11 @@ func TestMetal3DataUpdateValidation(t *testing.T) {
}

if tt.expectErr {
g.Expect(newData.ValidateUpdate(oldData)).NotTo(Succeed())
_, err := newData.ValidateUpdate(oldData)
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(newData.ValidateUpdate(oldData)).To(Succeed())
_, err := newData.ValidateUpdate(oldData)
g.Expect(err).ToNot(HaveOccurred())
}
})
}
Expand Down
19 changes: 10 additions & 9 deletions api/v1beta1/metal3dataclaim_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *Metal3DataClaim) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -38,7 +39,7 @@ func (c *Metal3DataClaim) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3DataClaim) ValidateCreate() error {
func (c *Metal3DataClaim) ValidateCreate() (admission.Warnings, error) {
allErrs := field.ErrorList{}
if c.Spec.Template.Name == "" {
allErrs = append(allErrs,
Expand All @@ -51,17 +52,17 @@ func (c *Metal3DataClaim) ValidateCreate() error {
}

if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("Metal3DataClaim").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Metal3DataClaim").GroupKind(), c.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3DataClaim) ValidateUpdate(old runtime.Object) error {
func (c *Metal3DataClaim) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
allErrs := field.ErrorList{}
oldMetal3DataClaim, ok := old.(*Metal3DataClaim)
if !ok || oldMetal3DataClaim == nil {
return apierrors.NewInternalError(errors.New("unable to convert existing object"))
return nil, apierrors.NewInternalError(errors.New("unable to convert existing object"))
}

if c.Spec.Template.Name != oldMetal3DataClaim.Spec.Template.Name {
Expand Down Expand Up @@ -91,12 +92,12 @@ func (c *Metal3DataClaim) ValidateUpdate(old runtime.Object) error {
}

if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("Metal3DataClaim").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Metal3DataClaim").GroupKind(), c.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3DataClaim) ValidateDelete() error {
return nil
func (c *Metal3DataClaim) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
12 changes: 8 additions & 4 deletions api/v1beta1/metal3dataclaim_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ func TestMetal3DataClaimValidation(t *testing.T) {
g := NewWithT(t)

if tt.expectErr {
g.Expect(tt.c.ValidateCreate()).NotTo(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(tt.c.ValidateCreate()).To(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).ToNot(HaveOccurred())
}
})
}
Expand Down Expand Up @@ -197,9 +199,11 @@ func TestMetal3DataClaimUpdateValidation(t *testing.T) {
}

if tt.expectErr {
g.Expect(newDataClaim.ValidateUpdate(oldDataClaim)).NotTo(Succeed())
_, err := newDataClaim.ValidateUpdate(oldDataClaim)
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(newDataClaim.ValidateUpdate(oldDataClaim)).To(Succeed())
_, err := newDataClaim.ValidateUpdate(oldDataClaim)
g.Expect(err).ToNot(HaveOccurred())
}
})
}
Expand Down
17 changes: 9 additions & 8 deletions api/v1beta1/metal3datatemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *Metal3DataTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -88,16 +89,16 @@ func (c *Metal3DataTemplate) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3DataTemplate) ValidateCreate() error {
return c.validate()
func (c *Metal3DataTemplate) ValidateCreate() (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3DataTemplate) ValidateUpdate(old runtime.Object) error {
func (c *Metal3DataTemplate) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
allErrs := field.ErrorList{}
oldM3dt, ok := old.(*Metal3DataTemplate)
if !ok || oldM3dt == nil {
return apierrors.NewInternalError(errors.New("unable to convert existing object"))
return nil, apierrors.NewInternalError(errors.New("unable to convert existing object"))
}

if !reflect.DeepEqual(c.Spec.MetaData, oldM3dt.Spec.MetaData) {
Expand All @@ -121,14 +122,14 @@ func (c *Metal3DataTemplate) ValidateUpdate(old runtime.Object) error {
}

if len(allErrs) == 0 {
return nil
return nil, nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("Metal3Data").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Metal3Data").GroupKind(), c.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3DataTemplate) ValidateDelete() error {
return nil
func (c *Metal3DataTemplate) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (c *Metal3DataTemplate) validate() error {
Expand Down
15 changes: 10 additions & 5 deletions api/v1beta1/metal3datatemplate_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ func TestMetal3DataTemplateValidation(t *testing.T) {
g := NewWithT(t)

if tt.expectErr {
g.Expect(tt.c.ValidateCreate()).NotTo(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(tt.c.ValidateCreate()).To(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).ToNot(HaveOccurred())
}

g.Expect(tt.c.ValidateDelete()).To(Succeed())
_, err := tt.c.ValidateDelete()
g.Expect(err).ToNot(HaveOccurred())
})
}
}
Expand Down Expand Up @@ -200,9 +203,11 @@ func TestMetal3DataTemplateUpdateValidation(t *testing.T) {
}

if tt.expectErr {
g.Expect(newDT.ValidateUpdate(oldDT)).NotTo(Succeed())
_, err := newDT.ValidateUpdate(oldDT)
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(newDT.ValidateUpdate(oldDT)).To(Succeed())
_, err := newDT.ValidateUpdate(oldDT)
g.Expect(err).ToNot(HaveOccurred())
}
})
}
Expand Down
13 changes: 7 additions & 6 deletions api/v1beta1/metal3machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *Metal3Machine) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -37,18 +38,18 @@ func (c *Metal3Machine) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Machine) ValidateCreate() error {
return c.validate()
func (c *Metal3Machine) ValidateCreate() (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Machine) ValidateUpdate(old runtime.Object) error {
return c.validate()
func (c *Metal3Machine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3Machine) ValidateDelete() error {
return nil
func (c *Metal3Machine) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (c *Metal3Machine) validate() error {
Expand Down
12 changes: 8 additions & 4 deletions api/v1beta1/metal3machine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ func TestMetal3MachineValidation(t *testing.T) {
g := NewWithT(t)

if tt.expectErr {
g.Expect(tt.c.ValidateCreate()).NotTo(Succeed())
g.Expect(tt.c.ValidateUpdate(nil)).NotTo(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).To(HaveOccurred())
_, err = tt.c.ValidateUpdate(nil)
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(tt.c.ValidateCreate()).To(Succeed())
g.Expect(tt.c.ValidateUpdate(nil)).To(Succeed())
_, err := tt.c.ValidateCreate()
g.Expect(err).ToNot(HaveOccurred())
_, err = tt.c.ValidateUpdate(nil)
g.Expect(err).ToNot(HaveOccurred())
}
})
}
Expand Down
13 changes: 7 additions & 6 deletions api/v1beta1/metal3machinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *Metal3MachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -37,18 +38,18 @@ func (c *Metal3MachineTemplate) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3MachineTemplate) ValidateCreate() error {
return c.validate()
func (c *Metal3MachineTemplate) ValidateCreate() (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3MachineTemplate) ValidateUpdate(old runtime.Object) error {
return c.validate()
func (c *Metal3MachineTemplate) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
return nil, c.validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *Metal3MachineTemplate) ValidateDelete() error {
return nil
func (c *Metal3MachineTemplate) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (c *Metal3MachineTemplate) validate() error {
Expand Down
Loading

0 comments on commit 35a6bd4

Please sign in to comment.