Skip to content

Commit 3b6274d

Browse files
authored
Merge pull request #45155 from jpbetz/crd-field-selectors
Add docs for CRD field selection
2 parents 302679c + 8c0a57a commit 3b6274d

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

content/en/docs/concepts/extend-kubernetes/api-extension/custom-resources.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,50 @@ When you add a custom resource, you can access it using:
292292
(generating one is an advanced undertaking, but some projects may provide a client along with
293293
the CRD or AA).
294294

295+
296+
## Custom resource field selectors
297+
298+
[Field Selectors](/docs/concepts/overview/working-with-objects/field-selectors/)
299+
let clients select custom resources based on the value of one or more resource
300+
fields.
301+
302+
All custom resources support the `metadata.name` and `metadata.namespace` field
303+
selectors.
304+
305+
Fields declared in a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}}
306+
may also be used with field selectors when included in the `spec.versions[*].selectableFields` field of the
307+
{{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}}.
308+
309+
### Selectable fields for custom resources {#crd-selectable-fields}
310+
311+
{{< feature-state feature_gate_name="CustomResourceFieldSelectors" >}}
312+
313+
You need to enable the `CustomResourceFieldSelectors`
314+
[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) to
315+
use this behavior, which then applies to all CustomResourceDefinitions in your
316+
cluster.
317+
318+
The `spec.versions[*].selectableFields` field of a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} may be used to
319+
declare which other fields in a custom resource may be used in field selectors.
320+
The following example adds the `.spec.color` and `.spec.size` fields as
321+
selectable fields.
322+
323+
{{% code_sample file="customresourcedefinition/shirt-resource-definition.yaml" %}}
324+
325+
Field selectors can then be used to get only resources with with a `color` of `blue`:
326+
327+
```shell
328+
kubectl get shirts.stable.example.com --field-selector spec.color=blue
329+
```
330+
331+
The output should be:
332+
333+
```
334+
NAME COLOR SIZE
335+
example1 blue S
336+
example2 blue M
337+
```
338+
295339
## {{% heading "whatsnext" %}}
296340

297341
* Learn how to [Extend the Kubernetes API with the aggregation layer](/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/).
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: CustomResourceFieldSelectors
3+
content_type: feature_gate
4+
_build:
5+
list: never
6+
render: false
7+
8+
stages:
9+
- stage: alpha
10+
defaultValue: false
11+
fromVersion: "1.30"
12+
---
13+
14+
Enable `selectableFields` in the
15+
{{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} API to allow filtering
16+
of custom resource **list**, **watch** and **deletecollection** requests.

content/en/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,96 @@ my-new-cron-object * * * * * 1 7s
16241624
The `NAME` column is implicit and does not need to be defined in the CustomResourceDefinition.
16251625
{{< /note >}}
16261626

1627+
### Field selectors
1628+
1629+
[Field Selectors](/docs/concepts/overview/working-with-objects/field-selectors/)
1630+
let clients select custom resources based on the value of one or more resource
1631+
fields.
1632+
1633+
All custom resources support the `metadata.name` and `metadata.namespace` field
1634+
selectors.
1635+
1636+
Fields declared in a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}}
1637+
may also be used with field selectors when included in the `spec.versions[*].selectableFields` field of the
1638+
{{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}}.
1639+
1640+
#### Selectable fields for custom resources {#crd-selectable-fields}
1641+
1642+
{{< feature-state state="alpha" for_k8s_version="v1.30" >}}
1643+
{{< feature-state feature_gate_name="CustomResourceFieldSelectors" >}}
1644+
1645+
You need to enable the `CustomResourceFieldSelectors`
1646+
[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) to
1647+
use this behavior, which then applies to all CustomResourceDefinitions in your
1648+
cluster.
1649+
1650+
The `spec.versions[*].selectableFields` field of a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} may be used to
1651+
declare which other fields in a custom resource may be used in field selectors.
1652+
The following example adds the `.spec.color` and `.spec.size` fields as
1653+
selectable fields.
1654+
1655+
Save the CustomResourceDefinition to `shirt-resource-definition.yaml`:
1656+
1657+
{{% code_sample file="customresourcedefinition/shirt-resource-definition.yaml" %}}
1658+
1659+
Create the CustomResourceDefinition:
1660+
1661+
```shell
1662+
kubectl apply -f https://k8s.io/examples/customresourcedefinition/shirt-resource-definition.yaml
1663+
```
1664+
1665+
Define some Shirts by editing `shirt-resources.yaml`; for example:
1666+
1667+
{{% code_sample file="customresourcedefinition/shirt-resources.yaml" %}}
1668+
1669+
Create the custom resources:
1670+
1671+
```shell
1672+
kubectl apply -f https://k8s.io/examples/customresourcedefinition/shirt-resources.yaml
1673+
```
1674+
1675+
Get all the resources:
1676+
1677+
```shell
1678+
kubectl get shirts.stable.example.com
1679+
```
1680+
1681+
The output is:
1682+
1683+
```
1684+
NAME COLOR SIZE
1685+
example1 blue S
1686+
example2 blue M
1687+
example3 green M
1688+
```
1689+
1690+
Fetch blue shirts (retrieve Shirts with a `color` of `blue`):
1691+
1692+
```shell
1693+
kubectl get shirts.stable.example.com --field-selector spec.color=blue
1694+
```
1695+
1696+
Should output:
1697+
1698+
```
1699+
NAME COLOR SIZE
1700+
example1 blue S
1701+
example2 blue M
1702+
```
1703+
1704+
Get only resources with a `color` of `green` and a `size` of `M`:
1705+
1706+
```shell
1707+
kubectl get shirts.stable.example.com --field-selector spec.color=green,spec.size=M
1708+
```
1709+
1710+
Should output:
1711+
1712+
```
1713+
NAME COLOR SIZE
1714+
example2 blue M
1715+
```
1716+
16271717
#### Priority
16281718

16291719
Each column includes a `priority` field. Currently, the priority
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: shirts.stable.example.com
5+
spec:
6+
group: stable.example.com
7+
scope: Namespaced
8+
names:
9+
plural: shirts
10+
singular: shirt
11+
kind: Shirt
12+
versions:
13+
- name: v1
14+
served: true
15+
storage: true
16+
schema:
17+
openAPIV3Schema:
18+
type: object
19+
properties:
20+
spec:
21+
type: object
22+
properties:
23+
color:
24+
type: string
25+
size:
26+
type: string
27+
selectableFields:
28+
- jsonPath: .spec.color
29+
- jsonPath: .spec.size
30+
additionalPrinterColumns:
31+
- jsonPath: .spec.color
32+
name: Color
33+
type: string
34+
- jsonPath: .spec.size
35+
name: Size
36+
type: string
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
apiVersion: stable.example.com/v1
3+
kind: Shirt
4+
metadata:
5+
name: example1
6+
spec:
7+
color: blue
8+
size: S
9+
---
10+
apiVersion: stable.example.com/v1
11+
kind: Shirt
12+
metadata:
13+
name: example2
14+
spec:
15+
color: blue
16+
size: M
17+
---
18+
apiVersion: stable.example.com/v1
19+
kind: Shirt
20+
metadata:
21+
name: example3
22+
spec:
23+
color: green
24+
size: M

0 commit comments

Comments
 (0)