Skip to main content

stackless_core/
capabilities.rs

1//! Provider support is validated before admission or external effects.
2
3use serde::{Deserialize, Serialize};
4
5use crate::def::{StackDef, WorkloadKind};
6use crate::substrate::SubstrateFault;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Capabilities {
10    pub early_origins: bool,
11    #[serde(default)]
12    pub tcp_services: bool,
13    pub workers: bool,
14    pub jobs: bool,
15    pub local_sources: bool,
16    pub empty_sources: bool,
17    pub environment: bool,
18    pub sandbox: bool,
19    pub containers: bool,
20    pub logs: bool,
21}
22
23impl Capabilities {
24    pub fn local() -> Self {
25        Self {
26            early_origins: true,
27            tcp_services: true,
28            workers: true,
29            jobs: true,
30            local_sources: true,
31            empty_sources: true,
32            environment: true,
33            sandbox: true,
34            containers: true,
35            logs: true,
36        }
37    }
38
39    pub fn cloud(environment: bool, logs: bool) -> Self {
40        Self {
41            early_origins: false,
42            tcp_services: false,
43            workers: false,
44            jobs: false,
45            local_sources: false,
46            empty_sources: false,
47            environment,
48            sandbox: false,
49            containers: false,
50            logs,
51        }
52    }
53
54    pub fn validate(&self, provider: &str, def: &StackDef) -> Result<(), SubstrateFault> {
55        for (name, workload) in &def.services {
56            if workload.on.as_ref().is_some_and(|on| on != provider) {
57                continue;
58            }
59            let unsupported = match workload.kind {
60                _ if workload
61                    .health
62                    .as_ref()
63                    .is_some_and(|health| health.is_tcp())
64                    && !self.tcp_services =>
65                {
66                    Some("TCP services")
67                }
68                _ if workload.image.is_some() && !self.containers => Some("container images"),
69                WorkloadKind::Worker if !self.workers => Some("workers"),
70                WorkloadKind::Job if !self.jobs => Some("jobs"),
71                _ if workload.source.path.is_some() && !self.local_sources => {
72                    Some("local source paths")
73                }
74                _ if workload.source.repo.is_empty()
75                    && workload.source.path.is_none()
76                    && !self.empty_sources =>
77                {
78                    Some("workloads without a git source")
79                }
80                _ if !self.environment
81                    && (!workload.env.is_empty()
82                        || !workload.secrets.is_empty()
83                        || !workload
84                            .substrate_env(name, provider)
85                            .map_err(|error| SubstrateFault::from_fault(&error))?
86                            .is_empty()) =>
87                {
88                    Some("environment injection")
89                }
90                _ => None,
91            };
92            if let Some(feature) = unsupported {
93                return Err(unsupported_feature(provider, name, feature));
94            }
95        }
96        Ok(())
97    }
98}
99
100pub fn unsupported_feature(provider: &str, workload: &str, feature: &str) -> SubstrateFault {
101    SubstrateFault {
102        code: "provider.unsupported".into(),
103        message: format!("{provider} does not support {feature} for workload {workload:?}"),
104        remediation:
105            "choose a provider that advertises this capability or change the workload definition"
106                .into(),
107        context: Box::default(),
108    }
109}