Skip to content

Commit 00e596b

Browse files
authored
Merge pull request #439 from clux/patch-docs
update patch documentation
2 parents a2d33fb + 7591adc commit 00e596b

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ fmt:
1212
cargo +nightly fmt
1313

1414
doc:
15-
cargo +nightly doc --lib
15+
# TODO: replace with RUSTDOCFLAGS="--cfg docsrs" once it works
16+
cargo +nightly doc --lib --workspace --features=derive,ws,oauth,jsonpatch
1617
xdg-open target/doc/kube/index.html
1718

1819
test:

kube-derive/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod custom_resource;
7070
///
7171
/// ### `#[kube(apiextensions = "v1beta1")]`
7272
/// The version for `CustomResourceDefinition` desired in the `apiextensions.k8s.io` group.
73-
/// Default is `v1` (for clusters >= 1.17). If using kubernetes <= 1.16 pluase use `v1beta1`.
73+
/// Default is `v1` (for clusters >= 1.17). If using kubernetes <= 1.16 please use `v1beta1`.
7474
///
7575
/// **NOTE**: Support for `v1` requires deriving the openapi v3 `JsonSchema` via the `schemars` dependency.
7676
///
@@ -150,6 +150,19 @@ mod custom_resource;
150150
/// }
151151
/// ```
152152
///
153+
/// ## Customizing Schemas
154+
/// Should you need to customize the schemas, you can use:
155+
/// - [Serde/Schemars Attributes](https://graham.cool/schemars/examples/3-schemars_attrs/) (no need to duplicate serde renames)
156+
/// - [`#[schemars(schema_with = "func")]`](https://graham.cool/schemars/examples/7-custom_serialization/) (e.g. like in the [`crd_derive` example](https://github.com/clux/kube-rs/blob/master/examples/crd_derive.rs))
157+
/// - `impl JsonSchema` on a type / newtype around external type. See [#129](https://github.com/clux/kube-rs/issues/129#issuecomment-750852916)
158+
///
159+
/// In general, you will need to override parts of the schemas (for fields in question) when you are:
160+
/// - **using complex enums**: enums do not currently generate [structural schemas](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema), so kubernetes won't support them by default
161+
/// - **customizing [merge-strategies](https://kubernetes.io/docs/reference/using-api/server-side-apply/#merge-strategy)** (e.g. like in the [`crd_derive_schema` example](https://github.com/clux/kube-rs/blob/master/examples/crd_derive_schema.rs))
162+
/// - **customizing [certain kubebuilder like validation rules](https://github.com/clux/kube-rs/issues/129#issuecomment-749463718)** (tail the issue for state of affairs)
163+
///
164+
/// If you have to override a lot, [you can opt-out of schema-generation entirely](https://github.com/clux/kube-rs/issues/355#issuecomment-751253657)
165+
///
153166
/// ## Debugging
154167
/// Try `cargo-expand` to see your own macro expansion.
155168
///

kube/src/api/params.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,41 @@ impl PostParams {
144144

145145
/// Describes changes that should be applied to a resource
146146
///
147-
/// For all strategies except `Json`, patch can be represented with arbitrary
148-
/// serializable value, such as `serde_json::Value`. You may also want to use
149-
/// a `k8s-openapi` definition for the resource for the better type safety.
147+
/// Takes arbitrary serializable data for all strategies except `Json`.
148+
///
149+
/// We recommend using ([server-side](https://kubernetes.io/blog/2020/04/01/kubernetes-1.18-feature-server-side-apply-beta-2)) `Apply` patches on new kubernetes releases.
150+
///
151+
/// See [kubernetes patch docs](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment) for the older patch types.
152+
///
153+
/// Note that patches have different effects on different fields depending on their merge strategies.
154+
/// These strategies are configurable when deriving your [`CustomResource`](kube_derive::CustomResource#customizing-schemas).
155+
///
156+
/// # Creating a patch via serde_json
157+
/// ```
158+
/// use kube::api::Patch;
159+
/// let patch = serde_json::json!({
160+
/// "apiVersion": "v1",
161+
/// "kind": "Pod",
162+
/// "metadata": {
163+
/// "name": "blog"
164+
/// },
165+
/// "spec": {
166+
/// "activeDeadlineSeconds": 5
167+
/// }
168+
/// });
169+
/// let patch = Patch::Apply(&patch);
170+
/// ```
171+
/// # Creating a patch from a type
172+
/// ```
173+
/// use kube::api::Patch;
174+
/// use k8s_openapi::api::rbac::v1::Role;
175+
/// use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
176+
/// let r = Role {
177+
/// metadata: ObjectMeta { name: Some("user".into()), ..ObjectMeta::default() },
178+
/// rules: Some(vec![])
179+
/// };
180+
/// let patch = Patch::Apply(&r);
181+
/// ```
150182
#[non_exhaustive]
151183
pub enum Patch<T: Serialize> {
152184
/// [Server side apply](https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply)

kube/src/api/typed.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,9 @@ where
193193
self.client.request_status::<ObjectList<K>>(req).await
194194
}
195195

196-
/// Patch a resource a subset of its properties
196+
/// Patch a subset of a resource's properties
197197
///
198-
/// Note that actual `patch` value depends on what `PatchStrategy` is used.
199-
/// It is configured in `PatchParams` and defauls to strategic merge
200-
/// patch.
201-
///
202-
/// See [kubernetes json patch types](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment)
203-
/// for more information about their distinction.
198+
/// Takes a [`Patch`] along with [`PatchParams`] for the call.
204199
///
205200
/// ```no_run
206201
/// use kube::{api::{Api, PatchParams, Patch, Meta}, Client};
@@ -225,10 +220,8 @@ where
225220
/// Ok(())
226221
/// }
227222
/// ```
228-
/// [`Merge`]: super::PatchStrategy::Merge
229-
/// [`JSON`]: super::PatchStrategy::JSON
230-
/// [`Strategic`]: super::PatchStrategy::Strategic
231-
/// [`Apply`]: super::PatchStrategy::Apply
223+
/// [`Patch`]: super::Patch
224+
/// [`PatchParams`]: super::PatchParams
232225
pub async fn patch<P: Serialize>(&self, name: &str, pp: &PatchParams, patch: &Patch<P>) -> Result<K> {
233226
let req = self.resource.patch(name, &pp, patch)?;
234227
self.client.request::<K>(req).await

0 commit comments

Comments
 (0)