(&self)
| 708 | // Kept `async` to match the gRPC handler signature in `grpc.rs`, which awaits this method. |
| 709 | #[allow(clippy::unused_async)] |
| 710 | pub async fn watch_sandboxes(&self) -> Result<WatchStream, String> { |
| 711 | let namespace = self.config.namespace.clone(); |
| 712 | let agent_sandbox_api = self |
| 713 | .supported_agent_sandbox_api(self.watch_client.clone()) |
| 714 | .await?; |
| 715 | let event_api: Api<KubeEventObj> = Api::namespaced(self.watch_client.clone(), &namespace); |
| 716 | let mut sandbox_stream = |
| 717 | watcher::watcher(agent_sandbox_api.api, watcher::Config::default()).boxed(); |
| 718 | let mut event_stream = watcher::watcher(event_api, watcher::Config::default()).boxed(); |
| 719 | let (tx, rx) = mpsc::channel(256); |
| 720 | |
| 721 | tokio::spawn(async move { |
| 722 | let mut sandbox_name_to_id = std::collections::HashMap::<String, String>::new(); |
| 723 | let mut agent_pod_to_id = std::collections::HashMap::<String, String>::new(); |
| 724 | |
| 725 | loop { |
| 726 | tokio::select! { |
| 727 | result = sandbox_stream.try_next() => match result { |
| 728 | Ok(Some(Event::Applied(obj))) => { |
| 729 | match sandbox_from_object(&namespace, obj) { |
| 730 | Ok(sandbox) => { |
| 731 | update_indexes(&mut sandbox_name_to_id, &mut agent_pod_to_id, &sandbox); |
| 732 | let event = WatchSandboxesEvent { |
| 733 | payload: Some(watch_sandboxes_event::Payload::Sandbox( |
| 734 | WatchSandboxesSandboxEvent { sandbox: Some(sandbox) } |
| 735 | )), |
| 736 | }; |
| 737 | if tx.send(Ok(event)).await.is_err() { |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | Err(err) => { |
| 742 | if tx.send(Err(KubernetesDriverError::Message(err))).await.is_err() { |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | Ok(Some(Event::Deleted(obj))) => { |
| 749 | match sandbox_id_from_object(&obj) { |
| 750 | Ok(sandbox_id) => { |
| 751 | remove_indexes(&mut sandbox_name_to_id, &mut agent_pod_to_id, &sandbox_id); |
| 752 | let event = WatchSandboxesEvent { |
| 753 | payload: Some(watch_sandboxes_event::Payload::Deleted( |
| 754 | WatchSandboxesDeletedEvent { sandbox_id } |
| 755 | )), |
| 756 | }; |
| 757 | if tx.send(Ok(event)).await.is_err() { |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | Err(err) => { |
| 762 | if tx.send(Err(KubernetesDriverError::Message(err))).await.is_err() { |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
nothing calls this directly
no test coverage detected