Skip to content

Commit 0ad78e3

Browse files
authored
Tidy DelWorkload handling in a pre-snapshot context (#1403)
* Fix this up so it's the inverse of `AddWorkload` Signed-off-by: Benjamin Leggett <[email protected]> * Test Signed-off-by: Benjamin Leggett <[email protected]> * lint Signed-off-by: Benjamin Leggett <[email protected]> * Fmt Signed-off-by: Benjamin Leggett <[email protected]> * Clarifying comment Signed-off-by: Benjamin Leggett <[email protected]> * Also make sure to prune `pending_workloads` during reconcile Signed-off-by: Benjamin Leggett <[email protected]> * Can't during reconcile, it's not in the state Signed-off-by: Benjamin Leggett <[email protected]> * Another test Signed-off-by: Benjamin Leggett <[email protected]> * wording Signed-off-by: Benjamin Leggett <[email protected]> --------- Signed-off-by: Benjamin Leggett <[email protected]>
1 parent a9563b2 commit 0ad78e3

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

src/inpod/statemanager.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl WorkloadProxyManagerState {
134134
));
135135
};
136136
if !self.snapshot_received {
137+
debug!("got workload add before snapshot");
137138
self.snapshot_names.insert(poddata.workload_uid.clone());
138139
}
139140
let netns =
@@ -172,12 +173,20 @@ impl WorkloadProxyManagerState {
172173
"pod delete request, shutting down proxy"
173174
);
174175
if !self.snapshot_received {
175-
// TODO: consider if this is an error. if not, do this instead:
176-
// self.snapshot_names.remove(&workload_uid)
177-
// self.pending_workloads.remove(&workload_uid)
178-
return Err(Error::ProtocolError(
179-
"pod delete received before snapshot".into(),
180-
));
176+
debug!("got workload delete before snapshot");
177+
// Since we insert here on AddWorkload before we get a snapshot,
178+
// make sure we also opportunistically remove here before we
179+
// get a snapshot
180+
//
181+
// Note that even though AddWorkload starts the workload, we do *not* need
182+
// to stop it here, as it should be auto-dropped subsequently during snapshot
183+
// reconcile(), when we actually get the `SnapshotSent` notification.
184+
self.snapshot_names.remove(&workload_uid);
185+
// `reconcile()` will drop this workload later, but if the workload never successfully
186+
// starts it will stay in the pending queue (which `reconcile()` can't remove it from),
187+
// so clear the pending queue here.
188+
self.pending_workloads.remove(&workload_uid);
189+
return Ok(());
181190
}
182191
self.del_workload(&workload_uid);
183192
Ok(())
@@ -536,7 +545,7 @@ mod tests {
536545
}
537546

538547
#[tokio::test]
539-
async fn idemepotency_add_workload_fails_and_then_deleted() {
548+
async fn idempotency_add_workload_fails_and_then_deleted() {
540549
let fixture = fixture!();
541550
let mut state = fixture.state;
542551

@@ -567,6 +576,45 @@ mod tests {
567576
state.drain().await;
568577
}
569578

579+
#[tokio::test]
580+
async fn del_workload_before_snapshot_removes_from_snapshot_and_pending() {
581+
let fixture = fixture!();
582+
let mut state = fixture.state;
583+
584+
let ns = new_netns();
585+
586+
// to make the proxy fail, bind to its ports in its netns
587+
let _sock = create_proxy_conflict(&ns);
588+
589+
let data = WorkloadData {
590+
netns: ns,
591+
workload_uid: uid(0),
592+
workload_info: workload_info(),
593+
};
594+
595+
let ret = state.process_msg(WorkloadMessage::AddWorkload(data)).await;
596+
597+
assert!(state.snapshot_names.len() == 1);
598+
assert!(ret.is_err());
599+
assert!(state.have_pending());
600+
601+
state
602+
.process_msg(WorkloadMessage::DelWorkload(uid(0)))
603+
.await
604+
.unwrap();
605+
606+
assert!(state.snapshot_names.is_empty());
607+
608+
state
609+
.process_msg(WorkloadMessage::WorkloadSnapshotSent)
610+
.await
611+
.unwrap();
612+
613+
assert!(state.snapshot_names.is_empty());
614+
assert!(!state.have_pending());
615+
state.drain().await;
616+
}
617+
570618
#[tokio::test]
571619
async fn add_delete_add_workload_starts_only_one_proxy() {
572620
let fixture = fixture!();

0 commit comments

Comments
 (0)