Skip to content

Commit

Permalink
fix: add type casting in ValidateDelete; update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <[email protected]>
  • Loading branch information
fabriziosestito committed Jan 21, 2025
1 parent 2d1e780 commit 68b3c36
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 5 deletions.
9 changes: 8 additions & 1 deletion api/policies/v1/admissionpolicy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func (v *admissionPolicyValidator) ValidateUpdate(_ context.Context, oldObj, new
}

// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (v *admissionPolicyValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
func (v *admissionPolicyValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
admissionPolicy, ok := obj.(*AdmissionPolicy)
if !ok {
return nil, fmt.Errorf("expected an AdmissionPolicy object, got %T", obj)
}

v.logger.Info("Validating AdmissionPolicy delete", "name", admissionPolicy.GetName())

return nil, nil
}
19 changes: 19 additions & 0 deletions api/policies/v1/admissionpolicy_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/require"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"

"github.com/kubewarden/kubewarden-controller/internal/constants"
)
Expand Down Expand Up @@ -178,3 +179,21 @@ func TestAdmissionPolicyValidateUpdateWithErrors(t *testing.T) {
require.Error(t, err)
assert.Empty(t, warnings)
}

func TestAdmissionPolicyValidateDelete(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
policy := NewAdmissionPolicyFactory().Build()

warnings, err := validator.ValidateDelete(context.Background(), policy)
require.NoError(t, err)
assert.Empty(t, warnings)
}

func TestAdmissionPolicyValidateDeleteWithInvalidType(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateDelete(context.Background(), obj)
require.ErrorContains(t, err, "expected an AdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)
}
9 changes: 8 additions & 1 deletion api/policies/v1/admissionpolicygroup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (v *admissionPolicyGroupValidator) ValidateUpdate(_ context.Context, oldObj
}

// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (v *admissionPolicyGroupValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
func (v *admissionPolicyGroupValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
admissionPolicyGroup, ok := obj.(*AdmissionPolicyGroup)
if !ok {
return nil, fmt.Errorf("expected an AdmissionPolicyGroup object, got %T", obj)
}

v.logger.Info("Validating AdmissionPolicyGroup delete", "name", admissionPolicyGroup.GetName())

return nil, nil
}
19 changes: 19 additions & 0 deletions api/policies/v1/admissionpolicygroup_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"

"github.com/kubewarden/kubewarden-controller/internal/constants"
)
Expand Down Expand Up @@ -179,3 +180,21 @@ func TestAdmissionPolicyGroupValidateUpdateWithErrors(t *testing.T) {
require.Error(t, err)
assert.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateDelete(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
policy := NewAdmissionPolicyGroupFactory().Build()

warnings, err := validator.ValidateDelete(context.Background(), policy)
require.NoError(t, err)
assert.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateDeleteWithInvalidType(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateDelete(context.Background(), obj)
require.ErrorContains(t, err, "expected an AdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)
}
9 changes: 8 additions & 1 deletion api/policies/v1/clusteradmissionpolicy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func (v *clusterAdmissionPolicyValidator) ValidateUpdate(_ context.Context, oldO
}

// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (v *clusterAdmissionPolicyValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
func (v *clusterAdmissionPolicyValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
clusterAdmissionPolicy, ok := obj.(*ClusterAdmissionPolicy)
if !ok {
return nil, fmt.Errorf("expected a ClusterAdmissionPolicy object, got %T", obj)
}

v.logger.Info("Validating ClusterAdmissionPolicy delete", "name", clusterAdmissionPolicy.GetName())

return nil, nil
}
19 changes: 19 additions & 0 deletions api/policies/v1/clusteradmissionpolicy_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"

"github.com/kubewarden/kubewarden-controller/internal/constants"
)
Expand Down Expand Up @@ -179,3 +180,21 @@ func TestClusterAdmissionPolicyValidateUpdateWithErrors(t *testing.T) {
require.Error(t, err)
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateDelete(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
policy := NewClusterAdmissionPolicyFactory().Build()

warnings, err := validator.ValidateDelete(context.Background(), policy)
require.NoError(t, err)
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateDeleteWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateDelete(context.Background(), obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)
}
9 changes: 8 additions & 1 deletion api/policies/v1/clusteradmissionpolicygroup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (v *clusterAdmissionPolicyGroupValidator) ValidateUpdate(_ context.Context,
}

// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (v *clusterAdmissionPolicyGroupValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
func (v *clusterAdmissionPolicyGroupValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
clusterAdmissionPolicyGroup, ok := obj.(*ClusterAdmissionPolicyGroup)
if !ok {
return nil, fmt.Errorf("expected a ClusterAdmissionPolicyGroup object, got %T", obj)
}

v.logger.Info("Validating ClusterAdmissionPolicyGroup delete", "name", clusterAdmissionPolicyGroup.GetName())

return nil, nil
}
19 changes: 19 additions & 0 deletions api/policies/v1/clusteradmissionpolicygroup_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"

"github.com/kubewarden/kubewarden-controller/internal/constants"
)
Expand Down Expand Up @@ -179,3 +180,21 @@ func TestClusterAdmissionPolicyGroupValidateUpdateWithErrors(t *testing.T) {
require.Error(t, err)
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyGroupValidateDelete(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
policy := NewClusterAdmissionPolicyGroupFactory().Build()

warnings, err := validator.ValidateDelete(context.Background(), policy)
require.NoError(t, err)
assert.Empty(t, warnings)
}

func TestClusteerAdmissionPolicyGroupValidateDeleteWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateDelete(context.Background(), obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)
}
9 changes: 8 additions & 1 deletion api/policies/v1/policyserver_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ func (v *policyServerValidator) ValidateUpdate(ctx context.Context, _, newObj ru
}

// ValdidaeDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (v *policyServerValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
func (v *policyServerValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
policyServer, ok := obj.(*PolicyServer)
if !ok {
return nil, fmt.Errorf("expected a PolicyServer object, got %T", obj)
}

Check warning on line 127 in api/policies/v1/policyserver_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/policies/v1/policyserver_webhook.go#L123-L127

Added lines #L123 - L127 were not covered by tests

v.logger.Info("Validating PolicyServer delete", "name", policyServer.GetName())

return nil, nil

Check warning on line 131 in api/policies/v1/policyserver_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/policies/v1/policyserver_webhook.go#L129-L131

Added lines #L129 - L131 were not covered by tests
}

Expand Down

0 comments on commit 68b3c36

Please sign in to comment.