Skip to content

Commit ea82a43

Browse files
doc: add info about status and webhooks
1 parent d2fa79c commit ea82a43

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/book/src/reference/admission-webhook.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,81 @@ if you want to authenticate the clients, you can configure the apiserver to use
2121
basic auth, bearer token, or a cert to authenticate itself to the webhooks.
2222
You can find detailed steps
2323
[here](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#authenticate-apiservers).
24+
25+
<aside class="note">
26+
<H1>Execution Order</H1>
27+
28+
**Validating webhooks run after all mutating webhooks**, so you don't need to worry about another webhook changing an
29+
object after your validation has accepted it.
30+
31+
</aside>
32+
33+
## Handling Resource Status in Admission Webhooks
34+
35+
<aside class="warning">
36+
<H1>Execution Order</H1>
37+
38+
**You cannot modify or default the status of a resource using a mutating admission webhook**.
39+
Set initial status in your controller when you first see a new object.
40+
41+
</aside>
42+
43+
### Understanding Why:
44+
45+
#### Mutating Admission Webhooks
46+
47+
Mutating Admission Webhooks are primarily designed to intercept and modify requests concerning the creation,
48+
modification, or deletion of objects. Though they possess the capability to modify an object's specification,
49+
directly altering its status isn't deemed a standard practice,
50+
often leading to unintended results.
51+
52+
```go
53+
// MutatingWebhookConfiguration allows for modification of objects.
54+
// However, direct modification of the status might result in unexpected behavior.
55+
type MutatingWebhookConfiguration struct {
56+
...
57+
}
58+
```
59+
60+
#### Setting Initial Status
61+
62+
For those diving into custom controllers for custom resources, it's imperative to grasp the concept of setting an
63+
initial status. This initialization typically takes place within the controller itself. The moment the controller
64+
identifies a new instance of its managed resource, primarily through a watch mechanism, it holds the authority
65+
to assign an initial status to that resource.
66+
67+
```go
68+
// Custom controller's reconcile function might look something like this:
69+
func (r *ReconcileMyResource) Reconcile(request reconcile.Request) (reconcile.Result, error) {
70+
// ...
71+
// Upon discovering a new instance, set the initial status
72+
instance.Status = SomeInitialStatus
73+
// ...
74+
}
75+
```
76+
77+
#### Status Subresource
78+
79+
Delving into Kubernetes custom resources, a clear demarcation exists between the spec (depicting the desired state)
80+
and the status (illustrating the observed state). Activating the /status subresource for a custom resource definition
81+
(CRD) bifurcates the `status` and `spec`, each assigned to its respective API endpoint.
82+
This separation ensures that changes introduced by users, such as modifying the spec, and system-driven updates,
83+
like status alterations, remain distinct. Leveraging a mutating webhook to tweak the status during a spec-modifying
84+
operation might not pan out as expected, courtesy of this isolation.
85+
86+
```yaml
87+
apiVersion: apiextensions.k8s.io/v1
88+
kind: CustomResourceDefinition
89+
metadata:
90+
name: myresources.mygroup.mydomain
91+
spec:
92+
...
93+
subresources:
94+
status: {} # Enables the /status subresource
95+
```
96+
97+
#### Conclusion
98+
99+
While certain edge scenarios might allow a mutating webhook to seamlessly modify the status, treading this path isn't a
100+
universally acclaimed or recommended strategy. Entrusting the controller logic with status updates remains the
101+
most advocated approach.

0 commit comments

Comments
 (0)