Files
Davanum Srinivas f91b3e1b24 feat(agent): add a control plane endpoint health gate (#2514)
* feat(agent): add a control plane endpoint health gate

The agent can take a control plane node out of the cluster endpoint
before an operation, but only when the node runs kube-vip. A node behind
a load balancer that it does not run has no kube-vip, so the withdrawal
did nothing there. Connections through the load balancer stayed on the
node while its API server or its etcd restarted, and worker kubelets on
those connections went NotReady.

Add a second way to withdraw a node, a health gate. The gate is a file,
and the health check of the load balancer has to fail while the file
exists. NodeAgentServer.ControlPlaneEndpointGate configures it. The zero
value means no gate, and the agent then behaves as before. No flag sets
the field yet.

withdrawKubeVipLocked closes the gate first. It reads the IPv4 address
of the backend interface, writes the gate file, waits the settle time,
closes the established sockets of remote clients on that address and
port 6443 with the existing ss -K code, and runs the existing quiet
window check on that address. A failed check fails the withdrawal.

restoreKubeVipLocked opens the gate after kube-vip is back. So
StartKubeVipRestore opens the gate, and the etcd defragmentation gets
the gate through the two calls that it already makes. A node with
kube-vip and a gate leaves both, and returns to both.

The gate file names the park directory of the flow that closed it. The
directory decides who opens the gate, as it decides who restores a
parked manifest. A caller's restore opens each gate. The etcd
defragmentation opens only a gate that it closed, and it leaves a gate
that a caller closed as it is.

The backend interface must have one global IPv4 address. IPv6 is not
handled, as for kube-vip.

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

* feat(agent): open or hand off a gate that an etcd defrag left closed

The etcd defragmentation closes the control plane endpoint gate and
opens it in the same call. Two cases left the gate closed with nobody to
open it.

The agent can stop in the middle of a defragmentation. The repair of
stale state at startup now opens a gate that names the park directory of
the defragmentation, after it has put the parked manifests back. A gate
that a caller closed names another directory and stays closed.

The defragmentation can leave the node out of service on purpose, when
etcd does not come back healthy. It then hands the parked manifests to
the caller. It now hands the gate over with them, so the next start of
the agent does not open the gate, and StartKubeVipRestore does.

The tests of the defragmentation with a gate are in this commit, because
one of their cases is the hand-off.

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

* feat(agent): add the control plane endpoint gate flags

Three flags configure the control plane endpoint gate:

  --control-plane-endpoint-gate-file          path of the gate file
  --control-plane-endpoint-backend-interface  interface on which the
                                              connections of the load
                                              balancer arrive
  --control-plane-endpoint-gate-settle        settle time, default 10s

With no gate file the agent has no gate. The agent does not start with a
gate file and no backend interface, with a backend interface and no gate
file, with a relative gate file path, or with a negative settle time.

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

* docs(agent): describe the control plane endpoint gate

The agent README lists the three flags, and says what the deployment has
to provide and who opens a closed gate.

Two pages were wrong with the gate. The agent design page said that the
graceful shutdown and StartKubeVipRestore handle only kube-vip. The
certificate renewal page said that a node without kube-vip skips the
drain.

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

* feat(agent): close the control plane endpoint gate around kubeadm upgrade apply

KubeadmUpgradeApply runs kubeadm upgrade apply, which restarts etcd and
the API server of the node. The control plane endpoint gate did not
cover this call. The load balancer took the node out only after its
health check had failed, and in that time it still sent calls to an API
server that could not answer them.

With a gate, the call now takes the node out of the load balancer after
kubeadm upgrade plan and before kubeadm upgrade apply, in the way the
graceful shutdown does it. It opens the gate when kubeadm returns, also
when kubeadm fails. A node that cannot leave the load balancer does not
start the apply, and the call fails.

The call parks no manifest, so the gate file names the call and not a
park directory. A gate with that name is one that the agent opens, as it
opens a gate of the etcd defragmentation. The repair of stale state at
startup therefore opens it too, when the agent stopped in the middle of
the call.

The gate stays open on a cluster with one control plane node, which the
agent reads as one voting member of etcd. kubeadm reaches the cluster
through the load balancer, so with the only node out of it the apply
would fail. The gate also stays open when the members cannot be listed.

The call holds the lock of the static pod changes from the close to the
open. A second call, which a caller sends when the first one takes too
long, then waits and does not open the gate under the first.

The call has no operation record, so it logs its gate steps as the
repair at startup does. The log line of those steps no longer names the
repair: it is now "control plane step outside an operation".

With no gate file the call runs the same two commands as before and
nothing else.

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

* docs(agent): describe the gate of kubeadm upgrade apply

The agent README says when KubeadmUpgradeApply closes and opens the
control plane endpoint gate, that the call does not withdraw kube-vip,
and why the gate stays open on a cluster with one control plane node.
The list of gate file contents has the name that this call writes.

The README also says that the other calls that restart the API server
do not touch the gate, and that their caller closes and opens it.

The agent design page names the call next to the graceful shutdown.

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

* fix(agent): reject a directory as the control plane endpoint gate file

The agent accepted a gate file path that is an existing directory. It
started, and each later withdrawal failed: the agent renames the gate
file into place, and a rename cannot replace a directory. An operation
of the agent that reads the gate first failed at the read.

The check of the gate flags now rejects such a path at startup, like
the other gate settings that cannot work. A path with no file is still
accepted. No file is the open gate, so the file is absent at each
normal start.

The test of the gate flags has a case for an existing directory and a
case for an absent file in an existing directory.

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

* fix(agent): stage the control plane endpoint gate file under a private name

The agent staged the gate file at the gate path plus ".tmp" and wrote
it with os.WriteFile, which follows a symbolic link. The agent runs as
root. A user who can write to the folder of the gate could leave a link
at that name. The agent then wrote the owner of the gate into the file
that the link names, and the rename put the link at the gate path.

The gate file now goes through atomicWriteFile, as the other files of
this package that are renamed into place. That helper creates the
staged file itself, with a name that nobody knows before, writes
through the open file and removes it when a step fails. The rename
replaces a link at the gate path and does not follow it. The gate no
longer uses the FileWriter of the server. The tests fail the write
through a function of the server that stands in for the helper.

Startup does not refuse a folder that another user can write to. The
folder often does not exist before the first close, and its mode does
not say enough on a node image that has the folder on a mounted volume.
The README now names such a folder as a thing the deployment has to
provide.

A new test puts a link at the old staging name and at the gate path.
It checks that the file behind the link is unchanged and that the gate
file is a regular file with mode 0644.

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

* fix(agent): keep parked manifests with the gate when its hand-off fails

A defragmentation that gives up on its etcd member hands the gate and
the parked manifests to the caller. The hand-off recorded a failure of
the gate and still moved the manifests. The gate file then named the
defragmentation, and the caller held the manifests. At its next start
the agent opened that gate, because the gate file named a flow of the
agent, while kube-apiserver was still parked for the caller.

The hand-off now does the gate first and stops when the gate fails.
No manifest moves. The gate and the manifests all stay with the
defragmentation, and the defragmentation reports the failure as
before. At its next start the agent puts the manifests back and then
opens the gate, as it does for a defragmentation that was interrupted.

A new test runs a defragmentation whose member does not return and
whose gate file cannot be written again. It checks that the gate and
kube-apiserver stay with the defragmentation, and that the next start
puts kube-apiserver back and opens the gate.

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

* fix(agent): list the etcd members of kubeadm upgrade apply under the lock

With a gate, KubeadmUpgradeApply listed the members of etcd before it
took the lock of the static pod changes. When the list failed, it ran
kubeadm upgrade apply with no lock. The apply of a first call restarts
the local etcd. A second call, which a caller sends when the first one
takes too long, could then fail its list and run a second kubeadm
upgrade apply next to the first.

The call now takes the lock before it lists the members. A second call
waits for the first, and lists the members when the first apply is
over. The apply on a cluster with one control plane node and the apply
with members that cannot be listed still leave the gate open, and they
now run under the lock too. With no gate file the call takes no lock,
as before.

The helpers that run under the lock do not take it themselves, and the
defragmentation already lists the members under the same lock.

A new test holds a first call in its apply, fails the member list and
sends a second call. It checks that the second apply does not start
while the first one runs. The cases for one control plane node and for
a failed member list now expect the lock during the apply.

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

* docs(agent): say what kubeadm upgrade apply does with a gate that exists

The agent README and the agent design page said that
KubeadmUpgradeApply removes the gate file when kubeadm returns, and
that the gate stays open with one control plane node or with members
that cannot be listed. Both say more than the code does.

The call removes only a gate file that it wrote. A gate that a caller
closed stays closed until the caller opens it with StartKubeVipRestore,
and the call then closes no connection. With one voting member of etcd,
or with members that it cannot list, the call does not use the gate. It
creates no gate file and leaves one that exists as it is.

The two pages now say this. The cases of the upgrade test for a gate of
a caller, for one control plane node and for a failed member list show
the same behavior.

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

* fix(agent): reject a gate file path that names a directory by its form

Startup refused a gate file that is an existing directory. A path that
ends with a separator, or whose last element is "." or "..", passed
that check while nothing existed there. At the first close of the gate
the agent creates the folder of the gate file, which for such a path is
the gate path itself. The rename of the staged file then fails, and a
directory stays at the gate path.

Startup now refuses such a path with the same message. A path of an
absent file with no such ending still passes.

Three new cases of TestControlPlaneEndpointGateFlags have a path that
ends with a separator, with "/." and with "/..". They fail without the
new check.

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

---------

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>
2026-11-30 14:01:59 -05:00
..