Skip to content

feat: support consumer labels from metadata labels#409

Closed
AlinsRan wants to merge 4 commits into
api7:masterfrom
AlinsRan:feat/issue-398-consumer-metadata-labels
Closed

feat: support consumer labels from metadata labels#409
AlinsRan wants to merge 4 commits into
api7: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 and Consumer.metadata.labels as 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

Consumer labels

This change is mainly for propagating CRD labels into data plane resource labels.

Both ApisixConsumer.metadata.labels and Consumer.metadata.labels are propagated to the generated APISIX consumer as custom labels, so users can inject business metadata such as tenant, team, or plan from the Kubernetes CRD into the data plane resource.

If a user-defined label key conflicts with a controller-generated reserved label, the controller-generated value is preserved.

ApisixConsumer CRD example

apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
  namespace: ingress-apisix
  name: alice
  labels:
    team: payments
    plan: gold
spec:
  ingressClassName: apisix
  authParameter:
    keyAuth:
      value:
        key: alice-primary-key

Consumer CRD example

apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
  namespace: ingress-apisix
  name: bob
  labels:
    team: platform
    tenant: internal
spec:
  gatewayRef:
    name: apisix
  credentials:
  - type: key-auth
    name: bob-key
    config:
      key: bob-primary-key

Data plane result example

The generated APISIX consumer will carry the injected labels together with controller-managed labels, for example:

{
  "username": "ingress-apisix_alice",
  "labels": {
    "team": "payments",
    "plan": "gold",
    "k8s/name": "alice",
    "k8s/namespace": "ingress-apisix",
    "k8s/kind": "ApisixConsumer",
    "k8s/controller-name": "apisix.apache.org/apisix-ingress-controller",
    "manager-by": "apisix-ingress-controller",
    "k8s/resource-key": "ApisixConsumer/ingress-apisix/alice"
  }
}

The same merge behavior also applies to Consumer resources, so labels defined on either CRD can be consumed directly on the data plane side.

Testing

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed label handling to preserve metadata labels on APISIX consumers and routes without being overwritten by controller-generated labels.
  • Tests

    • Added comprehensive tests verifying metadata labels are correctly preserved during consumer and route translation.

Review Change Stack

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4802f974-3fbc-4fac-9fb2-093581c091fb

📥 Commits

Reviewing files that changed from the base of the PR and between 59bfa6a and eef2031.

📒 Files selected for processing (2)
  • internal/adc/translator/consumer.go
  • internal/adc/translator/consumer_test.go

📝 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, internal/adc/translator/consumer.go, internal/adc/translator/consumer_test.go
TranslateApisixConsumer and TranslateConsumerV1alpha1 now assign consumer Labels using GenLabelWithObjectLabels instead of GenLabel. Tests verify that metadata labels like team and controller keys (k8s/name, k8s/resource-key) are preserved without being overwritten.
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 PR contains only unit tests, no E2E tests. Per review criteria, E2E completeness is blocking—tests must cover full business flow with real services, not just unit-level label merging logic. Add E2E tests verifying: (1) ApisixConsumer CRD with custom labels deploys to APISIX; (2) labels appear on APISIX consumer; (3) controller labels take precedence on conflicts. Use real Kubernetes/APISIX environment.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: support consumer labels from metadata labels' accurately and concisely describes the main change—enabling ApisixConsumer metadata labels to propagate as consumer labels in APISIX.
Linked Issues check ✅ Passed The PR fulfills all requirements from issue #398 by implementing support for user-defined consumer labels, adding label merge semantics, and ensuring controller-generated labels take precedence on conflicts.
Out of Scope Changes check ✅ Passed All changes are directly related to the linked issue #398 objectives: implementing GenLabelWithObjectLabels, updating label generation in ApisixConsumer/ApisixRoute/Consumer translators, and adding comprehensive test coverage.
Security Check ✅ Passed No vulnerabilities across 7 security categories. PR adds label merging for consumer/route metadata. Labels contain non-sensitive business attributes. No data exposure, DB risks, or secret issues.

✏️ 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",
},
AlinsRan and others added 2 commits May 8, 2026 15:49
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 02:25
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

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AlinsRan
Copy link
Copy Markdown
Contributor Author

Superseded by #410. Closing this fork-based PR so CI can use repository secrets from the same-repo branch workflow.

@AlinsRan AlinsRan closed this May 11, 2026
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.

2 participants