@@ -286,38 +286,45 @@ EOF
286286}
287287
288288function makeLocalLeaseAndReplaceRemote {
289- # TODO remote set +x/-x
290- set -x
289+ # Replace the remote lease with a new lease that we own
290+ # It's assumed that it's already determined it's safe to try and get the lease
291+ # (either the lease is unowned, expired, or owned by us).
292+ #
293+ # TBD: There's a small race where this call temporarily deletes the lease before
294+ # it replaces it with a new one,
295+ # which means someone else could come in and snipe it even if we already
296+ # own an older version of the lease and the older version hasn't expired.
297+ # If this happens, this call will fail when it tries to 'checkLease'
298+ # and the caller therefore is forced to give up their lease. In theory,
299+ # this race could be resolved by using a 'replace -f' pattern - but this
300+ # failed with unexpected errors on some kubectl setups but not others.
301+ #
302+
291303 makeLocalLease
292304 if [ $? -ne 0 ]; then
293305 traceError " failed - could not generate a new local lease"
294306 return 1
295307 fi
296308
297- local tempcf=${LOCAL_ROOT} /tempcf.yaml
298-
299- # next, try replace remote lease with the candidate lease
300- kubectl create configmap ${CONFIGMAP_NAME} --from-file ${LOCAL_ROOT} /${LOCAL_FILE} -o yaml -n default --dry-run > $tempcf
309+ deleteRemoteLeaseUnsafe
301310 if [ $? -ne 0 ]; then
302- traceError " failed - could not generate config map yaml "
311+ traceError " failed - could not delete remote lease "
303312 return 1
304313 fi
305314
306- kubectl replace -f $tempcf --validate=false
315+ kubectl create configmap ${CONFIGMAP_NAME} --from-file ${LOCAL_ROOT} / ${LOCAL_FILE} -n default
307316 if [ $? -ne 0 ]; then
308- traceError " failed - could not get replace remote lease "
317+ traceError " failed - could not replace"
309318 return 1
310319 fi
311320
312321 # finally, check if we now actually own the lease (someone could have been replacing at the same time)
313322 checkLease
314- if [ $? -eq 0 ]; then
315- return 0
316- else
323+ if [ $? -ne 0 ]; then
317324 traceError " failed - replaced remote lease, but we somehow lost a race or can no longer communicate with kubernetes"
318325 return 1
319326 fi
320- set +x
327+ return 0
321328}
322329
323330function getRemoteLease {
0 commit comments