@@ -286,38 +286,45 @@ EOF
286
286
}
287
287
288
288
function 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
+
291
303
makeLocalLease
292
304
if [ $? -ne 0 ]; then
293
305
traceError " failed - could not generate a new local lease"
294
306
return 1
295
307
fi
296
308
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
301
310
if [ $? -ne 0 ]; then
302
- traceError " failed - could not generate config map yaml "
311
+ traceError " failed - could not delete remote lease "
303
312
return 1
304
313
fi
305
314
306
- kubectl replace -f $tempcf --validate=false
315
+ kubectl create configmap ${CONFIGMAP_NAME} --from-file ${LOCAL_ROOT} / ${LOCAL_FILE} -n default
307
316
if [ $? -ne 0 ]; then
308
- traceError " failed - could not get replace remote lease "
317
+ traceError " failed - could not replace"
309
318
return 1
310
319
fi
311
320
312
321
# finally, check if we now actually own the lease (someone could have been replacing at the same time)
313
322
checkLease
314
- if [ $? -eq 0 ]; then
315
- return 0
316
- else
323
+ if [ $? -ne 0 ]; then
317
324
traceError " failed - replaced remote lease, but we somehow lost a race or can no longer communicate with kubernetes"
318
325
return 1
319
326
fi
320
- set +x
327
+ return 0
321
328
}
322
329
323
330
function getRemoteLease {
0 commit comments