Files
Davanum Srinivas 00d375a743 fix(agent): wait for kube-apiserver to reload its authentication config (#2542)
* fix(agent): wait for kube-apiserver to reload its authentication config

PatchAPIServerManifest can replace the authentication configuration of a
running kube-apiserver by writing only the file. kube-apiserver then
reloads the file by itself: it builds the new authenticators, checks
their health every 10 seconds and puts them in use once they are
healthy. The agent waited only for /readyz, which answers 200 before,
during and after the reload. So the call returned before the new
configuration was in use.

Enabling OIDC on an existing cluster takes this path. On a cluster with
three control plane nodes and Kubernetes 1.36, each kube-apiserver
logged "reloaded authentication config" 9.97 to 9.99 seconds after the
agent wrote the file. The enablement returned 9.6 seconds before the
kube-apiserver of the last node had loaded the file, and a client with a
SPIRE token got "Unauthorized" from it for the next 9 seconds.
The tree of that run did not have the control plane endpoint health
gate yet, which drains a node before the patch.

After a files-only change that writes the file that
--authentication-config names, the agent now waits until the local
kube-apiserver reports the hash of the written content in
apiserver_authentication_config_controller_last_config_info.
kube-apiserver sets that hash after it has put the new authenticators in
use, so the agent compares no clocks. The wait ends after two minutes at
the latest. The agent then restores the file and reports
FAILED_RESTORED. When kube-apiserver counted failed reloads in that
time, the message says that it rejected the file. Until now such a
rejection was silent: the call reported success and kube-apiserver kept
its old configuration.

The agent reads the state of kube-apiserver before it writes the file.
When it cannot read the metrics, or kube-apiserver does not publish the
hash (Kubernetes before 1.34), it logs that and waits only for /readyz,
as before. A change of the manifest is not affected: kube-apiserver
restarts there and reads the file at its start.

With this change, on a tree with the health gate, two runs of the same
enablement: the agent waited 10.7 to 11.1 seconds on each node, the
enablement returned after the last kube-apiserver had loaded the file,
and a probe with a SPIRE token got no "Unauthorized" in 45 calls to
each kube-apiserver. No kube-apiserver restarted.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

* docs(agent): describe the wait for the authentication config reload

PatchAPIServerManifest now waits until kube-apiserver has loaded an
authentication configuration that the agent rewrote without a restart.
Say so where the design page describes how the call is verified.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

* docs(agent): say when the authentication config reload is not confirmed

The design page said that the agent waits until kube-apiserver has
loaded a rewritten authentication configuration, and that it restores
the file if kube-apiserver does not. That is true only when the agent
could read the loaded hash from the metrics of kube-apiserver before
the write. When that read fails, also on Kubernetes 1.34 and later, the
agent logs a warning and waits for /readyz alone, and the call can
return OUTCOME_APPLIED before kube-apiserver has loaded the content.
Say so, with the text of the warning.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

* fix(agent): confirm the old authentication config after a restore

When PatchAPIServerManifest gives up on a files-only change, it writes
the old files back and waits for /readyz. If kube-apiserver had loaded a
staged authentication configuration, that rollback starts another
reload. The reload takes about 10 seconds, the staged authenticators
stay in use until it ends, and /readyz answers 200 all the time. So the
call could return OUTCOME_FAILED_RESTORED while the node was not yet
back in its state from before the call.

After the rollback the agent now waits until the local kube-apiserver
reports again the hash that it had loaded before the write. The agent
already reads that hash before it writes the file. If kube-apiserver
never loaded the staged content, it still reports that hash and the wait
ends with the first read. The wait has the same limit of two minutes as
the wait for the staged content. When the limit passes, the call returns
OUTCOME_FAILED_NOT_RESTORED, and the message says that kube-apiserver
did not return to its previous authentication configuration.

The wait is for the hash from before the write and not for the hash of
the restored file. The two are the same when kube-apiserver had loaded
the file that was on the node. The first is also right when it had not,
and when the call created the file.

When the agent could not read the hash before the write, the restore
waits only for /readyz, as before. A files-only change can now take 440
seconds in the worst case: two minutes and 100 seconds for the change,
and the same for the restore. The NVKE controller gives the call 8
minutes.

One case stays open. A reload of the staged content that kube-apiserver
started before the rollback can end after the call has returned.
kube-apiserver then loads the restored file again by itself.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

* docs(controller): correct the worst case of the node agent call in a comment

The comment on WithRolloutNodeAgentApplyOptions says that the node agent
call takes about 400 seconds in the worst case. A files-only change of
the authentication config now also waits for kube-apiserver to load the
file, for up to two minutes after the write and for up to two minutes
after a restore. Its worst case is 440 seconds: two minutes and 100
seconds for the change, and the same for the restore.

The activity deadline of 8 minutes still covers that. Only the comment
changes.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

* fix(controller): give the node agent call 10 minutes

The activity that calls PatchAPIServerManifest had a deadline of 8
minutes, and its comment said that the call takes about 440 seconds in
the worst case. That count took a readiness wait as 100 seconds. A
readiness wait is up to 11 probes of /readyz of at most 3 seconds each,
with 10 sleeps of 10 seconds between them: 133 seconds.

A files-only change of the authentication config can take 536 seconds:
up to 30 seconds for the read of the metrics before the write, 120
seconds for the reload and 133 seconds for readiness, then 120 and 133
seconds again for a restore. That is more than the 480 seconds of the
deadline. A manifest change can take 466 seconds plus its reads of the
mirror pod, which have no time limit of their own.

Raise StartToCloseTimeout to 10 minutes. ScheduleToCloseTimeout and
HeartbeatTimeout stay as they are. Correct the numbers in the comment
of the activity options, and in the comment of the reload limit in the
agent.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

* fix(agent): accept the restored file after a rollback

After a rollback of the authentication config, the agent waited until
kube-apiserver reported the hash that it had loaded before the write.
That hash is not always the hash of the file on the node. When
kube-apiserver had another config loaded than that file, and then
loaded the staged content, the rollback writes the file of the node
back and kube-apiserver loads it. The hash from before the write does
not come back. The wait ran to its limit of two minutes, and the call
reported OUTCOME_FAILED_NOT_RESTORED for a node that no longer used the
staged content.

The wait now also ends with success when kube-apiserver reports the
hash of the file that the rollback wrote back. The agent computes that
hash before the write, from the content that the write replaces, in the
same way as the hash of the staged content. When the call created the
file, the rollback removes it, and only the hash from before the write
counts, as before.

The design page now names the two hashes that the wait accepts.

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>

---------

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>
2026-12-01 20:58:20 -05:00
..