Skip to main content

clawft_kernel/workload_kind/
cog.rs

1//! The `cog` kind (ADR-100), behind [`WorkloadKind`]. Behaviour is the
2//! pre-registry cog path unchanged.
3
4#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
5use std::path::Path;
6
7#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
8use crate::workload_ctl::{PlaneError, cog_workload_spec};
9#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
10use crate::workload_pkg::{DirSource, TrustAnchors, VerifyPolicy, verify_dir_in};
11use crate::workload_pkg::{ManifestEnvelope, ManifestError};
12#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
13use crate::workload_runtime::VerifiedWorkload;
14#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
15use clawft_types::placement::engine::WorkloadSpec;
16
17use super::WorkloadKind;
18#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
19use super::{KindRegistry, MAX_KIND_LEN};
20
21/// Native, container and Seed-hosted cog packages.
22#[derive(Debug, Clone, Copy, Default)]
23pub struct CogKind;
24
25impl WorkloadKind for CogKind {
26    fn id(&self) -> &'static str {
27        crate::workload_pkg::KIND_COG
28    }
29
30    fn validate(&self, envelope: &ManifestEnvelope) -> Result<(), ManifestError> {
31        envelope.cog_body().map(|_| ())
32    }
33
34    #[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
35    fn load(
36        &self,
37        package_dir: &Path,
38        anchors: &TrustAnchors,
39        kinds: &KindRegistry,
40    ) -> Result<VerifiedWorkload, PlaneError> {
41        load_cog_shaped(self.id(), package_dir, anchors, kinds)
42    }
43
44    #[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
45    fn spec(&self, workload: &VerifiedWorkload) -> Result<WorkloadSpec, String> {
46        cog_workload_spec(workload)
47    }
48
49    #[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
50    fn adapters(&self) -> &'static [&'static str] {
51        &["native", "container", "emulated", "remote.api"]
52    }
53}
54
55/// Verify and load a package whose verified body is cog-shaped, refusing it
56/// unless the verified envelope's kind is `expected` (closes the window
57/// between peeking the kind and verifying the package).
58#[cfg(all(feature = "workload-runtime", feature = "mesh", unix))]
59pub fn load_cog_shaped(
60    expected: &str,
61    package_dir: &Path,
62    anchors: &TrustAnchors,
63    kinds: &KindRegistry,
64) -> Result<VerifiedWorkload, PlaneError> {
65    let verified = verify_dir_in(package_dir, anchors, &VerifyPolicy::default(), kinds)
66        .map_err(|e| PlaneError::Package(e.to_string()))?;
67    if verified.envelope.kind != expected {
68        return Err(PlaneError::Package(format!(
69            "manifest kind {:?} is not {expected:?}",
70            verified
71                .envelope
72                .kind
73                .chars()
74                .take(MAX_KIND_LEN)
75                .collect::<String>()
76        )));
77    }
78    VerifiedWorkload::from_package(&verified, &DirSource::new(package_dir))
79        .map_err(|e| PlaneError::Package(e.to_string()))
80}