Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce support for Projected Volumes kubernetes ServiceAccount Tokens #416

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions cni-plugin/deployment/scripts/install-cni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,24 @@ sync() {
# previously observed SHA (updated with each file watch) and compare it
# against the new file's SHA. If they differ, it means something has
# changed.
new_sha=$(sha256sum "${filepath}" | while read -r s _; do echo "$s"; done)
if [ "$new_sha" != "$prev_sha" ]; then
# Create but don't rm old one since we don't know if this will be configured
# to run as _the_ cni plugin.
log "New file [$filename] detected; re-installing"
install_cni_conf "$filepath"
if [[ -n "$prev_sha" ]]; then
new_sha=$(sha256sum "${filepath}" | while read -r s _; do echo "$s"; done)
if [ "$new_sha" != "$prev_sha" ]; then
# Create but don't rm old one since we don't know if this will be configured
# to run as _the_ cni plugin.
log "New file [$filename] detected; re-installing"
install_cni_conf "$filepath"
else
# If the SHA hasn't changed or we get an unrecognised event, ignore it.
# When the SHA is the same, we can get into infinite loops whereby a file has
# been created and after re-install the watch keeps triggering CREATE events
# that never end.
log "Ignoring event: $ev $filepath; no real changes detected"
fi
else
# If the SHA hasn't changed or we get an unrecognised event, ignore it.
# When the SHA is the same, we can get into infinite loops whereby a file has
# been created and after re-install the watch keeps triggering CREATE events
# that never end.
log "Ignoring event: $ev $filepath; no real changes detected"
# If there is no previous SHA (as in the case of the token), reinstall cni.
log "Processing event for $filename with no SHA comparison needed."
install_cni_conf "$DEFAULT_CNI_CONF_PATH"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't be sure at this point if the config is held on $DEFAULT_CNI_CONF_PATH, which is a .conf file, or a .conflist file.

fi
fi
}
Expand All @@ -298,6 +304,11 @@ monitor() {
cni_conf_sha="$(sha256sum "$directory/$filename" | while read -r s _; do echo "$s"; done)"
fi
fi
done &
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to be careful with backgrounding these tasks -- we want to ensure that inotifywait failures are propagated so that the program can fail if any of these calls fail.

There are other changes we probably need to make to this script first, but we'll want to ensure that we preserve PIDs and wait on them to surface errors.

inotifywait -m "/var/run/secrets/kubernetes.io/serviceaccount/token" -e create,delete,moved_to,modify |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In create_cni_conf() we have SERVICE_ACCOUNT_PATH=/var/run/secrets/kubernetes.io/serviceaccount. Can you make that a global variable, so we can reuse it here?

while read -r directory action filename; do
log "Detected change in Service Account token: $action $filename"
sync "$DEFAULT_CNI_CONF_PATH" "$action" ""
done
}

Expand Down
Loading