Skip to content

Commit e76e1eb

Browse files
authored
fix(linkerd-cni): improve SA token rotation detection (#478)
* fix(linkerd-cni): improve SA token rotation detection This makes the logic introduced in #440 more robust, by watching over the proper file change to trigger the kubeconfig file re-creation. See linkerd/linkerd2#12573 (comment)
1 parent 659da44 commit e76e1eb

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

cni-plugin/deployment/scripts/install-cni.sh

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,21 +277,26 @@ monitor_cni_config() {
277277
done
278278
}
279279
280-
# Kubernetes rolls out serviceaccount tokens by creating new directories
281-
# containing a new token file and re-creating the
282-
# /var/run/secrets/kubernetes.io/serviceaccount/token symlink pointing to it.
283-
# This function listens to creation events under the serviceaccount directory,
284-
# only reacting to direct creation of a "token" file, or creation of
285-
# directories containing a "token" file.
280+
# This function detects whether the service account token was rotated by
281+
# listening to MOVED_TO events under the directory
282+
# /var/run/secrets/kubernetes.io/serviceaccount, detecting whether the ..data
283+
# directory was moved to, as recommended by k8s' atomic writer:
284+
# > Consumers of the target directory can monitor the ..data symlink using
285+
# > inotify or fanotify to receive events when the content in the volume is
286+
# > updated.
287+
# Indeed, as per atomic writer's Write function docs, in the final steps the
288+
# ..data_tmp symlink points to a new timestamped directory containing the new
289+
# files, which is then atomically renamed to ..data:
290+
# > 8. A symlink to the new timestamped directory ..data_tmp is created that will
291+
# > become the new data directory.
292+
# > 9. The new data directory symlink is renamed to the data directory; rename is atomic.
293+
# See https://github.com/kubernetes/kubernetes/blob/release-1.32/pkg/volume/util/atomic_writer.go
286294
monitor_service_account_token() {
287-
inotifywait -m "${SERVICEACCOUNT_PATH}" -e create |
288-
while read -r directory _ filename; do
289-
target=$(realpath "$directory/$filename")
290-
if [[ (-f "$target" && "${target##*/}" == "token") || (-d "$target" && -e "$target/token") ]]; then
291-
log "Detected creation of file in $directory: $filename; recreating kubeconfig file"
292-
create_kubeconfig
293-
else
294-
log "Detected creation of file in $directory: $filename; ignoring"
295+
inotifywait -m "${SERVICEACCOUNT_PATH}" -e moved_to |
296+
while read -r _ _ filename; do
297+
if [[ "$filename" == "..data" ]]; then
298+
log "Detected change in service account files; recreating kubeconfig file"
299+
create_kubeconfig
295300
fi
296301
done
297302
}

0 commit comments

Comments
 (0)