You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From 1.25 onward, unrecognized or duplicate fields in an object are detected via
794
795
validation on the server when you use HTTP verbs that can submit data (`POST`, `PUT`, and `PATCH`). Possible levels of
@@ -937,17 +938,143 @@ rules:
937
938
938
939
See [Authorization Overview](/docs/reference/access-authn-authz/authorization/).
939
940
940
-
## Server Side Apply
941
+
## Updates to existing resources {#patch-and-apply}
942
+
943
+
Kubernetes provides several ways to update existing objects.
944
+
You can read [choosing an update mechanism](#update-mechanism-choose) to
945
+
learn about which approach might be best for your use case.
946
+
947
+
You can overwrite (**update**) an existing resource - for example, a ConfigMap -
948
+
using an HTTP PUT. For a PUT request, it is the client's responsibility to specify
949
+
the `resourceVersion` (taking this from the object being updated). Kubernetes uses
950
+
that `resourceVersion` information so that the API server can detect lost updates
951
+
and reject requests made by a client that is out of date with the cluster.
952
+
In the event that the resource has changed (the `resourceVersion` the client
953
+
provided is stale), the API server returns a `409 Conflict` error response.
954
+
955
+
Instead of sending a PUT request, the client can send an instruction to the API
956
+
server to **patch** an existing resource. A **patch** is typically appropriate
957
+
if the change that the client wants to make isn't conditional on the existing data. Clients that need effective detection of lost updates should consider
958
+
making their request conditional on the existing `resourceVersion` (either HTTP PUT or HTTP PATCH),
959
+
and then handle any retries that are needed in case there is a conflict.
960
+
961
+
The Kubernetes API supports four different PATCH operations, determined by their
962
+
corresponding HTTP `Content-Type` header:
963
+
964
+
`application/apply-patch+yaml`
965
+
: Server Side Apply YAML (a Kubernetes-specific extension, based on YAML).
966
+
All JSON documents are valid YAML, so you can also submit JSON using this
967
+
media type. See [Server Side Apply serialization](/docs/reference/using-api/server-side-apply/#serialization)
968
+
for more details.
969
+
To Kubernetes, this is a **create** operation if the object does not exist,
970
+
or a **patch** operation if the object already exists.
971
+
972
+
`application/json-patch+json`
973
+
: JSON Patch, as defined in [RFC6902](https://tools.ietf.org/html/rfc6902).
974
+
A JSON patch is a sequence of operations that are executed on the resource;
975
+
for example `{"op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ]}`.
976
+
To Kubernetes, this is a **patch** operation.
977
+
978
+
A **patch** using `application/json-patch+json` can include conditions to
979
+
validate consistency, allowing the operation to fail if those conditions
980
+
are not met (for example, to avoid a lost update).
981
+
982
+
`application/merge-patch+json`
983
+
: JSON Merge Patch, as defined in [RFC7386](https://tools.ietf.org/html/rfc7386).
984
+
A JSON Merge Patch is essentially a partial representation of the resource.
985
+
The submitted JSON is combined with the current resource to create a new one,
986
+
then the new one is saved.
987
+
To Kubernetes, this is a **patch** operation.
988
+
989
+
`application/strategic-merge-patch+json`
990
+
: Strategic Merge Patch (a Kubernetes-specific extension based on JSON).
991
+
Strategic Merge Patch is a custom implementation of JSON Merge Patch.
992
+
You can only use Strategic Merge Patch with built-in APIs, or with aggregated
993
+
API servers that have special support for it. You cannot use
994
+
`application/strategic-merge-patch+json`with any API
995
+
defined using a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}}.
996
+
997
+
{{< note >}}
998
+
The Kubernetes _server side apply_ mechanism has superseded Strategic Merge
999
+
Patch.
1000
+
{{< /note >}}
1001
+
941
1002
942
1003
Kubernetes' [Server Side Apply](/docs/reference/using-api/server-side-apply/)
943
1004
feature allows the control plane to track managed fields for newly created objects.
944
1005
Server Side Apply provides a clear pattern for managing field conflicts,
945
-
offers server-side `Apply` and `Update` operations, and replaces the
1006
+
offers server-side **apply** and **update** operations, and replaces the
946
1007
client-side functionality of `kubectl apply`.
947
1008
948
-
The API verb for Server-Side Apply is **apply**.
1009
+
For Server-Side Apply, Kubernetes treats the request as a **create** if the object
1010
+
does not yet exist, and a **patch** otherwise. For other requests that use PATCH
1011
+
at the HTTP level, the logical Kubernetes operation is always **patch**.
1012
+
949
1013
See [Server Side Apply](/docs/reference/using-api/server-side-apply/) for more details.
950
1014
1015
+
### Choosing an update mechanism {#update-mechanism-choose}
1016
+
1017
+
#### HTTP PUT to replace existing resource {#update-mechanism-update}
1018
+
1019
+
The **update** (HTTP `PUT`) operation is simple to implement and flexible,
1020
+
but has drawbacks:
1021
+
1022
+
* You need to handle conflicts where the `resourceVersion` of the object changes
1023
+
between your client reading it and trying to write it back. Kubernetes always
1024
+
detects the conflict, but you as the client author need to implement retries.
1025
+
* You might accidentally drop fields if you decode an object locally (for example,
1026
+
using client-go, you could receive fields that your client does not know how to
1027
+
handle - and then drop them as part of your update.
1028
+
* If there's a lot of contention on the object (even on a field, or set of fields,
1029
+
that you're not trying to edit), you might have trouble sending the update.
1030
+
The problem is worse for larger objects and for objects with many fields.
1031
+
1032
+
#### HTTP PATCH using JSON Patch {#update-mechanism-json-patch}
1033
+
1034
+
A **patch** update is helpful, because:
1035
+
1036
+
* As you're only sending differences, you have less data to send in the `PATCH`
1037
+
request.
1038
+
* You can make changes that rely on existing values, such as copying the
1039
+
value of a particular field into an annotation.
1040
+
* Unlike with an **update** (HTTP `PUT`), making your change can happen right away
1041
+
even if there are frequent changes to unrelated fields): you usually would
1042
+
not need to retry.
1043
+
* You might still need to specify the `resourceVersion` (to match an existing object)
1044
+
if you want to be extra careful to avoid lost updates
1045
+
* It's still good practice to write in some retry logic in case of errors.
1046
+
* You can use test conditions to careful craft specific update conditions.
1047
+
For example, you can increment a counter without reading it if the existing
1048
+
value matches what you expect. You can do this with no lost update risk,
1049
+
even if the object has changed in other ways since you last wrote to it.
1050
+
(If the test condition fails, you can fall back to reading the current value
1051
+
and then write back the changed number).
1052
+
1053
+
However:
1054
+
1055
+
* you need more local (client) logic to build the patch; it helps a lot if you have
1056
+
a library implementation of JSON Patch, or even for making a JSON Patch specifically against Kubernetes
1057
+
* as the author of client software, you need to be careful when building the patch
1058
+
(the HTTP request body) not to drop fields (the order of operations matters)
1059
+
1060
+
#### HTTP PATCH using Server-Side Apply {#update-mechanism-server-side-apply}
1061
+
1062
+
Server-Side Apply has some clear benefits:
1063
+
1064
+
* A single round trip: it rarely requires making a `GET` request first.
1065
+
* and you can still detect conflicts for unexpected changes
1066
+
* you have the option to force override a conflict, if appropriate
1067
+
* Client implementations are easy to make
1068
+
* You get an atomic create-or-update operation without extra effort
1069
+
(similar to `UPSERT` in some SQL dialects)
1070
+
1071
+
However:
1072
+
1073
+
* Server-Side Apply does not work at all for field changes that depend on a current value of the object
1074
+
* You can only apply updates to objects. Some resources in the Kubernetes HTTP API are
1075
+
not objects (they do not have a `.metadata` field), and Server-Side Apply
1076
+
is only relevant for Kubernetes objects.
1077
+
951
1078
## Resource versions
952
1079
953
1080
Resource versions are strings that identify the server's internal version of an
0 commit comments