Skip to content

feat: support consumer labels from metadata labels#409

Open
AlinsRan wants to merge 2 commits intoapi7:masterfrom
AlinsRan:feat/issue-398-consumer-metadata-labels
Open

feat: support consumer labels from metadata labels#409
AlinsRan wants to merge 2 commits intoapi7:masterfrom
AlinsRan:feat/issue-398-consumer-metadata-labels

Conversation

@AlinsRan
Copy link
Copy Markdown
Contributor

@AlinsRan AlinsRan commented May 8, 2026

Summary

  • reuse ApisixConsumer.metadata.labels as APISIX consumer labels
  • add a shared label helper so controller-generated labels always win on conflicts
  • align ApisixRoute with the same merge behavior and add coverage

Closes #398

Testing

  • go test ./internal/controller/label ./internal/adc/translator
  • go test $(go list ./... | grep -v /e2e | grep -v /conformance)

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed label handling for APISIX consumers and routes to preserve custom metadata labels without overwriting controller-managed labels
  • Tests

    • Added comprehensive test coverage for label preservation in consumers and routes

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 8, 2026 07:13
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 8, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds support for custom labels on APISIX consumers and routes. A new GenLabelWithObjectLabels function merges object metadata labels with controller-generated labels, giving controller labels precedence on key collision. The consumer and route translators are updated to use this function instead of GenLabel alone, and corresponding tests verify label preservation.

Changes

Label Preservation Enhancement

Layer / File(s) Summary
Label Merge Utility
internal/controller/label/label.go, internal/controller/label/label_test.go
New GenLabelWithObjectLabels function merges object metadata labels with controller-generated labels from GenLabel, with controller labels taking precedence on collision. Test verifies merging includes custom metadata, generated keys, and managed-by values.
Consumer Translation
internal/adc/translator/apisixconsumer.go, internal/adc/translator/apisixconsumer_test.go
TranslateApisixConsumer now assigns consumer.Labels using GenLabelWithObjectLabels instead of GenLabel. Test verifies that metadata labels like team and controller keys (k8s/name, k8s/resource-key) are preserved without overwriting.
Route Translation
internal/adc/translator/apisixroute.go, internal/adc/translator/apisixroute_test.go
buildRoute updated to use GenLabelWithObjectLabels for label assignment; previous inline label-merging loop removed. Test constructs a route with custom metadata labels and verifies buildRoute preserves both custom and derived labels.

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
E2e Test Quality Review ⚠️ Warning E2E tests missing. Per custom check requirement: unit tests alone insufficient. PR provides unit tests only (no K8s cluster, no APISIX API calls, no integration). Add E2E tests covering full flow: ApisixConsumer with metadata labels → reconcile → verify APISIX consumer labels via API. Add unit tests for edge cases: empty labels, nil maps, large label sets.
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding support for consumer labels from metadata labels, which is the core objective of this PR.
Linked Issues check ✅ Passed The PR successfully implements all objectives from issue #398: it enables user-defined consumer labels via metadata labels, adds a shared label helper with controller-generated label precedence on conflicts, and includes comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly scoped to issue #398: the new GenLabelWithObjectLabels helper, updates to ApisixConsumer and ApisixRoute label generation, and corresponding test coverage are all aligned with the stated objectives.
Security Check ✅ Passed No security vulnerabilities found. Controller-generated labels override user-provided ones. Kubernetes validates label constraints.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for propagating Kubernetes metadata.labels into translated APISIX resources (Consumers and Routes) while ensuring controller-generated labels always take precedence on key conflicts. This enables user-defined labeling use cases (e.g., APISIX consumer label workflows) without breaking the controller’s label-based identification.

Changes:

  • Introduce label.GenLabelWithObjectLabels to merge object metadata labels with controller labels (controller labels win on conflicts).
  • Use the new helper when translating ApisixConsumer labels and ApisixRoute route labels (replacing the previous overwrite-prone merge).
  • Add unit tests covering merge behavior and precedence guarantees.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/controller/label/label.go Adds helper to merge object labels with controller-generated labels (controller wins).
internal/controller/label/label_test.go Adds unit test verifying merge behavior and conflict precedence.
internal/adc/translator/apisixroute.go Switches route label generation to the shared merge helper; removes manual merge logic.
internal/adc/translator/apisixroute_test.go Adds coverage ensuring metadata labels don’t override controller labels for routes.
internal/adc/translator/apisixconsumer.go Switches consumer label generation to include metadata labels while preserving controller precedence.
internal/adc/translator/apisixconsumer_test.go Adds coverage ensuring consumer metadata labels are included without overriding controller labels.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +97 to +101
Labels: map[string]string{
"team": "payments",
"k8s/name": "user-value",
"k8s/resource-key": "user-resource-key",
},
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enhancement: support consumer custom labels in ApisixConsumer

2 participants