Skip to content

Commit 7402646

Browse files
committed
agent: Fix use of "wrong" kube api client
All calls to `handle_config_add` used a newly created `KubeImpl` object this led to tests using the real Kube client rather than the mocked one. In order to fix this, use `Arc` (as required by `handle_config_add`) at creation time and propagate the `Arc` where needed. Also goes from `impl` to `dyn` wherever needed. Signed-off-by: Nicolas Belouin <nicolas.belouin@suse.com>
1 parent 90079c6 commit 7402646

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

agent/src/util/config_action.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub async fn do_config_watch(
5353
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
5454
info!("do_config_watch - enter");
5555
let config_map: ConfigMap = Arc::new(RwLock::new(HashMap::new()));
56-
let kube_interface = k8s::KubeImpl::new().await?;
56+
let kube_interface = Arc::new(k8s::KubeImpl::new().await?);
5757
let mut tasks = Vec::new();
5858

5959
// Handle pre-existing configs
@@ -62,9 +62,10 @@ pub async fn do_config_watch(
6262
let config_map = config_map.clone();
6363
let discovery_handler_map = discovery_handler_map.clone();
6464
let new_discovery_handler_sender = new_discovery_handler_sender.clone();
65+
let new_kube_interface = kube_interface.clone();
6566
tasks.push(tokio::spawn(async move {
6667
handle_config_add(
67-
Arc::new(k8s::KubeImpl::new().await.unwrap()),
68+
new_kube_interface,
6869
&config,
6970
config_map,
7071
discovery_handler_map,
@@ -78,7 +79,7 @@ pub async fn do_config_watch(
7879
// Watch for new configs and changes
7980
tasks.push(tokio::spawn(async move {
8081
watch_for_config_changes(
81-
&kube_interface,
82+
kube_interface,
8283
config_map,
8384
discovery_handler_map,
8485
new_discovery_handler_sender,
@@ -94,7 +95,7 @@ pub async fn do_config_watch(
9495

9596
/// This watches for Configuration events
9697
async fn watch_for_config_changes(
97-
kube_interface: &impl KubeInterface,
98+
kube_interface: Arc<dyn KubeInterface>,
9899
config_map: ConfigMap,
99100
discovery_handler_map: RegisteredDiscoveryHandlerMap,
100101
new_discovery_handler_sender: broadcast::Sender<String>,
@@ -115,7 +116,7 @@ async fn watch_for_config_changes(
115116
};
116117
let new_discovery_handler_sender = new_discovery_handler_sender.clone();
117118
handle_config(
118-
kube_interface,
119+
kube_interface.clone(),
119120
event,
120121
config_map.clone(),
121122
discovery_handler_map.clone(),
@@ -129,7 +130,7 @@ async fn watch_for_config_changes(
129130
/// This takes an event off the Configuration stream and delegates it to the
130131
/// correct function based on the event type.
131132
async fn handle_config(
132-
kube_interface: &impl KubeInterface,
133+
kube_interface: Arc<dyn KubeInterface>,
133134
event: Event<Configuration>,
134135
config_map: ConfigMap,
135136
discovery_handler_map: RegisteredDiscoveryHandlerMap,
@@ -157,7 +158,7 @@ async fn handle_config(
157158
config.metadata.name.clone().unwrap(),
158159
);
159160
info!("handle_config - deleted Configuration {:?}", config_id,);
160-
handle_config_delete(kube_interface, config_id, config_map).await?;
161+
handle_config_delete(kube_interface.as_ref(), config_id, config_map).await?;
161162
}
162163
Event::Restarted(configs) => {
163164
let new_configs: HashSet<ConfigId> = configs
@@ -171,11 +172,16 @@ async fn handle_config(
171172
.collect();
172173
let old_configs: HashSet<ConfigId> = config_map.read().await.keys().cloned().collect();
173174
for config_id in old_configs.difference(&new_configs) {
174-
handle_config_delete(kube_interface, config_id.clone(), config_map.clone()).await?;
175+
handle_config_delete(
176+
kube_interface.as_ref(),
177+
config_id.clone(),
178+
config_map.clone(),
179+
)
180+
.await?;
175181
}
176182
for config in configs {
177183
handle_config_apply(
178-
kube_interface,
184+
kube_interface.clone(),
179185
config,
180186
config_map.clone(),
181187
discovery_handler_map.clone(),
@@ -189,7 +195,7 @@ async fn handle_config(
189195
}
190196

191197
async fn handle_config_apply(
192-
kube_interface: &impl KubeInterface,
198+
kube_interface: Arc<dyn KubeInterface>,
193199
config: Configuration,
194200
config_map: ConfigMap,
195201
discovery_handler_map: RegisteredDiscoveryHandlerMap,
@@ -215,12 +221,12 @@ async fn handle_config_apply(
215221
"handle_config - modified Configuration {:?}",
216222
config.metadata.name,
217223
);
218-
handle_config_delete(kube_interface, config_id, config_map.clone()).await?;
224+
handle_config_delete(kube_interface.as_ref(), config_id, config_map.clone()).await?;
219225
}
220226

221227
tokio::spawn(async move {
222228
handle_config_add(
223-
Arc::new(k8s::KubeImpl::new().await.unwrap()),
229+
kube_interface,
224230
&config,
225231
config_map,
226232
discovery_handler_map,
@@ -282,7 +288,7 @@ async fn handle_config_add(
282288
/// Then, for each of the Configuration's Instances, it signals the DevicePluginService to shutdown,
283289
/// and deletes the Instance CRD.
284290
async fn handle_config_delete(
285-
kube_interface: &impl KubeInterface,
291+
kube_interface: &dyn KubeInterface,
286292
config_id: ConfigId,
287293
config_map: ConfigMap,
288294
) -> anyhow::Result<()> {
@@ -363,7 +369,7 @@ async fn should_recreate_config(
363369

364370
/// This shuts down all a Configuration's Instances and terminates the associated Device Plugins
365371
pub async fn delete_all_instances_in_map(
366-
kube_interface: &impl k8s::KubeInterface,
372+
kube_interface: &dyn k8s::KubeInterface,
367373
instance_map: InstanceMap,
368374
(namespace, name): ConfigId,
369375
) -> anyhow::Result<()> {
@@ -409,12 +415,13 @@ mod config_action_tests {
409415
config.metadata.namespace.clone().unwrap(),
410416
config.metadata.name.clone().unwrap(),
411417
);
418+
let kube_interface = Arc::new(MockKubeInterface::new());
412419

413420
let config_map = Arc::new(RwLock::new(HashMap::new()));
414421
let dh_map = Arc::new(std::sync::Mutex::new(HashMap::new()));
415422
let (tx, mut _rx1) = broadcast::channel(1);
416423
assert!(handle_config(
417-
&MockKubeInterface::new(),
424+
kube_interface.clone(),
418425
Event::Restarted(vec![config]),
419426
config_map.clone(),
420427
dh_map.clone(),
@@ -429,7 +436,7 @@ mod config_action_tests {
429436
assert!(config_map.read().await.contains_key(&config_id));
430437

431438
assert!(handle_config(
432-
&MockKubeInterface::new(),
439+
kube_interface,
433440
Event::Restarted(Vec::new()),
434441
config_map.clone(),
435442
dh_map,

0 commit comments

Comments
 (0)