kubewarden-controller
kubewarden-controller copied to clipboard
Refactor policy validation on create and update, including tests
We need to refactor and cleanup webhooks validation, as we realized that we could increase readability and test coverage
Acceptance criteria
- The file
api/policies/v1/policy_validation_matchconditions.go
was copy-pasted from k8s.io/kubernetes (see license, copyright, comment at its beginning). We tried to import as-is, but it makes more sense to own it instead. For that:- Enable linter in the file (check
golangci-lint.yml
). - Consider moving its functions into
api/policies/v1/policy_validation.go
and perform any cleanup necessary.
- Enable linter in the file (check
- Factories should be refactored with the builder pattern.
- Add integration tests for the webhooks methods:
ValidateCreate
,ValidateDelete
, andValidateUpdate
.
ATM We test each validator in isolation (seepolicy_validation_test.go
), however, we do not test that the validators are correctly integrated into the webhooks. We could for instance add just a couple of test cases: one with a valid resource and one with a resource that all fields in an invalid state, so that we are sure we are passing through the right validators.