Context
The resourcesv2 MongoDB collection stores the resource UID in two places:
metadata.uid — the canonical field from the Go struct (metav1.ObjectMeta.UID)
uid — a top-level denormalized copy, manually injected in database.go Set() method
These always hold the same value. The top-level uid doesn't exist in any Go struct's BSON tags — it's synthetically added at write time.
Current state
All queries against resourcesv2 filter on the top-level uid, and there's a unique index on it.
Where top-level uid is used:
| File |
Method |
Usage |
internal/apiservices/resourcesv2service/database.go:50 |
Set() |
filter: bson.M{"uid": uid} |
internal/apiservices/resourcesv2service/database.go:63 |
Set() |
write: doc["uid"] = uid (injection point) |
internal/apiservices/resourcesv2service/database.go:88 |
Patch() |
filter |
internal/apiservices/resourcesv2service/database.go:267 |
Del() |
filter |
internal/apiservices/resourcesv2service/database.go:335 |
GenerateAggregateQuery() |
$in match |
pkg/acl/aclstore/scopeexpander_mongo.go:31 (ror lib) |
resourceRef struct |
projection reads uid |
internal/databases/mongodb/seeding/seeding.go:52 |
Seeding |
unique index on uid |
Where metadata.uid is used:
| File |
Method |
Usage |
internal/apiservices/resourcesv2service/database.go:295 |
GetHashlistByQuery() |
projects $metadata.uid as uid |
Proposed changes
- Change all filters from
"uid" → "metadata.uid"
- Recreate the unique index on
"metadata.uid" instead of "uid"
- Stop injecting
doc["uid"] in Set()
- Update
scopeexpander_mongo.go projection in the ror lib
- Migration: drop old index + remove top-level
uid field from existing documents
Benefit
Eliminates data duplication and the fragile manual injection pattern. The canonical Kubernetes metadata UID should be the single source of truth.
Context
The
resourcesv2MongoDB collection stores the resource UID in two places:metadata.uid— the canonical field from the Go struct (metav1.ObjectMeta.UID)uid— a top-level denormalized copy, manually injected indatabase.goSet()methodThese always hold the same value. The top-level
uiddoesn't exist in any Go struct's BSON tags — it's synthetically added at write time.Current state
All queries against
resourcesv2filter on the top-leveluid, and there's a unique index on it.Where top-level
uidis used:internal/apiservices/resourcesv2service/database.go:50Set()bson.M{"uid": uid}internal/apiservices/resourcesv2service/database.go:63Set()doc["uid"] = uid(injection point)internal/apiservices/resourcesv2service/database.go:88Patch()internal/apiservices/resourcesv2service/database.go:267Del()internal/apiservices/resourcesv2service/database.go:335GenerateAggregateQuery()$inmatchpkg/acl/aclstore/scopeexpander_mongo.go:31(ror lib)resourceRefstructuidinternal/databases/mongodb/seeding/seeding.go:52uidWhere
metadata.uidis used:internal/apiservices/resourcesv2service/database.go:295GetHashlistByQuery()$metadata.uidasuidProposed changes
"uid"→"metadata.uid""metadata.uid"instead of"uid"doc["uid"]inSet()scopeexpander_mongo.goprojection in the ror libuidfield from existing documentsBenefit
Eliminates data duplication and the fragile manual injection pattern. The canonical Kubernetes metadata UID should be the single source of truth.