Skip to content

Commit 5f365f3

Browse files
committed
sidecar{,-rs}: cleanup lease directories on start
the primary crash scenario for the sidecar is if for whatever reason a stale lease is found. we crash out without cleaning up the stale lease, leading to a crashloop when it is checked again. clear the lease directories on start for a fresh slate.
1 parent fa2945b commit 5f365f3

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

containers/sidecar-rs/src/main.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ fn vault_fetch_consul_token(
397397
Ok(())
398398
}
399399

400+
fn clear_leases_dir(leases_dir: &Path) -> Result<()> {
401+
for entry in fs::read_dir(leases_dir)?.flatten() {
402+
if entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
403+
eprintln!("Removing old lease file: {}", entry.file_name().to_string_lossy());
404+
fs::remove_file(entry.path())?;
405+
}
406+
}
407+
Ok(())
408+
}
409+
400410
// --- Shared command logic ---
401411

402412
#[allow(clippy::too_many_arguments)]
@@ -446,7 +456,14 @@ fn do_kube_login(
446456
}
447457

448458
let vault_secrets = Path::new(vault_secrets_path);
449-
fs::create_dir_all(vault_secrets.join("leases"))?;
459+
let lease_dirs: HashSet<PathBuf> = [vault_secrets_path, consul_secrets_path, cert_dir]
460+
.iter()
461+
.map(|p| Path::new(p).join("leases"))
462+
.collect();
463+
for lease_dir in &lease_dirs {
464+
fs::create_dir_all(lease_dir)?;
465+
clear_leases_dir(lease_dir)?;
466+
}
450467

451468
let jwt = fs::read_to_string("/var/run/secrets/kubernetes.io/serviceaccount/token")
452469
.context("reading kubernetes service account token")?;
@@ -492,7 +509,6 @@ fn do_kube_login(
492509

493510
if fetch_consul_token {
494511
let consul_path = Path::new(consul_secrets_path);
495-
fs::create_dir_all(consul_path.join("leases"))?;
496512
vault_fetch_consul_token(
497513
client,
498514
vault_addr,
@@ -505,7 +521,6 @@ fn do_kube_login(
505521

506522
if fetch_cert {
507523
let cert_path = Path::new(cert_dir);
508-
fs::create_dir_all(cert_path.join("leases"))?;
509524
vault_fetch_certificate(
510525
client,
511526
vault_addr,

containers/sidecar/sidecar.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ def vault_fetch_consul_token(
364364
write_consul_token(consul_secrets_path, consul_token_response.json())
365365

366366

367+
def _clear_leases_dir(leases_dir):
368+
for entry in os.listdir(leases_dir):
369+
path = os.path.join(leases_dir, entry)
370+
if os.path.isfile(path):
371+
click.echo(f"Removing old lease file: {entry}")
372+
os.remove(path)
373+
374+
367375
# --- Shared logic for CLI commands ---
368376

369377

@@ -419,7 +427,12 @@ def do_kube_login(
419427
" unwrapped token must be accessible during bootstrap"
420428
)
421429

422-
os.makedirs(os.path.join(vault_secrets_path, "leases"), exist_ok=True)
430+
for lease_dir in set(
431+
os.path.join(x, "leases")
432+
for x in [vault_secrets_path, consul_secrets_path, cert_dir]
433+
):
434+
os.makedirs(lease_dir, exist_ok=True)
435+
_clear_leases_dir(lease_dir)
423436
with open("/var/run/secrets/kubernetes.io/serviceaccount/token", "r") as token_file:
424437
jwt = token_file.read()
425438

@@ -452,7 +465,6 @@ def do_kube_login(
452465
vault_token_file.write(token_contents)
453466

454467
if fetch_consul_token:
455-
os.makedirs(os.path.join(consul_secrets_path, "leases"), exist_ok=True)
456468
vault_fetch_consul_token(
457469
vault_addr,
458470
token_contents,
@@ -462,7 +474,6 @@ def do_kube_login(
462474
consul_secrets_path,
463475
)
464476
if fetch_cert:
465-
os.makedirs(os.path.join(cert_dir, "leases"), exist_ok=True)
466477
vault_fetch_certificate(
467478
vault_addr,
468479
token_contents,

0 commit comments

Comments
 (0)