|
| 1 | +# Label Selector |
| 2 | + |
| 3 | +`--label-selector` scopes ADC operations to resources with matching labels. Use it when multiple teams, applications, or CI pipelines manage separate parts of the same backend. |
| 4 | + |
| 5 | +Without a selector, `adc sync` reconciles all resources in the command scope. If team A and team B sync separate files to the same backend without partitioning, one team can accidentally delete the other team's resources. A selector prevents that by making each run see only its own labeled resources. |
| 6 | + |
| 7 | +## Basic Usage |
| 8 | + |
| 9 | +```bash |
| 10 | +adc sync -f team-a.yaml --label-selector team=a |
| 11 | +``` |
| 12 | + |
| 13 | +You can provide multiple labels in one flag or repeat the flag: |
| 14 | + |
| 15 | +```bash |
| 16 | +adc sync -f catalog.yaml --label-selector team=catalog,env=prod |
| 17 | +adc sync -f catalog.yaml --label-selector team=catalog --label-selector env=prod |
| 18 | +``` |
| 19 | + |
| 20 | +Matching uses exact string equality. Multiple labels are combined with AND, so a resource must match every key-value pair to be included. |
| 21 | + |
| 22 | +ADC does not support wildcards, OR expressions, or key-exists selectors. There is no `ADC_*` environment variable for label selectors; pass them as command-line flags. |
| 23 | + |
| 24 | +## How Remote Filtering Works |
| 25 | + |
| 26 | +For `dump`, and for the remote side of `diff` and `sync`, ADC filters remote resources by label. |
| 27 | + |
| 28 | +Remote filtering applies to top-level resource collections that carry labels, such as: |
| 29 | + |
| 30 | +- `services` |
| 31 | +- `consumers` |
| 32 | +- `consumer_groups` |
| 33 | +- `ssls` |
| 34 | +- other label-bearing top-level resource collections supported by the backend |
| 35 | + |
| 36 | +Remote filtering does not apply to `global_rules` or `plugin_metadata`. These resources are keyed by plugin name and are treated as global configuration. |
| 37 | + |
| 38 | +Filtering does not independently select nested resources. If a service matches the selector, its routes and stream routes are included with the service. If a consumer group matches the selector, its nested consumers are included with the group. |
| 39 | + |
| 40 | +For API7 Enterprise, ADC passes label filters to the Admin API so the backend can filter matching top-level resources. For Apache APISIX, ADC fetches the remote configuration and applies the same label filtering client-side. |
| 41 | + |
| 42 | +## How Local Label Injection Works |
| 43 | + |
| 44 | +ADC also applies the selector to the local file. It merges the selector labels into each local top-level resource and selected nested resources. For `diff` and `sync`, this happens before ADC compares the local file with the backend. For `validate`, this happens before ADC sends the local resources for backend validation. |
| 45 | + |
| 46 | +For example, this command: |
| 47 | + |
| 48 | +```bash |
| 49 | +adc sync -f catalog.yaml --label-selector team=catalog |
| 50 | +``` |
| 51 | + |
| 52 | +turns a local service like this: |
| 53 | + |
| 54 | +```yaml |
| 55 | +services: |
| 56 | + - name: catalog |
| 57 | + routes: |
| 58 | + - name: list-products |
| 59 | + uris: |
| 60 | + - /products |
| 61 | +``` |
| 62 | +
|
| 63 | +into an in-memory configuration equivalent to: |
| 64 | +
|
| 65 | +```yaml |
| 66 | +services: |
| 67 | + - name: catalog |
| 68 | + labels: |
| 69 | + team: catalog |
| 70 | + routes: |
| 71 | + - name: list-products |
| 72 | + labels: |
| 73 | + team: catalog |
| 74 | + uris: |
| 75 | + - /products |
| 76 | +``` |
| 77 | +
|
| 78 | +If the file already has the same label key, the selector value wins. |
| 79 | +
|
| 80 | +This behavior keeps future runs stable. Resources created under a selector remain visible to that selector, and nested resources do not produce repeated diffs just because the backend copy has labels added by an earlier sync. |
| 81 | +
|
| 82 | +## Share One Backend Across Teams |
| 83 | +
|
| 84 | +Team A and team B can manage separate files against the same backend: |
| 85 | +
|
| 86 | +```bash |
| 87 | +# Team A |
| 88 | +adc sync -f team-a.yaml --label-selector team=a |
| 89 | + |
| 90 | +# Team B |
| 91 | +adc sync -f team-b.yaml --label-selector team=b |
| 92 | +``` |
| 93 | + |
| 94 | +Team A sees and reconciles only resources labeled `team=a`. Team B sees and reconciles only resources labeled `team=b`. |
| 95 | + |
| 96 | +To inspect one partition: |
| 97 | + |
| 98 | +```bash |
| 99 | +adc dump -o team-a.yaml --label-selector team=a |
| 100 | +``` |
| 101 | + |
| 102 | +## Important Limits |
| 103 | + |
| 104 | +- Do not use label selectors to split ownership of routes inside one shared service. A service is the top-level unit for filtering, so its nested routes move with it. |
| 105 | +- Use `--gateway-group` for API7 Enterprise gateway group isolation. Use `--label-selector` when teams share a gateway group or when the backend is Apache APISIX. |
| 106 | +- Be careful with `global_rules` and `plugin_metadata`. They are global and are not filtered by label selector. |
| 107 | + |
| 108 | +## Related |
| 109 | + |
| 110 | +- [Resource IDs](./resource-ids.md) |
| 111 | +- [CLI Command Reference](../reference/cli.md#common-backend-options) |
0 commit comments