Skip to content

Commit f813968

Browse files
authored
clarify how delay is calculated (kubernetes-sigs#907)
* clarify how delay is calculated * improve writing * fix typo
1 parent f14a74e commit f813968

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

site/content/en/docs/user/stages-configuration.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Please note that `weight` only takes effect among stages with same `resourceRef`
8888
Additionally, the `delay` field in a Stage resource allows users to specify a delay before the stage is applied,
8989
and introduce jitter to the delay to specify the latest delay time to make the simulation more realistic.
9090
This can be useful for simulating real-world scenarios where events do not always happen at the same time.
91+
Please refer to [How Delay is Calculated] for more details.
9192

9293
By configuring the `delay`, `selector`, and `next` fields in a Stage, you can control when and how the stage is applied,
9394
providing a flexible and scalable way to simulate real-world scenarios in your Kubernetes cluster.
@@ -152,6 +153,45 @@ if a "Change Stage" is matched. The update action itself consequently causes a n
152153
However, this event-driven approach to applying Stages has a limitation: `kwok` won’t apply a Stage until a new event associated with that resource is received. To address the limitation,
153154
users can utilize the `immediateNextStage` field to make the controller apply Stages immediately rather than waiting for an event pushed from the apiserver.
154155

156+
## How Delay is Calculated
157+
158+
The delay time of applying a Stage is obtained by adding a constant time period and a randomized interval,
159+
which will be denoted by *duration* and *jitter* respectively in the following.
160+
161+
- `durationMilliseconds`: provides *duration*.
162+
- `jitterDurationMilliseconds`: calculates *jitter* by *random*(`jitterDurationMilliseconds`-*duration*).
163+
It shall be larger than `durationMilliseconds` if you want to inject jitter to the delay.
164+
Otherwise, it will be used directly as the delay time without any randomization.
165+
166+
`kwok` also provides other fields to flexibly handle more situations. They are used to extract
167+
and parse a RFC3339 timestamp field of a resource to help determine the delay time dynamically.
168+
169+
- `durationFrom`: calculates *duration* by `durationFrom`-*now*
170+
- `jitterDurationFrom`: calculates *jitter* by *random*(`jitterDurationFrom`-*now*-*duration*),
171+
where *now* is the timestamp when the delay starts.
172+
173+
{{< hint "info" >}}
174+
`durationFrom` and `jitterDurationFrom` have a higher priority than `durationMilliseconds`
175+
and `jitterDurationMilliseconds` if both are set.
176+
{{< /hint >}}
177+
178+
Let’s explain a little bit about the motivation behind these two advanced fields.
179+
But before that, for a better understanding, we briefly describe how kubelet "delete" a pod from a node.
180+
181+
Here are the steps to remove a pod in Kubernetes:
182+
183+
1. Execute the command `kubectl delete pod`. The apiserver receives the deletion request but does not immediately remove the corresponding pod resource from etcd.
184+
2. The apiserver sets the `metadata.deletionTimestamp` field to the time the request was issued plus a short period, defined by `metadata.deletionGracePeriodSeconds` (default 30s).
185+
3. The kubelet detects a non-null `metadata.deletionTimestamp` for a pod and starts to send a `TERM` signal to the main process of the container.
186+
4. If the `metadata.deletionTimestamp` expires before the process stops by itself, the main process is then terminated using the `KILL` signal.
187+
5. After all the containers in the pod have stopped running, the kubelet sends a force deletion request to the apiserver.
188+
6. The apiserver removes the pod object from etcd.
189+
190+
To simulate this situation, you can set `jitterDurationFrom` of a "Delete Stage" (`next.delete: true`) to point to `metadata.deletionTimestamp`.
191+
This will cause the deletion operation to occur at a random moment before `metadata.deletionTimestamp` expires.
192+
You can also let `kwok` perform the deletion in a deterministic way by pointing `durationFrom` to `metadata.deletionTimple`,
193+
making the deletion happen exactly at `metadata.deletionTimple`.
194+
155195
## Examples
156196

157197
### Node Stages
@@ -216,3 +256,4 @@ This example shows how to configure the simplest and fastest stages of Pod resou
216256
[General Pod Stages]: https://github.com/kubernetes-sigs/kwok/tree/main/kustomize/stage/pod/general
217257
[Stage API]: {{< relref "/docs/generated/apis" >}}#kwok.x-k8s.io/v1alpha1.Stage
218258
[Resource Lifecycle Simulation Controller]: {{< relref "/docs/design/architecture" >}}
259+
[How Delay is Calculated]: {{< relref "/docs/user/stages-configuration#how-delay-is-calculated" >}}

0 commit comments

Comments
 (0)