Skip to content

Commit 00b61da

Browse files
committed
feat: make workload_id in host the kubernetes UID
This prevents issues where the workload is not fully started, but a stop workload is called. Then the stop workload is called without an ID, and once the workload is fully started it wont be stopped. Therefore, it will run forever, which causes problems when you want to call a workload with a certain host. It might resolve to an old workload, which it shouldnt. Now it will always get removed successfully Also has some cleanups in log statements Signed-off-by: Mees Molenaar <mees.molenaar@bettyblocks.com>
1 parent db332aa commit 00b61da

File tree

8 files changed

+179
-142
lines changed

8 files changed

+179
-142
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
target/
1+
**/target/

crates/wash-runtime/src/engine/workload.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010

1111
use anyhow::{Context as _, bail, ensure};
1212
use tokio::{sync::RwLock, task::JoinHandle, time::timeout};
13-
use tracing::{debug, info, trace, warn};
13+
use tracing::{debug, error, info, trace, warn};
1414
use wasmtime::component::{
1515
Component, Instance, InstancePre, Linker, ResourceAny, ResourceType, Val, types::ComponentItem,
1616
};
@@ -713,7 +713,16 @@ impl ResolvedWorkload {
713713

714714
let func = instance
715715
.get_func(&mut store, func_idx)
716-
.context("function not found")?;
716+
.context("function not found");
717+
718+
let func = match func {
719+
Ok(func) => func,
720+
Err(e) => {
721+
error!("Error in gettin func: {:?}", e);
722+
return Err(e);
723+
}
724+
};
725+
717726
trace!(
718727
name = %import_name,
719728
fn_name = %export_name,
@@ -821,7 +830,6 @@ impl ResolvedWorkload {
821830
);
822831
continue;
823832
}
824-
825833
trace!(name = import_name, resource = export_name, ty = ?resource_ty, "linking resource import");
826834

827835
linker_instance

crates/wash-runtime/src/host/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ impl HostApi for Host {
594594
"Workload stopped successfully".to_string(),
595595
)
596596
} else {
597+
warn!(
598+
"Tried to stop non existing workload: {}",
599+
request.workload_id
600+
);
597601
(WorkloadState::Unspecified, "Workload not found".to_string())
598602
};
599603

crates/wash-runtime/src/washlet/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ async fn workload_start(
287287
config: &HostConfig,
288288
) -> anyhow::Result<types::v2::WorkloadStartResponse> {
289289
let Some(types::v2::Workload {
290+
workload_id,
290291
namespace,
291292
name,
292293
annotations,
@@ -370,7 +371,7 @@ async fn workload_start(
370371
let volumes = volumes.into_iter().map(Into::into).collect();
371372

372373
let request = crate::types::WorkloadStartRequest {
373-
workload_id: uuid::Uuid::new_v4().to_string(),
374+
workload_id: workload_id,
374375
workload: crate::types::Workload {
375376
namespace,
376377
name,
@@ -388,7 +389,16 @@ async fn workload_start(
388389
name=?request.workload.name,
389390
"Starting workload");
390391

391-
Ok(host.workload_start(request).await?.into())
392+
match host.workload_start(request).await {
393+
Ok(resp) => Ok(resp.into()),
394+
Err(e) => Ok(types::v2::WorkloadStartResponse {
395+
workload_status: Some(types::v2::WorkloadStatus {
396+
workload_id: "".into(),
397+
workload_state: types::v2::WorkloadState::Error.into(),
398+
message: format!("failed to start workload: {}", e),
399+
}),
400+
}),
401+
}
392402
}
393403

394404
async fn workload_stop(

proto/docs/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,13 @@ <h3 id="wasmcloud.runtime.v2.Workload">Workload</h3>
11241124
<td><p> </p></td>
11251125
</tr>
11261126

1127+
<tr>
1128+
<td>workload_id</td>
1129+
<td><a href="#string">string</a></td>
1130+
<td></td>
1131+
<td><p> </p></td>
1132+
</tr>
1133+
11271134
</tbody>
11281135
</table>
11291136

proto/wasmcloud/runtime/v2/workload.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ message Workload {
1515
WitWorld wit_world = 5;
1616

1717
repeated Volume volumes = 6;
18+
19+
string workload_id = 7;
1820
}
1921

2022
enum WorkloadState {

runtime-operator/internal/controller/runtime/workload_controller.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ func (r *WorkloadReconciler) reconcilePlacement(ctx context.Context, workload *r
262262

263263
req := &runtimev2.WorkloadStartRequest{
264264
Workload: &runtimev2.Workload{
265+
WorkloadId: string(workload.UID),
265266
Namespace: workload.Namespace,
266267
Name: workload.Name,
267268
Annotations: workload.GetAnnotations(),
@@ -275,13 +276,13 @@ func (r *WorkloadReconciler) reconcilePlacement(ctx context.Context, workload *r
275276
ctx, cancel := context.WithTimeout(ctx, workloadStartTimeout)
276277
defer cancel()
277278

278-
resp, err := client.Start(ctx, req)
279+
_, err := client.Start(ctx, req)
279280
if err != nil {
280281
return err
281282
}
282283

283284
// Set the WorkloadID in the status
284-
workload.Status.WorkloadID = resp.WorkloadStatus.WorkloadId
285+
workload.Status.WorkloadID = string(workload.UID)
285286
condition.ForceStatusUpdate(ctx)
286287
return condition.ErrSkipReconciliation()
287288
}
@@ -320,14 +321,9 @@ func (r *WorkloadReconciler) reconcileReady(_ context.Context, workload *runtime
320321
}
321322

322323
func (r *WorkloadReconciler) finalize(ctx context.Context, workload *runtimev1alpha1.Workload) error {
323-
if !workload.Status.AllTrue(runtimev1alpha1.WorkloadConditionPlacement) {
324-
// nothing to do, the workload was never placed
325-
return nil
326-
}
327-
328324
client := NewWashHostClient(r.Bus, workload.Status.HostID)
329325
req := &runtimev2.WorkloadStopRequest{
330-
WorkloadId: workload.Status.WorkloadID,
326+
WorkloadId: string(workload.UID),
331327
}
332328

333329
ctx, cancel := context.WithTimeout(ctx, workloadStopTimeout)

0 commit comments

Comments
 (0)