xbp-deploy 10.57.0

Service-centric declarative deploy engine for XBP.
Documentation
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
use std::path::{Path, PathBuf};
use std::sync::Arc;

use async_trait::async_trait;
use futures::future::join_all;
use xbp_oci::{OciRef, OciResolver};

use crate::context::DeployContext;
use crate::error::{DeployError, Result};
use crate::graph::{enforce_athena_operator_before_runtime, order_services, ServiceGraph};
use crate::lock::compute_plan_hash;
use crate::providers::{
    destination_to_provider, is_all_destinations, is_cloudflare_provider, is_kubernetes_provider,
    is_railway_provider, normalize_destination, provider_priority, provider_to_destination,
    DESTINATION_ALL,
};
use crate::target_resolver::resolve_target;
use crate::types::*;
use crate::validator::validate_project_config_for_target;

#[async_trait]
pub trait DeployPlanner: Send + Sync {
    async fn plan(&self, ctx: &DeployContext) -> Result<DeployPlan>;
}

pub struct DefaultDeployPlanner {
    pub oci: Option<Arc<dyn OciResolver>>,
}

impl DefaultDeployPlanner {
    pub fn new(oci: Option<Arc<dyn OciResolver>>) -> Self {
        Self { oci }
    }
}

#[async_trait]
impl DeployPlanner for DefaultDeployPlanner {
    async fn plan(&self, ctx: &DeployContext) -> Result<DeployPlan> {
        // Scope validation to the requested target first so a single-service
        // deploy is not blocked by stale deploy.groups.all entries.
        validate_project_config_for_target(&ctx.config, &ctx.env, Some(&ctx.target))?;

        // Always re-resolve from label so group vs service priority is authoritative.
        let resolution = resolve_target(&ctx.config, &ctx.target.label(), &ctx.env)?;
        let target = resolution.target.clone();

        // After resolve, re-check with the authoritative target (group vs service).
        if target != ctx.target {
            validate_project_config_for_target(&ctx.config, &ctx.env, Some(&target))?;
        }

        let graph = ServiceGraph::from_services(&ctx.config.services);
        let selected_names: Vec<String> = resolution.services.iter().map(|s| s.name.clone()).collect();
        let mut order = order_services(
            &selected_names,
            resolution.group_order.as_deref(),
            &graph,
        )?;
        if matches!(&target, DeployTarget::Group(g) if g == "athena") {
            enforce_athena_operator_before_runtime(&mut order);
        }

        // Provider priority: k8s operators → k8s → local → cloudflare → other.
        // Relative order among equal-priority services is preserved (stable sort).
        order.sort_by_key(|name| {
            resolution
                .services
                .iter()
                .find(|s| s.name == *name)
                .map(|s| {
                    provider_priority(
                        s.deploy
                            .as_ref()
                            .map(|d| d.provider.as_str())
                            .unwrap_or("kubernetes"),
                    )
                })
                .unwrap_or(9)
        });

        let mut service_plans = Vec::new();
        let mut expanded_order = Vec::new();
        for name in &order {
            let svc = resolution
                .services
                .iter()
                .find(|s| s.name == *name)
                .ok_or_else(|| DeployError::Other(format!("missing service {name}")))?;
            let plans = build_service_plans(&ctx.config, svc, &ctx.env, &ctx.flags)?;
            for plan in plans {
                let step_key = match &plan.destination {
                    Some(d) => format!("{}@{}", plan.name, d),
                    None => plan.name.clone(),
                };
                expanded_order.push(step_key);
                service_plans.push(plan);
            }
        }
        let order = expanded_order;

        // Parallel OCI digest resolution.
        if !ctx.flags.skip_oci {
            if let Some(oci) = &self.oci {
                let futures = service_plans.iter().map(|sp| {
                    let oci = Arc::clone(oci);
                    let image = sp.image.clone();
                    async move {
                        match image {
                            Some(img) => oci.resolve_digest(&img).await.ok(),
                            None => None,
                        }
                    }
                });
                let digests = join_all(futures).await;
                for (sp, digest) in service_plans.iter_mut().zip(digests) {
                    sp.digest = digest;
                }
            }
        }

        let mut oci_plan = OciPlan::default();
        let mut k8s_services = Vec::new();
        for sp in &service_plans {
            if let Some(r) = &sp.image_ref {
                oci_plan.images.insert(
                    sp.name.clone(),
                    OciImagePlan {
                        ref_str: r.clone(),
                        digest: sp.digest.clone(),
                    },
                );
            }
            // Only kubernetes* providers enter k8s_plan — CF/local never touch kubectl.
            if is_kubernetes_provider(&sp.provider) {
                k8s_services.push(xbp_k8s::K8sServicePlan {
                    service: sp.name.clone(),
                    provider: sp.provider.clone(),
                    namespace: sp.deploy.namespace.clone().or_else(|| ctx.flags.namespace.clone()),
                    workload: sp.deploy.workload.clone(),
                    manifest_paths: sp.deploy.manifest_paths.clone(),
                    crds_path: sp.deploy.crds_path.clone(),
                    install_path: sp.deploy.install_path.clone(),
                    selector: sp.deploy.selector.clone(),
                    health: sp.deploy.health.clone(),
                });
            }
        }

        let mut plan = DeployPlan {
            target,
            env: ctx.env.clone(),
            project: ctx.config.project_name.clone(),
            project_version: ctx.config.version.clone(),
            git_sha: ctx.config.git_sha.clone(),
            services: service_plans,
            order,
            oci_plan,
            k8s_plan: K8sPlanView {
                context: ctx.flags.context.clone().or_else(|| {
                    ctx.config
                        .kubernetes
                        .as_ref()
                        .and_then(|k| k.default_context.clone())
                }),
                default_namespace: {
                    // Prefer shared service namespace when every k8s service agrees;
                    // else CLI flag / project default (per-service ns still wins at apply).
                    let flag_or_project = ctx.flags.namespace.clone().or_else(|| {
                        ctx.config
                            .kubernetes
                            .as_ref()
                            .and_then(|k| k.default_namespace.clone())
                    });
                    let mut service_ns = k8s_services
                        .iter()
                        .filter_map(|s| s.namespace.as_ref())
                        .map(|s| s.trim())
                        .filter(|s| !s.is_empty());
                    if let Some(first) = service_ns.next() {
                        if service_ns.all(|n| n == first) {
                            Some(first.to_string())
                        } else {
                            flag_or_project
                        }
                    } else {
                        flag_or_project
                    }
                },
                services: k8s_services,
            },
            hash: String::new(),
        };
        plan.hash = compute_plan_hash(&plan);
        Ok(plan)
    }
}

/// One resolved destination step for a service.
struct DestinationSpec {
    destination: String,
    provider: String,
    worker: Option<String>,
    rollout: Option<String>,
}

/// Enabled destinations declared under `services[].deploy.destinations`,
/// ordered by provider priority (k8s before cloudflare).
fn configured_enabled_destinations(deploy: &ServiceDeployView) -> Vec<String> {
    let mut scored: Vec<(u8, String)> = deploy
        .destinations
        .iter()
        .filter(|(_, overlay)| overlay.enabled != Some(false))
        .map(|(key, overlay)| {
            let dest = normalize_destination(key);
            let provider = overlay
                .provider
                .as_deref()
                .filter(|s| !s.trim().is_empty())
                .map(str::to_string)
                .unwrap_or_else(|| destination_to_provider(&dest));
            (provider_priority(&provider), dest)
        })
        .collect();
    scored.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
    let mut out = Vec::new();
    for (_, dest) in scored {
        if !out.iter().any(|d| d == &dest) {
            out.push(dest);
        }
    }
    out
}

/// Resolve which destination(s) to ship for a service.
///
/// Selection rules:
/// 1. CLI `--to cloudflare,kubernetes` → those destinations only.
/// 2. CLI `--to all` (or `both` / `*`) → every **enabled** entry in
///    `deploy.destinations` (falls back to default provider if map empty).
/// 3. No `--to`, but `deploy.destinations` is non-empty → every **enabled**
///    destination (multi-target default). This is how Athena ships
///    `athena-auth` to both Cloudflare Worker+Container **and** Kubernetes.
/// 4. No `--to` and empty destinations map → single default `deploy.provider`
///    (backward compatible).
///
/// Config `deploy.destinations.<name>` supplies per-destination overlays
/// (`provider`, `worker`, `rollout`, `enabled`).
fn resolve_destination_specs(
    deploy: &ServiceDeployView,
    service_name: &str,
    flags: &crate::context::DeployFlags,
) -> Result<Vec<DestinationSpec>> {
    let explicit_cli = !flags.destinations.is_empty();
    let expand_all = explicit_cli && is_all_destinations(&flags.destinations);

    let selected: Vec<String> = if expand_all {
        let configured = configured_enabled_destinations(deploy);
        if configured.is_empty() {
            vec![provider_to_destination(&deploy.provider)]
        } else {
            configured
        }
    } else if explicit_cli {
        flags
            .destinations
            .iter()
            .map(|d| normalize_destination(d))
            .filter(|d| d != DESTINATION_ALL)
            .collect()
    } else if !deploy.destinations.is_empty() {
        // Multi-target services: default to every enabled destination.
        let configured = configured_enabled_destinations(deploy);
        if configured.is_empty() {
            // All destinations disabled without --to → fall back to default provider.
            vec![provider_to_destination(&deploy.provider)]
        } else {
            configured
        }
    } else {
        // Single-provider services (legacy): one destination from deploy.provider.
        vec![provider_to_destination(&deploy.provider)]
    };

    let mut specs = Vec::new();
    for dest in selected {
        let overlay = deploy.destinations.get(&dest).or_else(|| {
            // Allow config keys that are aliases of the destination.
            deploy.destinations.iter().find_map(|(k, v)| {
                if normalize_destination(k) == dest {
                    Some(v)
                } else {
                    None
                }
            })
        });

        if let Some(overlay) = overlay {
            // Explicit --to can re-enable a destinations entry marked enabled=false.
            if overlay.enabled == Some(false) && !explicit_cli {
                continue;
            }
            let provider = overlay
                .provider
                .clone()
                .filter(|s| !s.trim().is_empty())
                .unwrap_or_else(|| destination_to_provider(&dest));
            specs.push(DestinationSpec {
                destination: dest.clone(),
                provider,
                worker: overlay
                    .worker
                    .clone()
                    .or_else(|| deploy.worker.clone())
                    .filter(|s| !s.trim().is_empty())
                    .or_else(|| Some(service_name.to_string())),
                rollout: overlay.rollout.clone().or_else(|| deploy.rollout.clone()),
            });
        } else {
            // CLI --to without a destinations{} entry: synthesize provider for this run.
            let provider = if !explicit_cli {
                deploy.provider.clone()
            } else {
                destination_to_provider(&dest)
            };
            let use_service_worker = is_cloudflare_provider(&provider)
                || (!explicit_cli && is_cloudflare_provider(&deploy.provider));
            specs.push(DestinationSpec {
                destination: dest,
                provider: if !explicit_cli {
                    deploy.provider.clone()
                } else {
                    provider
                },
                worker: if use_service_worker {
                    deploy
                        .worker
                        .clone()
                        .filter(|s| !s.trim().is_empty())
                        .or_else(|| Some(service_name.to_string()))
                } else {
                    deploy.worker.clone().filter(|s| !s.trim().is_empty())
                },
                rollout: deploy.rollout.clone(),
            });
        }
    }

    // Stable order by provider priority when multi-target (k8s before CF).
    specs.sort_by(|a, b| {
        provider_priority(&a.provider)
            .cmp(&provider_priority(&b.provider))
            .then_with(|| a.destination.cmp(&b.destination))
    });

    if specs.is_empty() {
        return Err(DeployError::Validation(format!(
            "service `{service_name}` has no deploy destinations after filters \
             (configure services[].deploy.destinations or pass --to cloudflare|kubernetes|all)"
        )));
    }
    Ok(specs)
}

fn build_service_plans(
    config: &ProjectConfig,
    svc: &ServiceConfigView,
    env: &str,
    flags: &crate::context::DeployFlags,
) -> Result<Vec<ServicePlan>> {
    let deploy = svc
        .deploy
        .as_ref()
        .ok_or_else(|| DeployError::Validation(format!("service `{}` has no deploy", svc.name)))?;
    let env_cfg = deploy.envs.get(env).ok_or_else(|| {
        DeployError::Validation(format!("service `{}` missing deploy.envs.{env}", svc.name))
    })?;
    let specs = resolve_destination_specs(deploy, &svc.name, flags)?;
    let version = svc
        .version
        .clone()
        .unwrap_or_else(|| config.version.clone());
    let (image, image_ref) = resolve_image(svc, &version, &config.version);

    // Compose dotenv is required for k8s bootstrap env injection. Pure CF/Worker
    // destinations auto-discover `.env*` only as soft context — a local BOM or
    // junk line must not abort OpenNext/Wrangler deploys (no cluster / compose).
    let needs_strict_env = specs.iter().any(|s| is_kubernetes_provider(&s.provider));
    let composed = crate::compose::compose_service_runtime_with_opts(
        &config.project_root,
        svc,
        env,
        env_cfg,
        needs_strict_env,
    )?;
    // required_env only gates destinations that inject composed env (k8s).
    // Cloudflare Workers use wrangler / .dev.vars / OpenNext — not compose.
    if needs_strict_env && !composed.missing_required.is_empty() {
        return Err(DeployError::Validation(format!(
            "service `{}` missing required env for deploy.envs.{}: {} (set in .env, services.environment, or deploy.envs.{}.env)",
            svc.name,
            env,
            composed.missing_required.join(", "),
            env
        )));
    }

    let mut plans = Vec::new();
    for spec in specs {
        plans.push(build_one_service_plan(
            config,
            svc,
            env_cfg,
            &version,
            image.clone(),
            image_ref.clone(),
            flags,
            spec,
            &composed,
        )?);
    }
    Ok(plans)
}

fn build_one_service_plan(
    config: &ProjectConfig,
    svc: &ServiceConfigView,
    env_cfg: &ServiceDeployEnvView,
    version: &str,
    image: Option<OciRef>,
    image_ref: Option<String>,
    flags: &crate::context::DeployFlags,
    spec: DestinationSpec,
    composed: &crate::compose::ComposedRuntime,
) -> Result<ServicePlan> {
    let provider = spec.provider;
    let destination = spec.destination;

    let namespace = flags
        .namespace
        .clone()
        .or_else(|| env_cfg.namespace.clone())
        .or_else(|| {
            config
                .kubernetes
                .as_ref()
                .and_then(|k| k.default_namespace.clone())
        });

    let mut manifest_paths = Vec::new();
    let mut crds_path = None;
    let mut install_path = None;
    let mut workload = None;
    let mut k8s_service = None;
    let mut selector = None;
    // Manifests / workloads only apply to kubernetes destinations.
    if is_kubernetes_provider(&provider) {
        if let Some(k8s) = &env_cfg.kubernetes {
            for candidate in [&k8s.manifests_base, &k8s.manifests_overlay]
                .into_iter()
                .flatten()
            {
                manifest_paths.push(resolve_path(
                    &config.project_root,
                    svc.root_directory.as_deref(),
                    candidate,
                ));
            }
            workload = k8s.workload.clone();
            k8s_service = k8s
                .service
                .as_ref()
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty());
            selector = k8s.selector.clone();
            crds_path = k8s.crds_path.as_ref().map(|p| {
                resolve_path(&config.project_root, svc.root_directory.as_deref(), p)
            });
            install_path = k8s.install_path.as_ref().map(|p| {
                resolve_path(&config.project_root, svc.root_directory.as_deref(), p)
            });
        }
    }

    let worker_app = if is_cloudflare_provider(&provider) {
        Some(
            spec.worker
                .clone()
                .filter(|s| !s.trim().is_empty())
                .unwrap_or_else(|| svc.name.clone()),
        )
    } else {
        None
    };
    let rollout = if is_cloudflare_provider(&provider) {
        spec.rollout.clone()
    } else {
        None
    };

    let mut actions = Vec::new();
    let dockerfile = svc.oci.as_ref().and_then(|o| o.dockerfile.clone());
    let build_context = svc.oci.as_ref().and_then(|o| o.context.clone());
    let platforms = svc
        .oci
        .as_ref()
        .map(|o| o.platforms.clone())
        .unwrap_or_default();

    actions.push(format!("destination `{destination}`"));
    if is_kubernetes_provider(&provider) {
        // Always advertise build/push first when a Dockerfile is configured (including operators).
        if dockerfile.is_some() && image_ref.is_some() && !flags.skip_build {
            if flags.local_image {
                actions.push(
                    "build local OCI image (docker load — no registry push)".into(),
                );
            } else {
                actions.push("build + push OCI image (before cluster apply)".into());
            }
        } else if image_ref.is_some() {
            actions.push("verify OCI image".into());
        }
        if destination == "kubernetes-operator"
            || provider.to_ascii_lowercase().contains("operator")
        {
            if crds_path.is_some() {
                actions.push("apply CRDs".into());
            }
            if install_path.is_some() {
                actions.push("apply operator install".into());
            }
            if selector.is_some() {
                actions.push("wait operator selector".into());
            }
        } else {
            if !manifest_paths.is_empty() {
                actions.push(format!("apply {} manifest path(s)", manifest_paths.len()));
            } else if workload.is_some() {
                if flags.bootstrap_workload {
                    actions.push(
                        "bootstrap minimal Deployment/Service (no manifests configured)".into(),
                    );
                } else {
                    actions.push(
                        "error risk: no kubernetes manifests — use --bootstrap-workload or set kubernetes.manifests".into(),
                    );
                }
            }
            if !composed.env.is_empty() {
                actions.push(format!(
                    "compose {} runtime env key(s) from .env / services.environment / deploy.envs",
                    composed.env.len()
                ));
            }
            if !composed.config_mounts.is_empty() {
                actions.push(format!(
                    "mount {} config file(s) (ConfigMap)",
                    composed.config_mounts.len()
                ));
            }
            if !composed.loaded_env_files.is_empty() {
                actions.push(format!(
                    "loaded {} env file(s)",
                    composed.loaded_env_files.len()
                ));
            }
            if !composed.missing_required.is_empty() {
                actions.push(format!(
                    "WARNING: missing required env: {}",
                    composed.missing_required.join(", ")
                ));
            }
            if workload.is_some() {
                actions.push("wait rollout".into());
            }
            if let Some(ex) = &env_cfg.expose {
                let st = ex.service_type.as_deref().unwrap_or("ClusterIP");
                let mut bits = vec![format!("expose type={st}")];
                if let Some(lp) = ex.local_port {
                    bits.push(format!("local_port={lp}"));
                }
                if ex.port_forward.unwrap_or(ex.local_port.is_some() || !ex.dns_hosts.is_empty()) {
                    bits.push("port-forward".into());
                }
                if !ex.dns_hosts.is_empty() {
                    bits.push(format!("dns_hosts={}", ex.dns_hosts.len()));
                }
                actions.push(bits.join(", "));
            } else if flags.local_image {
                actions.push(
                    "hint: set deploy.envs.*.expose (local_port/port_forward) for localhost routing"
                        .into(),
                );
            }
            if !env_cfg.health.is_empty() {
                actions.push(format!("probe {} health URL(s)", env_cfg.health.len()));
            }
        }
    } else if is_cloudflare_provider(&provider) {
        let app = worker_app.as_deref().unwrap_or(svc.name.as_str());
        actions.push(format!("cloudflare doctor/deploy worker `{app}`"));
        if dockerfile.is_some() && image_ref.is_some() {
            actions.push(
                "skip GHCR build/push (Cloudflare owns image — OpenNext or containers local_build)"
                    .into(),
            );
        }
        if let Some(r) = &rollout {
            actions.push(format!("containers rollout `{r}`"));
        } else {
            actions.push("containers rollout (worker default)".into());
        }
        if !env_cfg.health.is_empty() {
            actions.push(format!("probe {} health URL(s)", env_cfg.health.len()));
        }
    } else if crate::providers::is_local_provider(&provider) {
        actions.push("local preflight steps".into());
    } else if is_railway_provider(&provider) {
        actions.push("railway deploy (not wired — use cloudflare or kubernetes for now)".into());
    } else if crate::providers::is_oci_provider(&provider) {
        actions.push("oci promote/publish".into());
    } else {
        actions.push(format!("provider `{provider}` (no runner yet)"));
    }

    // Ensure PORT matches container listen port for bootstrap containers.
    let mut runtime_env = composed.env.clone();
    if let Some(port) = composed.container_port {
        runtime_env
            .entry("PORT".into())
            .or_insert_with(|| port.to_string());
    }

    Ok(ServicePlan {
        name: svc.name.clone(),
        provider,
        destination: Some(destination),
        root_directory: svc.root_directory.clone(),
        version: version.to_string(),
        image,
        image_ref,
        digest: None,
        dockerfile,
        build_context,
        platforms,
        worker_app,
        rollout,
        runtime_env,
        container_port: composed.container_port,
        config_mounts: composed.config_mounts.clone(),
        expose: env_cfg.expose.clone(),
        deploy: ServiceDeployPlan {
            namespace,
            workload,
            service: k8s_service,
            health: env_cfg.health.clone(),
            manifest_paths,
            crds_path,
            install_path,
            selector,
            actions,
        },
    })
}

fn resolve_image(
    svc: &ServiceConfigView,
    service_version: &str,
    project_version: &str,
) -> (Option<OciRef>, Option<String>) {
    let Some(oci) = &svc.oci else {
        return (None, None);
    };
    let tag = if let Some(t) = oci.tag.as_deref().filter(|s| !s.is_empty()) {
        t.to_string()
    } else {
        match oci.tag_from.as_deref().unwrap_or("project_version") {
            "service_version" => service_version.to_string(),
            "git_sha" => "git".into(),
            _ => project_version.to_string(),
        }
    };
    let ref_str = format!("{}:{tag}", oci.image.trim());
    match xbp_oci::parse_image_ref(&ref_str) {
        Ok(r) => (Some(r), Some(ref_str)),
        Err(_) => (None, Some(ref_str)),
    }
}

fn resolve_path(project_root: &Path, service_root: Option<&str>, relative: &str) -> PathBuf {
    let rel = PathBuf::from(relative);
    if rel.is_absolute() {
        return rel;
    }
    if let Some(root) = service_root {
        let under = project_root.join(root).join(&rel);
        if under.exists() {
            return under;
        }
    }
    project_root.join(rel)
}

#[cfg(test)]
mod multi_destination_tests {
    use super::*;
    use crate::context::DeployFlags;
    use crate::types::{ServiceDeployDestinationView, ServiceDeployView};
    use std::collections::HashMap;

    fn dual_target_deploy() -> ServiceDeployView {
        let mut destinations = HashMap::new();
        destinations.insert(
            "kubernetes".into(),
            ServiceDeployDestinationView {
                provider: Some("kubernetes".into()),
                worker: None,
                rollout: None,
                enabled: None,
            },
        );
        destinations.insert(
            "cloudflare".into(),
            ServiceDeployDestinationView {
                provider: Some("cloudflare-containers".into()),
                worker: Some("athena-auth".into()),
                rollout: Some("immediate".into()),
                enabled: None,
            },
        );
        ServiceDeployView {
            provider: "cloudflare-containers".into(),
            worker: Some("athena-auth".into()),
            rollout: Some("immediate".into()),
            destinations,
            envs: HashMap::new(),
        }
    }

    #[test]
    fn default_uses_all_enabled_destinations() {
        let deploy = dual_target_deploy();
        let flags = DeployFlags::default();
        let specs = resolve_destination_specs(&deploy, "athena-auth", &flags).unwrap();
        assert_eq!(specs.len(), 2);
        // k8s first by provider priority
        assert_eq!(specs[0].destination, "kubernetes");
        assert!(is_kubernetes_provider(&specs[0].provider));
        assert_eq!(specs[1].destination, "cloudflare");
        assert!(is_cloudflare_provider(&specs[1].provider));
        assert_eq!(specs[1].worker.as_deref(), Some("athena-auth"));
        assert_eq!(specs[1].rollout.as_deref(), Some("immediate"));
    }

    #[test]
    fn to_cloudflare_narrows() {
        let deploy = dual_target_deploy();
        let mut flags = DeployFlags::default();
        flags.destinations = vec!["cloudflare".into()];
        let specs = resolve_destination_specs(&deploy, "athena-auth", &flags).unwrap();
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].destination, "cloudflare");
    }

    #[test]
    fn to_all_expands() {
        let deploy = dual_target_deploy();
        let mut flags = DeployFlags::default();
        flags.destinations = vec![DESTINATION_ALL.into()];
        let specs = resolve_destination_specs(&deploy, "athena-auth", &flags).unwrap();
        assert_eq!(specs.len(), 2);
    }

    #[test]
    fn empty_destinations_map_uses_default_provider() {
        let deploy = ServiceDeployView {
            provider: "kubernetes".into(),
            worker: None,
            rollout: None,
            destinations: HashMap::new(),
            envs: HashMap::new(),
        };
        let flags = DeployFlags::default();
        let specs = resolve_destination_specs(&deploy, "athena", &flags).unwrap();
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].destination, "kubernetes");
        assert!(is_kubernetes_provider(&specs[0].provider));
    }

    #[test]
    fn disabled_destination_skipped_unless_explicit() {
        let mut deploy = dual_target_deploy();
        deploy
            .destinations
            .get_mut("kubernetes")
            .unwrap()
            .enabled = Some(false);
        let flags = DeployFlags::default();
        let specs = resolve_destination_specs(&deploy, "athena-auth", &flags).unwrap();
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].destination, "cloudflare");

        let mut flags = DeployFlags::default();
        flags.destinations = vec!["kubernetes".into()];
        let specs = resolve_destination_specs(&deploy, "athena-auth", &flags).unwrap();
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].destination, "kubernetes");
    }
}