Skip to content

Remove denormalized top-level uid field from resourcesv2 collection #302

Description

@havardelnan

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

  1. Change all filters from "uid""metadata.uid"
  2. Recreate the unique index on "metadata.uid" instead of "uid"
  3. Stop injecting doc["uid"] in Set()
  4. Update scopeexpander_mongo.go projection in the ror lib
  5. 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.

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions