Fix autogen to use fully-qualified GVKs to prevent matching non-Kubernetes resources
Description
This PR fixes the issue where auto-generated rules for Pod controllers match non-Kubernetes CRDs that share the same kind name (e.g., Job).
Fixes #14214
Problem
When defining a Pod policy that triggers auto-generation, the autogen rules use simple kind names like Job, Deployment, etc. This causes them to incorrectly match CRDs with the same kind name, such as compute.databricks.crossplane.io/v1alpha1/Job.
Example error: policy restrict-image-registries/autogen-validate-registries fail: validation error: Unknown image registry. rule autogen-validate-registries failed at path /spec/template/
Solution
Modified the autogen logic to use fully-qualified GVKs (Group/Version/Kind) in the generated match rules:
-
Job→batch/v1/Job -
Deployment→apps/v1/Deployment -
CronJob→batch/v1/CronJob -
DaemonSet→apps/v1/DaemonSet -
StatefulSet→apps/v1/StatefulSet -
ReplicaSet→apps/v1/ReplicaSet -
ReplicationController→v1/ReplicationController
Implementation Details
-
Added
podControllerGVKMap- A mapping from simple kind names to fully-qualified GVKs -
Added
convertKindsToGVK()function - Converts simple kind names to GVKs when generating output rules - Maintained backward compatibility - Internal logic still uses simple kind names for checking and annotation parsing
- Updated tests - Test expectations updated to match the new GVK format
Files Changed
-
pkg/autogen/v1/autogen.go- Added GVK mapping and conversion function -
pkg/autogen/v1/rule.go- Applied GVK conversion when setting kinds in generated rules -
pkg/autogen/v2/autogen.go- Added GVK mapping and conversion function (v2 API) -
pkg/autogen/v1/autogen_test.go- Updated test expectations
Testing
All existing tests pass: go test ./pkg/autogen/v1/... -v go test ./pkg/autogen/v2/... -v
Benefits
-
✅ Autogen rules will only match built-in Kubernetes resources -
✅ No more false positives on CRDs with common kind names -
✅ Users no longer need workarounds with preconditions -
✅ Backward compatible with existing policies -
✅ Aligns with the suggested fix in the issue