Skip to content

Commit ad67096

Browse files
nikhitak8s-publishing-bot
authored andcommitted
sample-controller: add status subresource support
- Add an example to show how to use status subresources with custom resources. - Update the comment in the controller to mention that `UpdateStatus` can now be used. - Generate `UpdateStatus` for Foo. - Update the README to remove feature gate information for CRD validation since the current example requires a v1.9 cluster and it is enabled by default. - Update the README to add feature gate information for CustomResourceSubResources. Kubernetes-commit: 7c06d6fb17ee46f587c1facff93b9bb185522855
1 parent aadb6d0 commit ad67096

File tree

6 files changed

+69
-19
lines changed

6 files changed

+69
-19
lines changed

Diff for: README.md

+32-5
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,44 @@ type User struct {
7979

8080
To validate custom resources, use the [`CustomResourceValidation`](https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/#validation) feature.
8181

82-
This feature is beta and enabled by default in v1.9. If you are using v1.8, enable the feature using
83-
the `CustomResourceValidation` feature gate on the [kube-apiserver](https://kubernetes.io/docs/admin/kube-apiserver):
82+
This feature is beta and enabled by default in v1.9.
83+
84+
### Example
85+
86+
The schema in [`crd-validation.yaml`](./artifacts/examples/crd-validation.yaml) applies the following validation on the custom resource:
87+
`spec.replicas` must be an integer and must have a minimum value of 1 and a maximum value of 10.
88+
89+
In the above steps, use `crd-validation.yaml` to create the CRD:
90+
91+
```sh
92+
# create a CustomResourceDefinition supporting validation
93+
$ kubectl create -f artifacts/examples/crd-validation.yaml
94+
```
95+
96+
## Subresources
97+
98+
Custom Resources support `/status` and `/scale` subresources as an
99+
[alpha feature](https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/#subresources) in v1.10.
100+
Enable this feature using the `CustomResourceSubresources` feature gate on the [kube-apiserver](https://kubernetes.io/docs/admin/kube-apiserver):
84101

85102
```sh
86-
--feature-gates=CustomResourceValidation=true
103+
--feature-gates=CustomResourceSubresources=true
87104
```
88105

89106
### Example
90107

91-
The schema in the [example CRD](./artifacts/examples/crd.yaml) applies the following validation on the custom resource:
92-
`spec.replicas` must be an integer and must have a minimum value of 1 and a maximum value of 10.
108+
The CRD in [`crd-status-subresource.yaml`](./artifacts/examples/crd-status-subresource.yaml) enables the `/status` subresource
109+
for custom resources.
110+
This means that [`UpdateStatus`](./controller.go#L330) can be used by the controller to update only the status part of the custom resource.
111+
112+
To understand why only the status part of the custom resource should be updated, please refer to the [Kubernetes API conventions](https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status).
113+
114+
In the above steps, use `crd-status-subresource.yaml` to create the CRD:
115+
116+
```sh
117+
# create a CustomResourceDefinition supporting the status subresource
118+
$ kubectl create -f artifacts/examples/crd-status-subresource.yaml
119+
```
93120

94121
## Cleanup
95122

Diff for: artifacts/examples/crd-status-subresource.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: foos.samplecontroller.k8s.io
5+
spec:
6+
group: samplecontroller.k8s.io
7+
version: v1alpha1
8+
names:
9+
kind: Foo
10+
plural: foos
11+
scope: Namespaced
12+
subresources:
13+
status: {}

Diff for: artifacts/examples/crd-validation.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: foos.samplecontroller.k8s.io
5+
spec:
6+
group: samplecontroller.k8s.io
7+
version: v1alpha1
8+
names:
9+
kind: Foo
10+
plural: foos
11+
scope: Namespaced
12+
validation:
13+
openAPIV3Schema:
14+
properties:
15+
spec:
16+
properties:
17+
replicas:
18+
type: integer
19+
minimum: 1
20+
maximum: 10

Diff for: artifacts/examples/crd.yaml

-9
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,3 @@ spec:
99
kind: Foo
1010
plural: foos
1111
scope: Namespaced
12-
validation:
13-
openAPIV3Schema:
14-
properties:
15-
spec:
16-
properties:
17-
replicas:
18-
type: integer
19-
minimum: 1
20-
maximum: 10

Diff for: controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,10 @@ func (c *Controller) updateFooStatus(foo *samplev1alpha1.Foo, deployment *appsv1
327327
// Or create a copy manually for better performance
328328
fooCopy := foo.DeepCopy()
329329
fooCopy.Status.AvailableReplicas = deployment.Status.AvailableReplicas
330-
// Until #38113 is merged, we must use Update instead of UpdateStatus to
331-
// update the Status block of the Foo resource. UpdateStatus will not
332-
// allow changes to the Spec of the resource, which is ideal for ensuring
333-
// nothing other than resource status has been updated.
330+
// If the CustomResourceSubresources feature gate is not enabled,
331+
// we must use Update instead of UpdateStatus to update the Status block of the Foo resource.
332+
// UpdateStatus will not allow changes to the Spec of the resource,
333+
// which is ideal for ensuring nothing other than resource status has been updated.
334334
_, err := c.sampleclientset.SamplecontrollerV1alpha1().Foos(foo.Namespace).Update(fooCopy)
335335
return err
336336
}

Diff for: pkg/apis/samplecontroller/v1alpha1/types.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
)
2222

2323
// +genclient
24-
// +genclient:noStatus
2524
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
2625

2726
// Foo is a specification for a Foo resource

0 commit comments

Comments
 (0)