Skip to content

Add pkgMetadata label select on CRD - #1160

Open
efiacor wants to merge 7 commits into
kptdev:mainfrom
Nordix:pkgmeta_label_selector
Open

Add pkgMetadata label select on CRD#1160
efiacor wants to merge 7 commits into
kptdev:mainfrom
Nordix:pkgmeta_label_selector

Conversation

@efiacor

@efiacor efiacor commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Add PackageMetadata label selector support for v1alpha2 CRD


Description

  • What changed:

    • Implemented label mirroring from spec.packageMetadata.labels to object metadata labels with porch.kpt.dev/kptfile-label__ prefix
    • Fixed metadata sync semantics from merge to replace (omitted keys now deleted)
    • Added label escaping for Kubernetes compatibility (slashes → __)
  • Why it's needed:

    • v1alpha1 supported filtering by spec.packageMetadata.labels[key]=value via field selectors
    • v1alpha2 CRD cannot express bracket notation, so we mirror labels to standard Kubernetes label selectors
    • Users can now query packages by Kptfile labels using kubectl -l porch.kpt.dev/kptfile-label__key=value
  • How it works:

    • After rendering, updateKptfileFields() extracts labels from Kptfile's spec.packageMetadata.labels
    • Labels are mirrored to object metadata.labels with prefix and slash escaping
    • Slashes in label keys (e.g., app.example.com/name) are escaped as __ for Kubernetes compatibility
    • Metadata replacement semantics ensure omitted keys are removed (aligns with v1alpha1)

Related Issue(s)


Type of Change

  • New feature
  • Enhancement
  • Tests
  • Documentation

Checklist

  • Code follows project style guidelines
  • Self-reviewed changes
  • Tests added/updated (3 e2e tests, unit tests for deletion semantics)
  • Documentation added/updated (inspecting-packages.md)
  • All tests and gating checks pass

Testing Instructions

  1. Deploy porch with v1alpha2 enabled
  2. Create a PackageRevision with spec.packageMetadata.labels: {env: prod, tier: backend}
  3. Verify labels are mirrored: kubectl get pr -l porch.kpt.dev/kptfile-label__env=prod
  4. Test slash escaping: create label app.example.com/name and query with porch.kpt.dev/kptfile-label__app.example.com__name

Additional Notes

  • Known issues: None
  • Further improvements: Could extend to annotation filtering if needed in future
  • Review notes: Only labels are mirrored (not annotations) to match v1alpha1 semantics

AI Disclosure

  • I have used AI in the creation of this PR.
    • Kiro was used to implement the label mirroring logic, write unit tests for deletion semantics, and generate comprehensive e2e test coverage

@efiacor efiacor self-assigned this Aug 27, 2026
@efiacor
efiacor requested review from a team August 27, 2026 12:29
@netlify

netlify Bot commented Aug 27, 2026

Copy link
Copy Markdown

Deploy Preview for kpt-porch ready!

Name Link
🔨 Latest commit 021d84e
🔍 Latest deploy log https://app.netlify.com/projects/kpt-porch/deploys/6a99909eb14c8e0008695375
😎 Deploy Preview https://deploy-preview-1160--kpt-porch.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 27, 2026
@github-actions
github-actions Bot requested a lite review from Copilot August 27, 2026 12:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds v1alpha2 support for querying spec.packageMetadata.labels via standard Kubernetes label selectors by mirroring Kptfile labels onto the PackageRevision’s metadata.labels, and updates metadata sync semantics to “replace” so omitted keys are deleted.

Changes:

  • Mirror Kptfile labels to metadata.labels using porch.kpt.dev/kptfile-label__ prefix with / escaping.
  • Switch spec→Kptfile metadata application from merge to replace (omitted keys removed), with new tests for deletion semantics.
  • Add/expand e2e coverage and update docs for label-selector based filtering.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/e2e/crd/metadata_test.go Adds e2e tests for label mirroring, slash-escaping, and deletion propagation behavior.
docs/content/en/docs/4_tutorials_and_how-tos/working_with_package_revisions/inspecting-packages.md Documents querying PackageRevisions via mirrored Kptfile label selectors.
controllers/packagerevisions/pkg/controllers/packagerevision/status.go Implements label mirroring to metadata.labels and adjusts SSA spec apply behavior.
controllers/packagerevisions/pkg/controllers/packagerevision/status_test.go Adds a unit test covering readiness gate removal semantics (needs adjustment for new label patching).
controllers/packagerevisions/pkg/controllers/packagerevision/metadata.go Changes metadata application to replace semantics via applyMetadataMap.
controllers/packagerevisions/pkg/controllers/packagerevision/metadata_test.go Updates unit tests to reflect replace semantics and clearing behavior.
controllers/packagerevisions/pkg/controllers/packagerevision/metadata_deletion_test.go Adds additional tests for deletion/round-trip convergence (header comment needs update).
Suppressed comments (2)

controllers/packagerevisions/pkg/controllers/packagerevision/status.go:289

  • When Kptfile labels are removed and the object currently only has mirrored labels, this function returns updated == nil. Because ObjectMeta.Labels is omitempty, applying with a nil map omits the labels field entirely, so SSA won't prune the previously-applied mirror label keys (they can get “stuck” on the object). Ensure an explicit empty map is applied when mirror labels need to be cleared and no other labels remain.
	if len(kptfileLabels) == 0 {
		// No Kptfile labels; remove any existing mirror labels from current.
		var updated map[string]string
		changed := false
		for k, v := range current {

controllers/packagerevisions/pkg/controllers/packagerevision/status.go:307

  • Mirrored labels are copied verbatim (after only '/' escaping) and unbounded. If a Kptfile label key/value is invalid for Kubernetes labels (length/charset), applyObjectLabels will fail and log repeatedly, and selectors won't work. Also consider capping mirrored entries (the linked issue’s acceptance criteria mentions a cap) to avoid unbounded growth of object metadata.
	// Build desired Kptfile mirror labels: sort for determinism, then apply.
	desired := make(map[string]string)
	keys := slices.Sorted(maps.Keys(kptfileLabels))
	for _, k := range keys {
		// Escape "/" as "__" to fit Kubernetes label key constraints.
		mirrorKey := kptfileLabelPrefix + strings.ReplaceAll(k, "/", "__")

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread controllers/packagerevisions/pkg/controllers/packagerevision/status_test.go Outdated
@efiacor efiacor changed the title Add pkgMetadata lable select on CRD Add pkgMetadata label select on CRD Sep 1, 2026
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
@efiacor
efiacor force-pushed the pkgmeta_label_selector branch from eb59a7c to 3ebd84b Compare September 3, 2026 13:01
@liamfallon
liamfallon requested review from a team September 3, 2026 13:06
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
@sonarqubecloud

sonarqubecloud Bot commented Sep 3, 2026

Copy link
Copy Markdown

Comment thread .vscode/launch.json
Comment on lines +111 to -121
"--webhook-cert-dir=${workspaceFolder}/.build/deploy/.webhook-certs",
"-v=2"
],
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"env": {
"GIT_CACHE_DIR": "${workspaceFolder}/.cache-controller-v1alpha2",
"DB_HOST": "${env:DB_HOST}",
"DB_PORT": "5432",
"DB_NAME": "porch",
"DB_USER": "porch",
"DB_PASSWORD": "porch",
"DB_DRIVER": "pgx",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason were messing with the launch.json here? like removal of DB_HOST? or did that just get pulled in by mistake?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When getting the launch to work for controllers, I decided to move the ${env} ones out and move to use a local .env instead. Means we can each have our own gitignored .env file locally. I think we should do this for all launch targets, as having to set those (DB_HOST, FUNCTION_RUNNER_IP, etc) each time is messy. It's ok having a shared launch.json but ideally it shouldn't be commited to the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PackageMetadata field selectors not available in v1alpha2

4 participants