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
..

agent

gRPC agent that runs as a systemd service on each NVKE node. An orchestrator calls it to drive kubeadm lifecycle, package/CNI install, API encryption, and related node-local operations — without interactive SSH.

Doc Purpose
AGENTS.md Build, test, proto, versioning, and contributor conventions
docs/DESIGN.md Architecture and API surface
docs/SECURITY.md Security context (reporting: repo-root SECURITY.md)
pkg/agent/proto/node_agent.proto Canonical RPC definitions
config.env.example Build/runtime artifact endpoint knobs

Module: github.com/dsx-ai-factory/nvke/agent

Quick start

# From the monorepo root
./build/agent.sh test
./build/agent.sh build

# Or from this directory
make test
make build

Install a release tarball on a node (as root):

  1. Extract the package on the host
  2. Run scripts/agent-installer.sh (or the bundled installer from the release)
  3. Configure flags via /etc/default/nvke-agent (see below)

Configuration

Copy config.env.example → config.env (gitignored) for local build/publish overrides. On nodes, set flags via /etc/default/nvke-agent.

Important flags (also settable via env; see AGENT_ARGS):

Flag Notes
--bundle-server-url Optional. Bundle source for InstallBundle (https://, http://, file://, or oci://). Also NKE_BUNDLE_SERVER_URL. Prefer apt when unset.
--index-file-url Optional absolute index URL (NKE_BUNDLE_INDEX_URL); otherwise derived from the bundle server + OS
--cni-image-registry Optional OCI prefix for CNI helm global.registry (NKE_CNI_IMAGE_REGISTRY)
--disable-mtls Plaintext gRPC (development only)
--spiffe-socket-path SPIFFE workload API socket. Used for the gRPC server's mTLS unless --disable-mtls is set, and always by the SPIRE agent RPCs
--secure-server-port gRPC listen port (default 15005)
--control-plane-endpoint-gate-file Optional. Absolute path of the control plane endpoint gate file. Empty means no gate
--control-plane-endpoint-backend-interface Interface on which the connections of the load balancer arrive. Required with the gate file
--control-plane-endpoint-gate-settle Time between closing the gate and closing the established connections (default 10s)
--version Print build version and exit

Example:

# Apt-first (no bundle server required)
AGENT_ARGS="--disable-mtls"

# Optional: enable legacy HTTP/OCI bundle installs
AGENT_ARGS="--bundle-server-url https://artifacts.example.com/ --disable-mtls"
# OCI bundles:
# AGENT_ARGS="--bundle-server-url oci://registry.example.com/nvke/bundles --disable-mtls"

There is no built-in artifact host. When using bundles, consumers must point the agent at their own S3/HTTP or OCI layout. Index entries use paths relative to the bundle server URL (for example nke_bundles/Ubuntu_24.04/kubeadm_v1.30.0.tgz).

gRPC reflection is enabled on the same port (useful with grpcurl).

Control plane endpoint gate

A control plane node behind a load balancer that it does not run, for example a Service of another cluster, has no kube-vip to withdraw. With --control-plane-endpoint-gate-file set, the agent withdraws such a node through a gate file. With no gate file it withdraws only kube-vip.

On StartK8sControlPlaneNodeGracefulShutdown and StartEtcdDefrag the agent creates the gate file, waits the settle time, closes the established connections of remote clients on the IPv4 address of the backend interface and port 6443 (ss -K), and checks for 30 seconds that no new connection arrives there. A new connection fails the withdrawal. StartKubeVipRestore removes the gate file. A node with kube-vip and a gate leaves both, the gate first.

KubeadmUpgradeApply withdraws the node through the gate in the same way, after kubeadm upgrade plan and before kubeadm upgrade apply, which restarts etcd and the API server of the node. It removes the gate file when kubeadm upgrade apply returns, also when that command fails. A withdrawal that fails stops the call before kubeadm upgrade apply. This call does not withdraw kube-vip.

The call removes only a gate file that it wrote. It writes the file when the gate is open, and when the file names the agent as the one that opens the gate. A gate that a caller closed stays closed during the call and after it, until the caller opens it with StartKubeVipRestore. The call then waits no settle time and closes no connection.

On a cluster with one control plane node, which the agent reads as one voting member of etcd, the call does not use the gate: kubeadm reaches the cluster through the load balancer, and no other node is behind it. The call does not use the gate either when the agent cannot list the members of etcd. In both cases it creates no gate file, and it leaves a gate file that exists as it is.

Other calls that restart the API server, such as the certificate renewal, do not touch the gate. Their caller closes it first with StartK8sControlPlaneNodeGracefulShutdown and opens it with StartKubeVipRestore.

The agent cannot see the load balancer, so the deployment has to provide:

  • A health check that fails while the gate file exists, and that passes only when the file is gone and /readyz of the local API server answers 200. It must not connect to the backend address from another host.
  • A backend interface that carries only the connections of the load balancer. Clients in the cluster reach the API server on another address.
  • One global IPv4 address on the backend interface. IPv6 is not handled.
  • A kernel with INET_DIAG, INET_TCP_DIAG and INET_DIAG_DESTROY. Without them ss -K closes nothing and reports no error. The withdrawal then fails at the check for new connections, which also sees a connection that stayed.
  • A settle time longer than the health check needs to take the node out.
  • A folder for the gate file that only root can write to. A user who can write to that folder can close and open the gate without the agent. The agent does not check the folder.

The content of the gate file says who opens the gate:

  • /etc/kubernetes/manifests-backup: a caller, with StartKubeVipRestore. The gate stays closed until then, also over a restart of the agent. Over a reboot of the node it stays closed only when the gate file is on a path that the reboot does not clear.
  • /etc/kubernetes/manifests-backup/etcd-defrag: the agent, at the end of the defragmentation or at its next start.
  • kubeadm-upgrade-apply: the agent, when kubeadm upgrade apply returns or at its next start.
  • Other content, such as a file made by hand: a caller.

Logs

Path Purpose
/var/log/nvke-agent-audit.log Audit log (--audit-log-file); records are also mirrored to the service journal
/var/log/nvke-execute-scripts.log ExecuteScript / StreamExecuteScript results
journalctl -u nvke-agent Service journal

GetSupportBundle returns a tarball with state, journals, and related diagnostics.

Core capabilities

  • Kubeadm: init, join, reset, preflight, join strings, kubeconfig
  • CNI: InstallCNI after packages are present; prefer noCniInstall: true on init for new orchestration (legacy in-init CNI remains when noCniInstall is false)
  • Packages / collections: apt packages, apt repos, bundles, collections
  • Encryption: Kubernetes API/etcd encryption-at-rest
  • Control-plane ops: graceful shutdown / kube-vip restore, apiserver manifest patch
  • Utilities: script execution, file upload, support bundle

Full RPC list: pkg/agent/proto/node_agent.proto and docs/DESIGN.md.

Example: create a small cluster (grpcurl)

Examples below use obviously fake tokens, hashes, and keys so scanners do not treat them as live credentials. Replace with values from your environment.

# Init first control-plane (TEST-NET address 192.0.2.10 is documentation-only)
grpcurl -plaintext -d '{
  "podNetworkCidr": "10.244.0.0/16",
  "serviceCidr": "10.96.0.0/12",
  "noCniInstall": true
}' 192.0.2.10:15005 proto.NodeAgent.ExecuteKubeadmInit

# Poll status / join strings
grpcurl -plaintext 192.0.2.10:15005 proto.NodeAgent.GetKubeadmStatus
grpcurl -plaintext 192.0.2.10:15005 proto.NodeAgent.GetKubeadmJoinString

# Join another node (placeholders — not real kubeadm material)
grpcurl -plaintext -d '{
  "kubeadmJoinString": "join 192.0.2.10:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:0000000000000000000000000000000000000000000000000000000000000000 --control-plane --certificate-key 0000000000000000000000000000000000000000000000000000000000000000"
}' 192.0.2.11:15005 proto.NodeAgent.ExecuteKubeadmJoin

Install CNI after packages are on the control-plane host (InstallPackages), then:

grpcurl -plaintext -d '{
  "cniVariant": "CNI_VARIANT_OVN",
  "podNetworkCidr": "10.244.0.0/16",
  "serviceCidr": "10.96.0.0/12"
}' 192.0.2.10:15005 proto.NodeAgent.InstallCNI

More sample clients and manifests: samples/ and sample-clients/.

Encryption (sketch)

API encryption is a multi-step flow; see DESIGN.md and the proto comments. When posting example keys in docs or tickets, use clearly invalid material, e.g.:

encryptionKey: "EXAMPLEONLY_not-a-real-secretbox-key_000000000000="

Never paste production tokens, certificate keys, or encryption keys into the README.

mTLS

Production should use SPIFFE mTLS (default). --disable-mtls is for local/dev only. Clients need a matching SPIFFE identity (path prefix /nke-service/).

Build notes

make build          # linux/amd64 + linux/arm64
make package        # release tarballs under release/
make protos         # regenerate *.pb.go via buf
make setup-hooks    # monorepo-aware proto pre-commit hook

Version comes from git describe --tags --always --dirty --match 'v*', the repository's release tags (see AGENTS.md). Override with VERSION=... when needed.

Get the agent

The supported way to obtain the agent is to build it from source: make -C agent package for the release tarballs, or make -C agent deb-amd64 (deb-arm64) for the Debian package. Prebuilt agent binaries and packages in GHCR, NVCR and the apt intake are private internal artifacts today, not public downloads.

The reference OS image (os-image/) installs the .deb built from this checkout. The package spec and maintainer scripts live in os-image/packages/nvke-agent/packaging/; make -C agent deb-amd64 or deb-arm64 builds it with the shared pinned nfpm.

Internal maintainer publication

The publication pipeline's artifacts are private today. Its registries and apt intake are not a supported download path for external users. Maintainers can find the tag triggers, destinations and publication controls in the artifact publication reference.

Every commit on main and every vX.Y.Z release tag publishes the binaries to GHCR as OCI artifacts, joined by a multi-arch index (.github/workflows/agent-artifact.yml). Tags are edge, sha-<commit>, and vX.Y.Z, each with -amd64 / -arm64 single-platform variants underneath. A release tag gets the vX.Y.Z tag unless NVKE_GITHUB_PUBLISH leaves out agent; the sha-<commit> tag is pushed either way. These are binaries, not container images: the layer is the executable itself, so docker pull is not the way in.

Every vX.Y.Z and vX.Y.Z-beta.N tag also builds native amd64 and arm64 .debs, uploads them to the apt-publisher S3 intake (.github/workflows/agent-deb-publish.yml, via scripts/upload-s3-apt-intake.sh) and pushes them to NVCR. A vX.Y.Z tag also pushes them to GHCR unless NVKE_GITHUB_PUBLISH leaves out agent. A beta does not enter the intake's index.json. Main merges and branch workflow runs do not ingest — only refs/tags/v*.

The intake destination comes from the GitHub apt-intake environment (APT_INTAKE_S3_URI, APT_INTAKE_AWS_ROLE_ARN, …) — nothing bucket-specific is committed here. Once apt-publisher is pointed at that intake, cut over from the previous GitLab package source.