wick_runtime/runtime/scope/
child_init.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use flow_graph_interpreter::HandlerMap;
use seeded_random::Seed;
use tracing::Span;
use uuid::Uuid;
use wick_config::config::ComponentConfiguration;
use wick_packet::RuntimeConfig;

use super::{ComponentRegistry, Scope, ScopeInit};
use crate::runtime::RuntimeInit;
use crate::{BoxFuture, ScopeError};

#[derive()]
pub(crate) struct ChildInit {
  pub(crate) rng_seed: Seed,
  pub(crate) runtime_id: Uuid,
  pub(crate) allow_latest: bool,
  pub(crate) allowed_insecure: Vec<String>,
  pub(crate) root_config: Option<RuntimeConfig>,
  pub(crate) provided: Option<HandlerMap>,
  pub(crate) max_packet_size: Option<u32>,
  #[allow(unused)]
  pub(crate) span: Span,
}

impl std::fmt::Debug for ChildInit {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("ChildInit")
      .field("rng_seed", &self.rng_seed)
      .field("runtime_id", &self.runtime_id)
      .field("allow_latest", &self.allow_latest)
      .field("max_packet_size", &self.max_packet_size)
      .field("allowed_insecure", &self.allowed_insecure)
      .field("root_config", &self.root_config)
      .field("provided", &self.provided.as_ref().map(|p| p.inner().keys()))
      .finish()
  }
}

pub(crate) fn init_child(
  uid: Uuid,
  manifest: ComponentConfiguration,
  namespace: String,
  opts: ChildInit,
  max_packet_size: Option<u32>,
) -> BoxFuture<'static, Result<Scope, ScopeError>> {
  let child_span = info_span!(parent:&opts.span,"scope",id=%namespace);
  let mut components = ComponentRegistry::default();

  Box::pin(async move {
    for req in manifest.requires() {
      let ns = req.id();
      if let Some(handler) = opts.provided.as_ref().and_then(|p| p.get(ns).cloned()) {
        components.add(Box::new(move |_| Ok(handler.clone())));
      } else {
        return Err(ScopeError::RequirementUnsatisfied(ns.to_owned()));
      }
    }

    let config = RuntimeInit {
      manifest,
      allow_latest: opts.allow_latest,
      allowed_insecure: opts.allowed_insecure,
      namespace: Some(namespace),
      constraints: Default::default(),
      span: child_span,
      initial_components: components,
      max_packet_size,
    };

    let init = ScopeInit::new_with_id(Some(opts.runtime_id), uid, opts.rng_seed, config);

    Scope::start(init).await
  })
}