Skip to content

Commit 88380ad

Browse files
authored
Modify note content about kubectl, CRDs, and subresources (#34468)
* Modified the NOTE for Kubectl Subresource Support. Improvement: Removed word tool. Typo fix. * Added task for patch the deployment using subresource flag. * Updated the content. * Updated the link for kubectl patch. * Resolved nits. * Improvement: updated the content. * Removed new deployment and used existing deployment manifests.
1 parent f456be2 commit 88380ad

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,18 @@ The following is an example procedure to upgrade from `v1beta1` to `v1`.
10811081
3. Remove `v1beta1` from the CustomResourceDefinition `status.storedVersions` field.
10821082

10831083
{{< note >}}
1084-
The `kubectl` tool currently cannot be used to edit or patch the `status` subresource on a CRD: see the [Kubectl Subresource Support KEP](https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/2590-kubectl-subresource) for more details.
1084+
The flag `--subresource` is used with the kubectl get, patch, edit, and replace commands to
1085+
fetch and update the subresources, `status` and `scale`, for all the API resources that
1086+
support them. This flag is available starting from kubectl version v1.24. Previously, reading
1087+
subresources (like `status`) via kubectl involved using `kubectl --raw`, and updating
1088+
subresources using kubectl was not possible at all. Starting from v1.24, the `kubectl` tool
1089+
can be used to edit or patch the `status` subresource on a CRD object. See [How to patch a Deployment using the subresource flag](/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#scale-kubectl-patch).
10851090

1086-
The easier way to patch the status subresource from the CLI is directly interacting with the API server using the `curl` tool, in this example:
1091+
This page is part of the documentation for Kubernetes v{{< skew currentVersion >}}.
1092+
If you are running a different version of Kubernetes, consult the documentation for that release.
1093+
1094+
Here is an example of how to patch the `status` subresource for a CRD object using `kubectl`:
10871095
```bash
1088-
kubectl proxy &
1089-
curl --header "Content-Type: application/json-patch+json" \
1090-
--request PATCH http://localhost:8001/apis/apiextensions.k8s.io/v1/customresourcedefinitions/<your CRD name here>/status \
1091-
--data '[{"op": "replace", "path": "/status/storedVersions", "value":["v1"]}]'
1096+
kubectl patch customresourcedefinitions <CRD_Name> --subresource='status' --type='merge' -p '{"status":{"storedVersions":["v1"]}}'
10921097
```
10931098
{{< /note >}}

content/en/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ You can also see the `retainKeys` strategy in the [OpenApi spec](https://raw.git
392392
And you can see the `retainKeys` strategy in the
393393
[Kubernetes API documentation](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#deploymentspec-v1-apps).
394394

395-
## Alternate forms of the kubectl patch command
395+
### Alternate forms of the kubectl patch command
396396

397397
The `kubectl patch` command takes YAML or JSON. It can take the patch as a file or
398398
directly on the command line.
@@ -427,6 +427,90 @@ kubectl patch deployment patch-demo --patch-file patch-file.json
427427
kubectl patch deployment patch-demo --patch '{"spec": {"template": {"spec": {"containers": [{"name": "patch-demo-ctr-2","image": "redis"}]}}}}'
428428
```
429429

430+
### Update an object's replica count using `kubectl patch` with `--subresource` {#scale-kubectl-patch}
431+
432+
{{< feature-state for_k8s_version="v1.24" state="alpha" >}}
433+
434+
The flag `--subresource=[subresource-name]` is used with kubectl commands like get, patch,
435+
edit and replace to fetch and update `status` and `scale` subresources of the resources
436+
(applicable for kubectl version v1.24 or more). This flag is used with all the API resources
437+
(built-in and CRs) which has `status` or `scale` subresource. Deployment is one of the
438+
examples which supports these subresources.
439+
440+
Here's a manifest for a Deployment that has two replicas:
441+
442+
{{< codenew file="application/deployment.yaml" >}}
443+
444+
Create the Deployment:
445+
446+
```shell
447+
kubectl apply -f https://k8s.io/examples/application/deployment.yaml
448+
```
449+
450+
View the Pods associated with your Deployment:
451+
452+
```shell
453+
kubectl get pods -l app=nginx
454+
```
455+
456+
In the output, you can see that Deployment has two Pods. For example:
457+
458+
```
459+
NAME READY STATUS RESTARTS AGE
460+
nginx-deployment-7fb96c846b-22567 1/1 Running 0 47s
461+
nginx-deployment-7fb96c846b-mlgns 1/1 Running 0 47s
462+
```
463+
464+
Now, patch that Deployment with `--subresource=[subresource-name]` flag:
465+
466+
```shell
467+
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":3}}'
468+
```
469+
470+
The output is:
471+
472+
```shell
473+
scale.autoscaling/nginx-deployment patched
474+
```
475+
476+
View the Pods associated with your patched Deployment:
477+
478+
```shell
479+
kubectl get pods -l app=nginx
480+
```
481+
482+
In the output, you can see one new pod is created, so now you have 3 running pods.
483+
484+
```
485+
NAME READY STATUS RESTARTS AGE
486+
nginx-deployment-7fb96c846b-22567 1/1 Running 0 107s
487+
nginx-deployment-7fb96c846b-lxfr2 1/1 Running 0 14s
488+
nginx-deployment-7fb96c846b-mlgns 1/1 Running 0 107s
489+
```
490+
491+
View the patched Deployment:
492+
493+
```shell
494+
kubectl get deployment nginx-deployment -o yaml
495+
```
496+
497+
```yaml
498+
...
499+
spec:
500+
replicas: 3
501+
...
502+
status:
503+
...
504+
availableReplicas: 3
505+
readyReplicas: 3
506+
replicas: 3
507+
```
508+
509+
{{< note >}}
510+
If you run `kubectl patch` and specify `--subresource` flag for resource that doesn't support that
511+
particular subresource, the API server returns a 404 Not Found error.
512+
{{< /note >}}
513+
430514
## Summary
431515

432516
In this exercise, you used `kubectl patch` to change the live configuration

0 commit comments

Comments
 (0)