Fix the replay of liveness and readiness probes

Review the AI's codeEasy12 minGoogle and Amazon style

Your task

An assistant's Kubernetes manifest pointed both probes at a /healthz that checks a downstream API. When that API slowed down, the liveness probe failed and Kubernetes restarted the whole fleet in a loop. The two probes have opposite remedies: a failing liveness probe restarts the container, and a failing readiness probe only takes the pod out of the load balancer. The team now replays a probe's results to see which actions each one takes.

An assistant wrote simulate_probe_actions(kind, results, failure_threshold). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • kind: "liveness" or "readiness".
  • results: one probe result per tick, true for a success and false for a failure. Ticks are numbered from 0. The list can be empty.
  • failure_threshold: how many failures in a row trigger an action, an integer. A value below 1 counts as 1.
  • Return the actions in tick order, each as "<tick>:<action>", such as "3:restart".

Rules the tests check:

  • Any other kind, such as "startup", returns an empty list. The match is case sensitive.
  • Count failures in a row. Any success resets the count to 0, for both kinds.
  • When the count reaches the threshold, reset it to 0, then act on that tick:
    • Liveness: add "<tick>:restart".
    • Readiness: if the pod is registered, mark it not registered and add "<tick>:deregister". If it is not registered, add nothing.
  • Readiness only: the pod starts not registered. A success while it is not registered registers it and adds "<tick>:register". A success while it is registered adds nothing.
  • Liveness never adds register or deregister.

Examples

simulate_probe_actions(kind: str, results: list[bool], failure_threshold: int) -> list[str]
Example 1
Inputsimulate_probe_actions("liveness", [True, True, False, False, False, False], 2)
Output["3:restart", "5:restart"]
Example 2
Inputsimulate_probe_actions("readiness", [True, True, False, False, False, False], 2)
Output["0:register", "3:deregister"]
Example 3
Inputsimulate_probe_actions("readiness", [False, True, False, False, True], 2)
Output["1:register", "3:deregister", "4:register"]