Skip to content

Remove duplicate Go validators covered by CRD schema validation#12150

Open
caseydavenport wants to merge 4 commits intomasterfrom
casey-remove-duplicate-validators
Open

Remove duplicate Go validators covered by CRD schema validation#12150
caseydavenport wants to merge 4 commits intomasterfrom
casey-remove-duplicate-validators

Conversation

@caseydavenport
Copy link
Member

Follow-up to #11973. Removes 22 pure-regex/enum Go struct validators from validator.go that are now enforced by the CRD OpenAPI schema and CEL rules in crd_validation.go. These include action, ipIpMode, vxlanMode, logLevel, bpfLogLevel, datastoreType, iptablesBackend, filterAction, matchOperator, and others.

The go-playground/validator library panics on unknown struct tags, so the removed validators are registered as no-ops to keep things working until the API struct tags are cleaned up separately.

Tests that previously passed sub-objects (Rule, FelixConfigurationSpec, BGPFilterRuleV4, etc.) are updated to pass full CRD resources, which is how the validator is called in production. A few tests that were stricter than the CRD (case-sensitive log levels, case-sensitive action names) are updated to match the CRD's case-insensitive behavior.

createDefaultHostEndpoint is kept as a real validator because the KubeControllersConfiguration CRD lacks an enum constraint on that field.

Add a new cel validator package that compiles x-kubernetes-validations
rules from embedded CRD schemas into Go-executable CEL programs at init
time, using the K8s apiextensions-apiserver CEL libraries.

Wire it into the existing v3 Validate() function so CEL rules are
enforced regardless of datastore backend. In Kubernetes mode the API
server already enforces these via CRDs, but in etcd mode there's no API
server — so without this, CEL validations were simply not checked.

Both Go struct validation errors and CEL errors are now collected and
returned together in a single ErrorValidation.
- Move CEL validation from separate cel package into validator/v3 as
  crd_validation.go
- Add OpenAPI schema validation alongside CEL (enforces enum, minItems,
  maxLength, pattern, etc. from CRD schemas)
- Find storage version instead of assuming Versions[0]
- Infer Kind from Go type when TypeMeta isn't set
- Use logrus directly instead of aliasing as log
- Consolidate var block, use init() instead of sync.Once
- Replace custom contains helpers with strings.Contains
- Restructure tests as per-Kind top-level tests with shared crdTestCase
  runner
Remove 22 pure-regex/enum validators from validator.go that are now
enforced by the CRD OpenAPI schema and CEL rules in crd_validation.go.
These include action, ipIpMode, vxlanMode, logLevel, bpfLogLevel,
datastoreType, iptablesBackend, filterAction, matchOperator, and others.

The remaining no-op registrations prevent go-playground/validator from
panicking on struct tags that reference removed validators.

Tests that previously passed sub-objects (Rule, FelixConfigurationSpec,
etc.) are updated to pass full CRD resources, matching how the validator
is actually called in production. A few tests that were stricter than
the CRD (case-sensitive log levels, case-sensitive action names) are
updated to match the CRD's case-insensitive behavior, which is what the
API server enforces.

createDefaultHostEndpoint is kept as a real validator because the
KubeControllersConfiguration CRD lacks an enum constraint on that field.
@caseydavenport caseydavenport requested a review from a team as a code owner March 16, 2026 20:46
@caseydavenport caseydavenport added docs-not-required Docs not required for this change release-note-not-required Change has no user-facing impact labels Mar 16, 2026
Base automatically changed from casey-go-cel to master March 17, 2026 15:52
@marvin-tigera marvin-tigera added this to the Calico v3.32.0 milestone Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-not-required Docs not required for this change release-note-not-required Change has no user-facing impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants