Skip to content

Commit 96d07d5

Browse files
committed
minor formatting and rewording on admission page
Signed-off-by: clux <sszynrae@gmail.com>
1 parent a379fcb commit 96d07d5

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

docs/controllers/admission.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ If your controller [[object]] is a CRD you own, then this is the recommended way
4141
This requires __Kubernetes >=1.25__ (where the feature is Beta), or Kubernetes >= 1.29 (where the [feature is GA](https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.29.md)).
4242
4343
44-
To include validation rules in the schemas you must add `x-kubernetes-validations` entries by [[schemas#overriding-members]] for the necessary types manually (or inject them more manually):
44+
To include validation rules in the schemas you must add `x-kubernetes-validations` entries by [[schemas#overriding-members]] for the necessary types manually (or use the `x-kube` validation attribute). The manual way (which does not depend on `KubeSchema`):
4545

4646
```rust
4747
fn string_legality(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
@@ -56,15 +56,21 @@ fn string_legality(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::
5656
}
5757
```
5858

59-
and this can be attached with a `#[schemars(schema_with = "string_legality)]` field attribute on some `Option<String>` (here). See [#1372](https://github.com/kube-rs/kube/pull/1372/files) too see interactions with errors and a larger struct and other validations.
59+
this fn can be attached with a `#[schemars(schema_with = "string_legality)]` field attribute on some `Option<String>` (in this example). See [#1372](https://github.com/kube-rs/kube/pull/1372/files) too see interactions with errors and a larger struct and other validations.
6060

6161
## `x_kube` validation
6262

63-
To simplify the generation of CRD schemas, `kube-rs` offers a dedicated attribute macro accessible via the `KubeSchema` derive. This implementation allows users to declaratively extend each field with validation rules tailored to their specific requirements. Additionally, users have the option to specify additional `x-kubernetes` extension flags, such as the SSA merge strategy, which facilitates the structuring of lists and maps in a specific format.
63+
To reduce manual schema overriding for CRDs, the alternate [KubeSchema] derive macro can be used instead of [JsonSchema].
6464

65-
Here are some examples to showcase these capabilities.
65+
!!! note "`JsonSchema` vs `KubeSchema`"
66+
67+
This macro generates `schemars` `JsonSchema` derive macro for the provided structure. Keep in mind that using the `KubeSchema` derive macro replaces `JsonSchema` derive, and specifying both will cause a conflict.*
68+
69+
70+
This implementation allows users to declaratively extend each field with validation rules (along with other `x-kubernetes` schema properties).
6671

6772
### `x_kube(validation = …)` attribute
73+
This attribute can be used to set one or more `validation` rules on a field. Rules can be created via one of; an explicit [Rule] / a string expression / a (string, reason) string pair;
6874

6975
```rust
7076
#[derive(KubeSchema)]
@@ -79,18 +85,16 @@ pub struct FooSpec {
7985
}
8086
```
8187

82-
!!! note "`JsonSchema` vs `KubeSchema`"
83-
84-
This macro generates `schemars` `JsonSchema` derive macro for the provided structure. Keep in mind that using the `KubeSchema` derive macro replaces `JsonSchema` derive, and specifying both will cause a conflict.*
88+
The `x_kube(validation = ...)` macro uses the [Rule] structure underneath. This can be constructed using the builder pattern, allowing users to extend the validation with a `message` to distinguish specific validation error and alternative [reasons](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-reason) via `reason`, as well as a `field_path` for detailed json path to the invalid field value.
8589

86-
The `x_kube(validation = ...)` macro uses a `Rule` structure underneath. A `Rule` can be constructed with builder pattern, allowing users to extend the validation with a `message` to distinguish specific validation error and alternative [reasons](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-reason) via `reason`, as well as a `field_path` for detailed json path to the invalid field value.
87-
88-
Alternatively, the rule macro can be constructed from a string or a tuple of two strings. The first string represents the validation rule, and the second string is the message assigned to the rule.
90+
Alternatively, the rule can be constructed implicitly from a string or a tuple of two strings. The first string represents the validation rule, and the second string is the message assigned to the rule.
8991

9092
To write CEL expressions consider using the [CEL playground](https://playcel.undistro.io/). There are more examples in the [CRD Validation Rules announcement blog](https://kubernetes.io/blog/2022/09/23/crd-validation-rules-beta/) and under [kubernetes.io crd validation-rules](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules).
9193

9294
### `x_kube(merge_strategy = …)` attribute
9395

96+
This `x-kubernetes` extension flag controls the [Kubernetes SSA merge strategy](https://kubernetes.io/docs/reference/using-api/server-side-apply/#merge-strategy) from our [MergeStrategy] enum and facilitates the structuring of lists and maps in a specific format.
97+
9498
```rust
9599
#[derive(KubeSchema)]
96100
pub struct FooSpec {
@@ -104,7 +108,7 @@ pub struct FooItem {
104108
}
105109
```
106110

107-
This example generates a `x-kubernetes-list-type=map` and `x-kubernetes-list-map-keys=["key"]` attributes with the field. This instructs the API server to treat the underlying list as a map and ensures that `key` field is used as a unique key for the internal map, preventing conflicts during submission of duplicate keys provided by different manager. For more details refer to [merge-strategy](https://kubernetes.io/docs/reference/using-api/server-side-apply/#merge-strategy) in k8s docs.
111+
This example generates a `x-kubernetes-list-type=map` and `x-kubernetes-list-map-keys=["key"]` attributes with the field. This instructs the API server to treat the underlying list as a map and ensures that `key` field is used as a unique key for the internal map, preventing conflicts on submission of duplicate keys by different managers.
108112

109113
## Validation Using Webhooks
110114
AKA writing an [admission controller](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/).

includes/links.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
[Server-Side Apply]: https://kubernetes.io/docs/reference/using-api/server-side-apply/
104104
[k3d]: https://k3d.io/
105105
[JsonSchema]: https://docs.rs/schemars/latest/schemars/trait.JsonSchema.html
106+
[KubeSchema]: https://docs.rs/kube/latest/kube/derive.KubeSchema.html
107+
[Rule]: https://docs.rs/kube/latest/kube/core/struct.Rule.html
108+
[MergeStrategy]: https://docs.rs/kube/latest/kube/core/cel/enum.MergeStrategy.html
106109
[env_logger]: https://docs.rs/env_logger/latest/env_logger/
107110
[AdmissionReview]: https://docs.rs/kube/latest/kube/core/admission/struct.AdmissionReview.html
108111
[AdmissionResponse]: https://docs.rs/kube/latest/kube/core/admission/struct.AdmissionResponse.html

0 commit comments

Comments
 (0)