|
285 | 285 | fi
|
286 | 286 | }
|
287 | 287 |
|
| 288 | +function makeLocalLeaseAndReplaceRemote { |
| 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 | + |
| 303 | + makeLocalLease |
| 304 | + if [ $? -ne 0 ]; then |
| 305 | + traceError "failed - could not generate a new local lease" |
| 306 | + return 1 |
| 307 | + fi |
| 308 | + |
| 309 | + deleteRemoteLeaseUnsafe |
| 310 | + if [ $? -ne 0 ]; then |
| 311 | + traceError "failed - could not delete remote lease" |
| 312 | + return 1 |
| 313 | + fi |
| 314 | + |
| 315 | + kubectl create configmap ${CONFIGMAP_NAME} --from-file ${LOCAL_ROOT}/${LOCAL_FILE} -n default |
| 316 | + if [ $? -ne 0 ]; then |
| 317 | + traceError "failed - could not replace" |
| 318 | + return 1 |
| 319 | + fi |
| 320 | + |
| 321 | + # finally, check if we now actually own the lease (someone could have been replacing at the same time) |
| 322 | + checkLease |
| 323 | + if [ $? -ne 0 ]; then |
| 324 | + traceError "failed - replaced remote lease, but we somehow lost a race or can no longer communicate with kubernetes" |
| 325 | + return 1 |
| 326 | + fi |
| 327 | + return 0 |
| 328 | +} |
| 329 | + |
288 | 330 | function getRemoteLease {
|
289 | 331 | #
|
290 | 332 | # first, if the remote lease configmap doesn't exist
|
@@ -395,25 +437,11 @@ function obtainLease {
|
395 | 437 | # so assume it can be replaced and we can try takeover the lease
|
396 | 438 |
|
397 | 439 | # first make a local candidate lease
|
398 |
| - makeLocalLease |
399 |
| - if [ $? -ne 0 ]; then |
400 |
| - traceError "failed - could not generate a new local lease" |
401 |
| - return 1 |
402 |
| - fi |
403 |
| - |
404 |
| - # next, try replace remote lease with the candidate lease |
405 |
| - kubectl create configmap ${CONFIGMAP_NAME} --from-file ${LOCAL_ROOT}/${LOCAL_FILE} -o yaml -n default --dry-run | kubectl replace -f - |
406 |
| - if [ $? -ne 0 ]; then |
407 |
| - traceError "failed - could not replace remote lease" |
408 |
| - return 1 |
409 |
| - fi |
410 |
| - |
411 |
| - # finally, check if we now actually own the lease (someone could have been replacing at the same time) |
412 |
| - checkLease |
| 440 | + makeLocalLeaseAndReplaceRemote |
413 | 441 | if [ $? -eq 0 ]; then
|
414 | 442 | return 0
|
415 | 443 | else
|
416 |
| - traceError "failed - replaced remote lease, but kubernetes is not responding or we lost a race and another potential owner replaced it too, will keep retrying up to the timeout" |
| 444 | + traceError "failed to replace remote lease, will keep retrying up to the timeout" |
417 | 445 | fi
|
418 | 446 | fi
|
419 | 447 | local mnow=`date +%s`
|
@@ -447,26 +475,12 @@ function renewLease {
|
447 | 475 | fi
|
448 | 476 |
|
449 | 477 | # now make a new local candidate lease
|
450 |
| - makeLocalLease |
451 |
| - if [ $? -ne 0 ]; then |
452 |
| - traceError "failed - could not generate a new local lease" |
453 |
| - return 1 |
454 |
| - fi |
455 |
| - |
456 |
| - # next, try replace remote lease with the candidate lease |
457 |
| - kubectl create configmap ${CONFIGMAP_NAME} --from-file ${LOCAL_ROOT}/${LOCAL_FILE} -o yaml -n default --dry-run | kubectl replace -f - |
| 478 | + makeLocalLeaseAndReplaceRemote |
458 | 479 | if [ $? -ne 0 ]; then
|
459 |
| - traceError "failed - could not get replace remote lease" |
| 480 | + traceError "failed to replace remote lease" |
460 | 481 | return 1
|
461 |
| - fi |
462 |
| - |
463 |
| - # finally, check if we now actually own the lease (someone could have been replacing at the same time) |
464 |
| - checkLease |
465 |
| - if [ $? -eq 0 ]; then |
466 |
| - return 0 |
467 | 482 | else
|
468 |
| - traceError "failed - replaced remote lease, but we somehow lost a race or can no longer communicate with kubernetes" |
469 |
| - return 1 |
| 483 | + return 0 |
470 | 484 | fi
|
471 | 485 | }
|
472 | 486 |
|
|
0 commit comments