@@ -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
286294monitor_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