@@ -56,7 +56,7 @@ HOST_CNI_NET="${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}"
5656# Location of legacy "interface mode" file, to be automatically deleted
5757DEFAULT_CNI_CONF_PATH=" ${HOST_CNI_NET} /01-linkerd-cni.conf"
5858KUBECONFIG_FILE_NAME=${KUBECONFIG_FILE_NAME:- ZZZ-linkerd-cni-kubeconfig}
59- SERVICEACCOUNT_PATH =/var/run/secrets/kubernetes.io/serviceaccount
59+ SERVICE_ACCOUNT_PATH =/var/run/secrets/kubernetes.io/serviceaccount
6060
6161# ###########################
6262# ## Function definitions ###
@@ -121,31 +121,32 @@ install_cni_bin() {
121121}
122122
123123create_kubeconfig () {
124- KUBE_CA_FILE=${KUBE_CA_FILE:- ${SERVICEACCOUNT_PATH } / ca.crt}
124+ KUBE_CA_FILE=${KUBE_CA_FILE:- ${SERVICE_ACCOUNT_PATH } / ca.crt}
125125 SKIP_TLS_VERIFY=${SKIP_TLS_VERIFY:- false}
126- SERVICEACCOUNT_TOKEN=$( cat ${SERVICEACCOUNT_PATH} /token)
127-
128- # Check if we're not running as a k8s pod.
129- if [[ ! -f " ${SERVICEACCOUNT_PATH} /token" ]]; then
130- return
131- fi
132-
133- if [ -z " ${KUBERNETES_SERVICE_HOST} " ]; then
134- log ' KUBERNETES_SERVICE_HOST not set' ; exit 1;
135- fi
136- if [ -z " ${KUBERNETES_SERVICE_PORT} " ]; then
137- log ' KUBERNETES_SERVICE_PORT not set' ; exit 1;
138- fi
126+ # Pull out service account token.
127+ SERVICEACCOUNT_TOKEN=$( cat ${SERVICE_ACCOUNT_PATH} /token)
128+
129+ # Check if we're running as a k8s pod.
130+ # The check will assert whether token exists and is a regular file
131+ if [ -f " ${SERVICE_ACCOUNT_PATH} /token" ]; then
132+ # We're running as a k8d pod - expect some variables.
133+ # If the variables are null, exit
134+ if [ -z " ${KUBERNETES_SERVICE_HOST} " ]; then
135+ log ' KUBERNETES_SERVICE_HOST not set' ; exit 1;
136+ fi
137+ if [ -z " ${KUBERNETES_SERVICE_PORT} " ]; then
138+ log ' KUBERNETES_SERVICE_PORT not set' ; exit 1;
139+ fi
139140
140- if [ " ${SKIP_TLS_VERIFY} " = ' true' ]; then
141- TLS_CFG=' insecure-skip-tls-verify: true'
142- elif [ -f " ${KUBE_CA_FILE} " ]; then
143- TLS_CFG=" certificate-authority-data: $( base64 " ${KUBE_CA_FILE} " | tr -d ' \n' ) "
144- fi
141+ if [ " ${SKIP_TLS_VERIFY} " = ' true' ]; then
142+ TLS_CFG=' insecure-skip-tls-verify: true'
143+ elif [ -f " ${KUBE_CA_FILE} " ]; then
144+ TLS_CFG=" certificate-authority-data: $( base64 " ${KUBE_CA_FILE} " | tr -d ' \n' ) "
145+ fi
145146
146- touch " ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR} /${KUBECONFIG_FILE_NAME} "
147- chmod " ${KUBECONFIG_MODE:- 600} " " ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR} /${KUBECONFIG_FILE_NAME} "
148- cat > " ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR} /${KUBECONFIG_FILE_NAME} " << EOF
147+ touch " ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR} /${KUBECONFIG_FILE_NAME} "
148+ chmod " ${KUBECONFIG_MODE:- 600} " " ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR} /${KUBECONFIG_FILE_NAME} "
149+ cat > " ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR} /${KUBECONFIG_FILE_NAME} " << EOF
149150# Kubeconfig file for linkerd CNI plugin.
150151apiVersion: v1
151152kind: Config
@@ -165,6 +166,8 @@ contexts:
165166 user: linkerd-cni
166167current-context: linkerd-cni-context
167168EOF
169+
170+ fi
168171}
169172
170173create_cni_conf () {
@@ -239,7 +242,14 @@ sync() {
239242
240243 local config_file_count
241244 local new_sha
242- if [ " $ev " = ' CREATE' ] || [ " $ev " = ' MOVED_TO' ] || [ " $ev " = ' MODIFY' ]; then
245+ if [ " $ev " = ' DELETE' ]; then
246+ # When the event type is 'DELETE', we check to see if there are any `*conf` or `*conflist`
247+ # files on the host's filesystem.
248+ config_file_count=$( find " ${HOST_CNI_NET} " -maxdepth 1 -type f \( -iname ' *conflist' -o -iname ' *conf' \) | sort | wc -l)
249+ if [ " $config_file_count " -eq 0 ]; then
250+ log " No active CNI configuration file found after $ev event"
251+ fi
252+ elif [ " $ev " = ' CREATE' ] || [ " $ev " = ' MOVED_TO' ] || [ " $ev " = ' MODIFY' ]; then
243253 # When the event type is 'CREATE', 'MOVED_TO' or 'MODIFY', we check the
244254 # previously observed SHA (updated with each file watch) and compare it
245255 # against the new file's SHA. If they differ, it means something has
@@ -264,13 +274,14 @@ sync() {
264274
265275# monitor_cni_config starts a watch on the host's CNI config directory
266276monitor_cni_config () {
267- inotifywait -m " ${HOST_CNI_NET} " -e create,moved_to,modify |
277+ inotifywait -m " ${HOST_CNI_NET} " -e create,delete, moved_to,modify |
268278 while read -r directory action filename; do
269279 if [[ " $filename " =~ .* .(conflist| conf)$ ]]; then
270280 log " Detected change in $directory : $action $filename "
271281 sync " $filename " " $action " " $cni_conf_sha "
272- # calculate file SHA to use in the next iteration
273- if [[ -e " $directory /$filename " ]]; then
282+ # When file exists (i.e we didn't deal with a DELETE ev)
283+ # then calculate its sha to be used the next turn.
284+ if [[ -e " $directory /$filename " && " $action " != ' DELETE' ]]; then
274285 cni_conf_sha=" $( sha256sum " $directory /$filename " | while read -r s _; do echo " $s " ; done)"
275286 fi
276287 fi
@@ -284,7 +295,7 @@ monitor_cni_config() {
284295# only reacting to direct creation of a " token" file, or creation of
285296# directories containing a " token" file.
286297monitor_service_account_token() {
287- inotifywait -m " ${SERVICEACCOUNT_PATH } " -e create |
298+ inotifywait -m " ${SERVICE_ACCOUNT_PATH } " -e create |
288299 while read -r directory _ filename; do
289300 target=$( realpath " $directory /$filename " )
290301 if [[ (-f " $target " && " ${target##*/ } " == " token" ) || (-d " $target " && -e " $target /token" ) ]]; then
0 commit comments