k8s: the controller pod keeps no volume; the state gate goes
This commit is contained in:
parent
b69f8c1de0
commit
513be8f7d0
1 changed files with 233 additions and 1110 deletions
|
|
@ -7,11 +7,6 @@
|
|||
//! units run, and lists the pods in the swarm's namespace with one `kubectl
|
||||
//! get`; [`classify`] turns that into [`Step`]s and is pure, so every case is
|
||||
//! tested against hand-built observations.
|
||||
//!
|
||||
//! The copy marker is never read from the volume. The pod's first init
|
||||
//! container, the start gate, exits 0 only with the marker present and
|
||||
//! refuses otherwise with the reason in its termination message; both are in
|
||||
//! the pod's status.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::Write as _;
|
||||
|
|
@ -26,13 +21,9 @@ use serde_json::Value;
|
|||
|
||||
/// What the module renders for this host.
|
||||
const CONFIG_PATH: &str = "/etc/hyperhive/migrate.json";
|
||||
/// The pod's start gate: the init container that checks the copy marker.
|
||||
const GATE_CONTAINER: &str = "state-gate";
|
||||
/// The label every pod of the swarm carries, and the one that names its service.
|
||||
const PART_OF_SELECTOR: &str = "app.kubernetes.io/part-of=hyperhive";
|
||||
const SERVICE_LABEL: &str = "app.kubernetes.io/name";
|
||||
/// The text of the gate's refusal on a volume that holds files and no marker.
|
||||
const GATE_NONEMPTY: &str = "holds files and no .hyperhive-migrated marker";
|
||||
|
||||
#[derive(Args)]
|
||||
#[command(args_conflicts_with_subcommands = true)]
|
||||
|
|
@ -43,8 +34,8 @@ pub struct MigrateArgs {
|
|||
|
||||
#[derive(Subcommand)]
|
||||
enum MigrateVerb {
|
||||
/// Report where each service stands: its runtime, its host units, its pod
|
||||
/// and the start gate's answer, and which step comes next.
|
||||
/// Report where each service stands: its runtime, its host units and its pod,
|
||||
/// and which step comes next.
|
||||
///
|
||||
/// Read-only: it creates no pod and changes nothing. Run it as root; it
|
||||
/// reads `/etc/hyperhive/migrate.json`, asks systemd about the host units
|
||||
|
|
@ -54,7 +45,7 @@ enum MigrateVerb {
|
|||
/// Exits 0 when every check ran, whatever the answers, and 1 when a
|
||||
/// check failed to run. A script reads the report from `--json`.
|
||||
Status(StatusArgs),
|
||||
/// Moves a service. Not available yet: only `status` is.
|
||||
/// Any other word: only `status` exists.
|
||||
#[command(external_subcommand)]
|
||||
External(Vec<String>),
|
||||
}
|
||||
|
|
@ -119,33 +110,6 @@ pub(crate) struct Service {
|
|||
pub(crate) runtime: Runtime,
|
||||
/// The units that run the service while it is on the host.
|
||||
pub(crate) host_units: Vec<String>,
|
||||
/// Null for a service with no state volume.
|
||||
pub(crate) state: Option<State>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct State {
|
||||
pub(crate) claim: String,
|
||||
/// Where the pod mounts the claim.
|
||||
pub(crate) mount: String,
|
||||
/// Where the data lives on the host; null where nothing is copied.
|
||||
pub(crate) host_path: Option<String>,
|
||||
/// Paths under `host_path` a copy leaves out.
|
||||
pub(crate) excludes: Vec<String>,
|
||||
}
|
||||
|
||||
/// What the pod's start gate has answered.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) enum Gate {
|
||||
/// Exited 0: the marker is in the volume.
|
||||
Passed,
|
||||
/// Exited non-zero, with its termination message.
|
||||
Refused(String),
|
||||
/// Not finished yet.
|
||||
Pending,
|
||||
/// The pod has no state gate.
|
||||
Absent,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -155,7 +119,6 @@ pub(crate) struct Pod {
|
|||
pub(crate) created: String,
|
||||
pub(crate) phase: String,
|
||||
pub(crate) ready: bool,
|
||||
pub(crate) gate: Gate,
|
||||
}
|
||||
|
||||
impl Pod {
|
||||
|
|
@ -184,8 +147,6 @@ pub(crate) enum Kind {
|
|||
Runtime,
|
||||
/// The service has moved before one that comes first.
|
||||
Order,
|
||||
/// The start gate's marker.
|
||||
Copy,
|
||||
/// The pod runs.
|
||||
Ready,
|
||||
}
|
||||
|
|
@ -196,7 +157,6 @@ impl Kind {
|
|||
Self::Config => "config",
|
||||
Self::Runtime => "runtime",
|
||||
Self::Order => "order",
|
||||
Self::Copy => "copy",
|
||||
Self::Ready => "ready",
|
||||
}
|
||||
}
|
||||
|
|
@ -288,8 +248,7 @@ fn finished(name: &str, service: &Service, observed: &Observed) -> bool {
|
|||
let newest = pods_of(name, pods).first().copied().cloned();
|
||||
service.runtime == Runtime::K8s
|
||||
&& host_units(service, &observed.units) == HostUnits::Stopped
|
||||
&& newest
|
||||
.is_some_and(|p| p.is_ready() && (service.state.is_none() || p.gate == Gate::Passed))
|
||||
&& newest.is_some_and(|p| p.is_ready())
|
||||
}
|
||||
|
||||
/// Judge every service from what was observed, in the order of moves.
|
||||
|
|
@ -338,13 +297,6 @@ pub(crate) fn classify(observed: &Observed) -> Vec<Step> {
|
|||
)),
|
||||
});
|
||||
}
|
||||
if service.state.is_some() {
|
||||
steps.push(Step {
|
||||
service: (*name).clone(),
|
||||
kind: Kind::Copy,
|
||||
status: copy_status(service, &units, &pods, newest),
|
||||
});
|
||||
}
|
||||
steps.push(Step {
|
||||
service: (*name).clone(),
|
||||
kind: Kind::Ready,
|
||||
|
|
@ -386,33 +338,6 @@ fn runtime_status(service: &Service, units: &HostUnits, pods: &Pods) -> Status {
|
|||
}
|
||||
}
|
||||
|
||||
fn copy_status(service: &Service, units: &HostUnits, pods: &Pods, newest: Option<&Pod>) -> Status {
|
||||
if service.runtime == Runtime::Host {
|
||||
return Status::NotDone("after the cut-over rebuild".into());
|
||||
}
|
||||
if let HostUnits::Running(unit, state) = units {
|
||||
return Status::Blocked(format!(
|
||||
"host unit {unit} is still {state}: a copy refuses until it stops"
|
||||
));
|
||||
}
|
||||
if let Err(why) = pods {
|
||||
return Status::Unknown(format!("pods: {why}"));
|
||||
}
|
||||
let Some(pod) = newest else {
|
||||
return Status::NotDone("no pod yet: the rebuild has not applied the workload".into());
|
||||
};
|
||||
match &pod.gate {
|
||||
Gate::Passed => Status::Done(format!("marker present ({})", pod.name)),
|
||||
Gate::Refused(message) if message.contains(GATE_NONEMPTY) => Status::NotDone(format!(
|
||||
"the volume holds files and no marker, so a copy was cut off or the volume was \
|
||||
written to: {message}"
|
||||
)),
|
||||
Gate::Refused(message) => Status::NotDone(format!("waiting for the copy: {message}")),
|
||||
Gate::Pending => Status::NotDone(format!("the gate of {} has not answered yet", pod.name)),
|
||||
Gate::Absent => Status::Unknown(format!("{} has no {GATE_CONTAINER} container", pod.name)),
|
||||
}
|
||||
}
|
||||
|
||||
fn ready_status(service: &Service, pods: &Pods, newest: Option<&Pod>) -> Status {
|
||||
if service.runtime == Runtime::Host {
|
||||
return Status::NotDone("not in the cluster".into());
|
||||
|
|
@ -423,9 +348,6 @@ fn ready_status(service: &Service, pods: &Pods, newest: Option<&Pod>) -> Status
|
|||
match newest {
|
||||
None => Status::NotDone("no pod".into()),
|
||||
Some(pod) if pod.is_ready() => Status::Done(format!("{} is {}", pod.name, pod.phase)),
|
||||
Some(pod) if matches!(pod.gate, Gate::Refused(_)) => {
|
||||
Status::NotDone(format!("{} waits behind its start gate", pod.name))
|
||||
}
|
||||
Some(pod) => Status::NotDone(format!("{} is {}, not Ready", pod.name, pod.phase)),
|
||||
}
|
||||
}
|
||||
|
|
@ -531,49 +453,11 @@ pub(crate) fn parse_pods(json: &str) -> Result<Vec<Pod>, String> {
|
|||
c.iter()
|
||||
.any(|c| c["type"] == "Ready" && c["status"] == "True")
|
||||
}),
|
||||
gate: gate_of(pod),
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn gate_of(pod: &Value) -> Gate {
|
||||
let declared = pod["spec"]["initContainers"]
|
||||
.as_array()
|
||||
.is_some_and(|c| c.iter().any(|c| c["name"] == GATE_CONTAINER));
|
||||
if !declared {
|
||||
return Gate::Absent;
|
||||
}
|
||||
let Some(status) = pod["status"]["initContainerStatuses"]
|
||||
.as_array()
|
||||
.and_then(|c| c.iter().find(|c| c["name"] == GATE_CONTAINER))
|
||||
else {
|
||||
return Gate::Pending;
|
||||
};
|
||||
let refused = |t: &Value| {
|
||||
Gate::Refused(
|
||||
t["message"]
|
||||
.as_str()
|
||||
.map(str::trim)
|
||||
.filter(|m| !m.is_empty())
|
||||
.map_or_else(|| format!("exit {}", t["exitCode"]), str::to_owned),
|
||||
)
|
||||
};
|
||||
let terminated = &status["state"]["terminated"];
|
||||
if terminated.is_object() {
|
||||
return if terminated["exitCode"] == 0 {
|
||||
Gate::Passed
|
||||
} else {
|
||||
refused(terminated)
|
||||
};
|
||||
}
|
||||
let last = &status["lastState"]["terminated"];
|
||||
if status["state"]["waiting"].is_object() && last.is_object() && last["exitCode"] != 0 {
|
||||
return refused(last);
|
||||
}
|
||||
Gate::Pending
|
||||
}
|
||||
|
||||
/// The report as text.
|
||||
fn render(steps: &[Step]) -> String {
|
||||
let mut out = String::new();
|
||||
|
|
@ -651,13 +535,10 @@ pub fn run(args: MigrateArgs) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
Some(MigrateVerb::External(words)) => bail!(
|
||||
"`swarmctl migrate {}` is not available yet; only `swarmctl migrate status` is",
|
||||
"`swarmctl migrate {}` is not a verb; only `swarmctl migrate status` exists",
|
||||
words.join(" ")
|
||||
),
|
||||
None => bail!(
|
||||
"`swarmctl migrate` does not move services yet: only `swarmctl migrate status` is \
|
||||
available"
|
||||
),
|
||||
None => bail!("`swarmctl migrate` needs a verb: only `swarmctl migrate status` exists"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -681,28 +562,21 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::tests::write_executable;
|
||||
|
||||
fn service(option: &str, runtime: Runtime, units: &[&str], state: bool) -> Service {
|
||||
fn service(option: &str, runtime: Runtime, units: &[&str]) -> Service {
|
||||
Service {
|
||||
option: format!("services.hyperhive.deploy.{option}.runtime"),
|
||||
runtime,
|
||||
host_units: units.iter().map(|u| (*u).to_owned()).collect(),
|
||||
state: state.then(|| State {
|
||||
claim: "x-state".into(),
|
||||
mount: "/var/lib/x".into(),
|
||||
host_path: Some("/var/lib/x".into()),
|
||||
excludes: vec![],
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn pod(service: &str, created: &str, ready: bool, gate: Gate) -> Pod {
|
||||
fn pod(service: &str, created: &str, ready: bool) -> Pod {
|
||||
Pod {
|
||||
name: format!("{service}-{created}"),
|
||||
service: service.into(),
|
||||
created: created.into(),
|
||||
phase: "Running".into(),
|
||||
ready,
|
||||
gate,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -737,11 +611,11 @@ mod tests {
|
|||
const CI: &str = "swarm-ci-metrics";
|
||||
const CI_UNITS: [&str; 2] = ["swarm-ci-metrics.timer", "swarm-ci-metrics.service"];
|
||||
|
||||
/// One stateful service, ci-metrics, with its two host units and the
|
||||
/// given runtime, host-unit state and pods.
|
||||
/// One service, ci-metrics, with its two host units and the given
|
||||
/// runtime, host-unit state and pods.
|
||||
fn ci(runtime: Runtime, unit_state: &str, pods: Vec<Pod>) -> Vec<Step> {
|
||||
classify(&observed(
|
||||
vec![(CI, service("ci-metrics", runtime, &CI_UNITS, true))],
|
||||
vec![(CI, service("ci-metrics", runtime, &CI_UNITS))],
|
||||
&[
|
||||
("swarm-ci-metrics.timer", unit_state),
|
||||
("swarm-ci-metrics.service", "inactive"),
|
||||
|
|
@ -777,10 +651,6 @@ mod tests {
|
|||
.detail()
|
||||
.contains("services.hyperhive.deploy.ci-metrics.runtime")
|
||||
);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
Some(Status::NotDone(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Ready),
|
||||
Some(Status::NotDone(_))
|
||||
|
|
@ -789,102 +659,36 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn host_unit_still_active_after_the_switch_blocks_the_copy() {
|
||||
fn host_unit_still_active_after_the_switch_is_not_a_finished_switch() {
|
||||
let steps = ci(Runtime::K8s, "active", vec![]);
|
||||
assert!(detail(step(&steps, CI, Kind::Runtime)).contains("still active"));
|
||||
let copy = step(&steps, CI, Kind::Copy).unwrap();
|
||||
assert!(matches!(copy, Status::Blocked(_)), "{copy:?}");
|
||||
// Control: stopped, the same observation is not blocked.
|
||||
let runtime = step(&steps, CI, Kind::Runtime).unwrap();
|
||||
assert!(matches!(runtime, Status::NotDone(_)), "{runtime:?}");
|
||||
assert!(runtime.detail().contains("still active"));
|
||||
// Control: stopped, the same observation is done.
|
||||
let steps = ci(Runtime::K8s, "inactive", vec![]);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Runtime),
|
||||
Some(Status::Done(_))
|
||||
));
|
||||
assert!(!matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
Some(Status::Blocked(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_unit_that_is_activating_counts_as_running() {
|
||||
let steps = ci(Runtime::K8s, "activating", vec![]);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
Some(Status::Blocked(_))
|
||||
));
|
||||
let runtime = step(&steps, CI, Kind::Runtime).unwrap();
|
||||
assert!(matches!(runtime, Status::NotDone(_)), "{runtime:?}");
|
||||
assert!(runtime.detail().contains("still activating"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rebuilt_without_a_pod_waits_for_the_workload() {
|
||||
let steps = ci(Runtime::K8s, "inactive", vec![]);
|
||||
assert!(detail(step(&steps, CI, Kind::Copy)).contains("no pod yet"));
|
||||
assert_eq!(detail(step(&steps, CI, Kind::Ready)), "no pod");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gate_refused_on_an_empty_volume_waits_for_the_copy() {
|
||||
let refusal = "swarm-ci-metrics: state volume has not been migrated: run `swarmctl migrate swarm-ci-metrics`";
|
||||
let steps = ci(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(CI, "1", false, Gate::Refused(refusal.into()))],
|
||||
);
|
||||
let copy = step(&steps, CI, Kind::Copy).unwrap();
|
||||
assert!(matches!(copy, Status::NotDone(_)));
|
||||
assert!(
|
||||
copy.detail().starts_with("waiting for the copy"),
|
||||
"{copy:?}"
|
||||
);
|
||||
assert!(copy.detail().contains(refusal));
|
||||
assert!(detail(step(&steps, CI, Kind::Ready)).contains("start gate"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gate_refused_on_files_without_a_marker_reports_a_cut_off_copy() {
|
||||
let refusal = "swarm-ci-metrics: state volume has not been migrated: it holds files and no .hyperhive-migrated marker, so a copy may have been cut off";
|
||||
let steps = ci(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(CI, "1", false, Gate::Refused(refusal.into()))],
|
||||
);
|
||||
let copy = detail(step(&steps, CI, Kind::Copy));
|
||||
assert!(
|
||||
copy.starts_with("the volume holds files and no marker"),
|
||||
"{copy}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gate_that_has_not_answered_and_gate_absent() {
|
||||
let steps = ci(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(CI, "1", false, Gate::Pending)],
|
||||
);
|
||||
assert!(detail(step(&steps, CI, Kind::Copy)).contains("not answered"));
|
||||
let steps = ci(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(CI, "1", false, Gate::Absent)],
|
||||
);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
Some(Status::Unknown(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn marker_present_but_pod_not_ready() {
|
||||
let steps = ci(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(CI, "1", false, Gate::Passed)],
|
||||
);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
Some(Status::Done(_))
|
||||
));
|
||||
fn a_pod_that_is_not_ready_is_the_next_step() {
|
||||
let steps = ci(Runtime::K8s, "inactive", vec![pod(CI, "1", false)]);
|
||||
let ready = step(&steps, CI, Kind::Ready).unwrap();
|
||||
assert!(matches!(ready, Status::NotDone(_)));
|
||||
assert!(ready.detail().contains("not Ready"));
|
||||
|
|
@ -892,12 +696,8 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn marker_present_and_ready_is_everything_done() {
|
||||
let steps = ci(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(CI, "1", true, Gate::Passed)],
|
||||
);
|
||||
fn a_ready_pod_with_the_host_units_stopped_is_everything_done() {
|
||||
let steps = ci(Runtime::K8s, "inactive", vec![pod(CI, "1", true)]);
|
||||
assert!(
|
||||
steps.iter().all(|s| matches!(s.status, Status::Done(_))),
|
||||
"{steps:?}"
|
||||
|
|
@ -908,7 +708,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn a_completed_cronjob_pod_is_ready() {
|
||||
let mut done = pod(CI, "1", false, Gate::Passed);
|
||||
let mut done = pod(CI, "1", false);
|
||||
done.phase = "Succeeded".into();
|
||||
let steps = ci(Runtime::K8s, "inactive", vec![done]);
|
||||
assert!(matches!(
|
||||
|
|
@ -919,22 +719,18 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn the_newest_pod_decides() {
|
||||
let old = pod(CI, "1", true, Gate::Passed);
|
||||
let new = pod(CI, "2", false, Gate::Refused("refused".into()));
|
||||
let old = pod(CI, "1", true);
|
||||
let new = pod(CI, "2", false);
|
||||
let steps = ci(Runtime::K8s, "inactive", vec![old, new]);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
step(&steps, CI, Kind::Ready),
|
||||
Some(Status::NotDone(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pod_for_a_service_still_on_the_host_is_the_wrong_runtime() {
|
||||
let steps = ci(
|
||||
Runtime::Host,
|
||||
"active",
|
||||
vec![pod(CI, "1", false, Gate::Pending)],
|
||||
);
|
||||
let steps = ci(Runtime::Host, "active", vec![pod(CI, "1", false)]);
|
||||
let runtime = step(&steps, CI, Kind::Runtime).unwrap();
|
||||
assert!(runtime.detail().starts_with("wrong runtime"), "{runtime:?}");
|
||||
// Control: with no pod the same observation is the plain host case.
|
||||
|
|
@ -942,30 +738,6 @@ mod tests {
|
|||
assert!(!detail(step(&steps, CI, Kind::Runtime)).contains("wrong runtime"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stateless_service_has_no_copy_step() {
|
||||
let router = "swarm-notify-router";
|
||||
let steps = classify(&observed(
|
||||
vec![(
|
||||
router,
|
||||
service(
|
||||
"notify-router",
|
||||
Runtime::K8s,
|
||||
&["container@swarm-notify-router.service"],
|
||||
false,
|
||||
),
|
||||
)],
|
||||
&[("container@swarm-notify-router.service", "inactive")],
|
||||
vec![pod(router, "1", true, Gate::Absent)],
|
||||
));
|
||||
assert!(step(&steps, router, Kind::Copy).is_none());
|
||||
assert!(matches!(
|
||||
step(&steps, router, Kind::Ready),
|
||||
Some(Status::Done(_))
|
||||
));
|
||||
assert!(next(&steps).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moving_ahead_of_an_earlier_service_is_out_of_order() {
|
||||
let router = "swarm-notify-router";
|
||||
|
|
@ -975,16 +747,16 @@ mod tests {
|
|||
vec![
|
||||
(
|
||||
router,
|
||||
service("notify-router", router_runtime, &[router_unit], false),
|
||||
service("notify-router", router_runtime, &[router_unit]),
|
||||
),
|
||||
(CI, service("ci-metrics", Runtime::K8s, &CI_UNITS, true)),
|
||||
(CI, service("ci-metrics", Runtime::K8s, &CI_UNITS)),
|
||||
],
|
||||
&[
|
||||
(router_unit, router_unit_state),
|
||||
("swarm-ci-metrics.timer", "inactive"),
|
||||
("swarm-ci-metrics.service", "inactive"),
|
||||
],
|
||||
[router_pods, vec![pod(CI, "1", false, Gate::Pending)]].concat(),
|
||||
[router_pods, vec![pod(CI, "1", false)]].concat(),
|
||||
))
|
||||
};
|
||||
let steps = build(Runtime::Host, "active", vec![]);
|
||||
|
|
@ -993,11 +765,7 @@ mod tests {
|
|||
assert!(order.detail().contains(router), "{order:?}");
|
||||
assert!(step(&steps, router, Kind::Order).is_none());
|
||||
// Control: with the router finished, ci-metrics is in order.
|
||||
let steps = build(
|
||||
Runtime::K8s,
|
||||
"inactive",
|
||||
vec![pod(router, "1", true, Gate::Absent)],
|
||||
);
|
||||
let steps = build(Runtime::K8s, "inactive", vec![pod(router, "1", true)]);
|
||||
assert!(step(&steps, CI, Kind::Order).is_none());
|
||||
}
|
||||
|
||||
|
|
@ -1008,9 +776,9 @@ mod tests {
|
|||
vec![
|
||||
(
|
||||
router,
|
||||
service("notify-router", Runtime::Host, &["a.service"], false),
|
||||
service("notify-router", Runtime::Host, &["a.service"]),
|
||||
),
|
||||
(CI, service("ci-metrics", Runtime::Host, &CI_UNITS, true)),
|
||||
(CI, service("ci-metrics", Runtime::Host, &CI_UNITS)),
|
||||
],
|
||||
&[
|
||||
("a.service", "active"),
|
||||
|
|
@ -1036,7 +804,7 @@ mod tests {
|
|||
let steps = classify(&observed(
|
||||
services
|
||||
.iter()
|
||||
.map(|n| (*n, service(n, Runtime::Host, &["u.service"], false)))
|
||||
.map(|n| (*n, service(n, Runtime::Host, &["u.service"])))
|
||||
.collect(),
|
||||
&[("u.service", "active")],
|
||||
vec![],
|
||||
|
|
@ -1062,7 +830,7 @@ mod tests {
|
|||
#[test]
|
||||
fn failed_checks_are_unknown_not_guessed() {
|
||||
let mut o = observed(
|
||||
vec![(CI, service("ci-metrics", Runtime::K8s, &CI_UNITS, true))],
|
||||
vec![(CI, service("ci-metrics", Runtime::K8s, &CI_UNITS))],
|
||||
&[
|
||||
("swarm-ci-metrics.timer", "inactive"),
|
||||
("swarm-ci-metrics.service", "inactive"),
|
||||
|
|
@ -1071,16 +839,12 @@ mod tests {
|
|||
);
|
||||
o.pods = Err("kubectl get pods failed (exit status: 1): refused".into());
|
||||
let steps = classify(&o);
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Copy),
|
||||
Some(Status::Unknown(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
step(&steps, CI, Kind::Ready),
|
||||
Some(Status::Unknown(_))
|
||||
));
|
||||
let mut o = observed(
|
||||
vec![(CI, service("ci-metrics", Runtime::K8s, &CI_UNITS, true))],
|
||||
vec![(CI, service("ci-metrics", Runtime::K8s, &CI_UNITS))],
|
||||
&[("swarm-ci-metrics.service", "inactive")],
|
||||
vec![],
|
||||
);
|
||||
|
|
@ -1093,23 +857,13 @@ mod tests {
|
|||
));
|
||||
}
|
||||
|
||||
/// The shape of `kubectl get pods --output json` for a pod behind a start
|
||||
/// gate, written from the API's schema.
|
||||
fn pod_json(name: &str, gate_state: &str, ready: &str, with_gate: bool) -> String {
|
||||
let init_spec = if with_gate {
|
||||
r#","initContainers":[{"name":"state-gate"}]"#
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let init_status = if with_gate {
|
||||
format!(r#","initContainerStatuses":[{{"name":"state-gate",{gate_state}}}]"#)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
/// The shape of `kubectl get pods --output json` for one pod, written from
|
||||
/// the API's schema.
|
||||
fn pod_json(name: &str, ready: &str) -> String {
|
||||
format!(
|
||||
r#"{{"metadata":{{"name":"{name}","creationTimestamp":"2026-11-22T20:00:00Z","labels":{{"app.kubernetes.io/name":"swarm-ci-metrics","app.kubernetes.io/part-of":"hyperhive"}}}},
|
||||
"spec":{{"containers":[{{"name":"swarm-ci-metrics"}}]{init_spec}}},
|
||||
"status":{{"phase":"Pending","conditions":[{{"type":"Ready","status":"{ready}"}}]{init_status}}}}}"#
|
||||
"spec":{{"containers":[{{"name":"swarm-ci-metrics"}}]}},
|
||||
"status":{{"phase":"Pending","conditions":[{{"type":"Ready","status":"{ready}"}}]}}}}"#
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -1120,48 +874,14 @@ mod tests {
|
|||
)
|
||||
}
|
||||
|
||||
const GATE_PASSED: &str = r#""state":{"terminated":{"exitCode":0,"reason":"Completed"}}"#;
|
||||
const GATE_REFUSED_BACKOFF: &str = r#""state":{"waiting":{"reason":"PodInitializing"}},"lastState":{"terminated":{"exitCode":1,"reason":"Error","message":"swarm-ci-metrics: state volume has not been migrated: run `swarmctl migrate swarm-ci-metrics`\n"}}"#;
|
||||
const GATE_REFUSED_NOW: &str =
|
||||
r#""state":{"terminated":{"exitCode":1,"reason":"Error","message":"refused now"}}"#;
|
||||
const GATE_RUNNING: &str = r#""state":{"running":{}}"#;
|
||||
|
||||
#[test]
|
||||
fn parses_a_gate_that_passed() {
|
||||
let pods = parse_pods(&list(&[pod_json("p1", GATE_PASSED, "True", true)])).unwrap();
|
||||
fn parses_a_pod_with_its_service_and_readiness() {
|
||||
let pods = parse_pods(&list(&[pod_json("p1", "True")])).unwrap();
|
||||
assert_eq!(pods.len(), 1);
|
||||
assert_eq!(pods[0].service, "swarm-ci-metrics");
|
||||
assert_eq!(pods[0].gate, Gate::Passed);
|
||||
assert!(pods[0].ready);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_a_gate_that_refused_with_its_message() {
|
||||
let pods = parse_pods(&list(&[pod_json(
|
||||
"p1",
|
||||
GATE_REFUSED_BACKOFF,
|
||||
"False",
|
||||
true,
|
||||
)]))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
pods[0].gate,
|
||||
Gate::Refused(
|
||||
"swarm-ci-metrics: state volume has not been migrated: run `swarmctl migrate swarm-ci-metrics`"
|
||||
.into()
|
||||
)
|
||||
);
|
||||
let pods = parse_pods(&list(&[pod_json("p1", "False")])).unwrap();
|
||||
assert!(!pods[0].ready);
|
||||
let pods = parse_pods(&list(&[pod_json("p1", GATE_REFUSED_NOW, "False", true)])).unwrap();
|
||||
assert_eq!(pods[0].gate, Gate::Refused("refused now".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_a_running_gate_and_a_pod_without_one() {
|
||||
let pods = parse_pods(&list(&[pod_json("p1", GATE_RUNNING, "False", true)])).unwrap();
|
||||
assert_eq!(pods[0].gate, Gate::Pending);
|
||||
let pods = parse_pods(&list(&[pod_json("p1", "", "True", false)])).unwrap();
|
||||
assert_eq!(pods[0].gate, Gate::Absent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1210,12 +930,6 @@ esac
|
|||
"option": "services.hyperhive.deploy.ci-metrics.runtime",
|
||||
"runtime": "k8s",
|
||||
"hostUnits": CI_UNITS,
|
||||
"state": {
|
||||
"claim": "swarm-ci-metrics-state",
|
||||
"mount": "/var/lib/swarm-ci-metrics",
|
||||
"hostPath": "/var/lib/private/swarm-ci-metrics",
|
||||
"excludes": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1248,10 +962,7 @@ esac
|
|||
|
||||
#[test]
|
||||
fn only_reads_and_only_in_the_swarms_namespace() {
|
||||
let fx = Fixture::new(
|
||||
"readonly",
|
||||
&list(&[pod_json("p1", GATE_PASSED, "True", true)]),
|
||||
);
|
||||
let fx = Fixture::new("readonly", &list(&[pod_json("p1", "True")]));
|
||||
let mut out = Vec::new();
|
||||
assert!(report(&fx.tools, false, &mut out).unwrap());
|
||||
let log = fx.log();
|
||||
|
|
@ -1282,9 +993,8 @@ esac
|
|||
]
|
||||
);
|
||||
// Control: the log does record calls, and the report read the stubs'
|
||||
// answers (a ready pod behind a passed gate).
|
||||
// answers (a ready pod).
|
||||
let text = String::from_utf8(out).unwrap();
|
||||
assert!(text.contains("marker present"), "{text}");
|
||||
assert!(
|
||||
text.contains("every service has moved and is Ready"),
|
||||
"{text}"
|
||||
|
|
@ -1315,28 +1025,18 @@ esac
|
|||
|
||||
#[test]
|
||||
fn json_report_carries_services_steps_and_next() {
|
||||
let fx = Fixture::new(
|
||||
"json",
|
||||
&list(&[pod_json("p1", GATE_REFUSED_BACKOFF, "False", true)]),
|
||||
);
|
||||
let fx = Fixture::new("json", &list(&[pod_json("p1", "False")]));
|
||||
let mut out = Vec::new();
|
||||
report(&fx.tools, true, &mut out).unwrap();
|
||||
let v: Value = serde_json::from_slice(&out).unwrap();
|
||||
assert_eq!(v["namespace"], "hyperhive");
|
||||
assert_eq!(
|
||||
v["services"][CI]["state"]["claim"],
|
||||
"swarm-ci-metrics-state"
|
||||
);
|
||||
assert_eq!(v["services"][CI]["runtime"], "k8s");
|
||||
assert!(v["services"][CI].get("state").is_none());
|
||||
let steps = v["steps"].as_array().unwrap();
|
||||
let copy = steps.iter().find(|s| s["step"] == "copy").unwrap();
|
||||
assert_eq!(copy["status"], "not_done");
|
||||
assert!(
|
||||
copy["detail"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains("swarmctl migrate")
|
||||
);
|
||||
assert_eq!(v["next"]["step"], "copy");
|
||||
let ready = steps.iter().find(|s| s["step"] == "ready").unwrap();
|
||||
assert_eq!(ready["status"], "not_done");
|
||||
assert!(ready["detail"].as_str().unwrap().contains("not Ready"));
|
||||
assert_eq!(v["next"]["step"], "ready");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue