Skip to content

Commit

Permalink
start adding auto-restart to instance-create
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Sep 9, 2024
1 parent 741af37 commit 1ad54e7
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
4 changes: 1 addition & 3 deletions nexus/db-model/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ impl Instance {
ncpus: params.ncpus.into(),
memory: params.memory.into(),
hostname: params.hostname.to_string(),
// TODO(eliza): allow this to be configured via the instance-create
// params...
auto_restart_policy: None,
auto_restart_policy: params.auto_restart_policy.clone().into(),
runtime_state,

updater_gen: Generation::new(),
Expand Down
25 changes: 24 additions & 1 deletion nexus/db-model/src/instance_auto_restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::impl_enum_type;
use nexus_types::external_api::params;
use serde::Deserialize;
use serde::Serialize;
use std::fmt;
Expand Down Expand Up @@ -34,7 +35,29 @@ impl InstanceAutoRestart {

impl fmt::Display for InstanceAutoRestart {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.label())
self.label().fmt(f)
}
}

impl From<InstanceAutoRestart> for params::InstanceAutoRestart {
fn from(value: InstanceAutoRestart) -> Self {
match value {
InstanceAutoRestart::Never => Self::Never,
InstanceAutoRestart::SledFailuresOnly => Self::SledFailuresOnly,
InstanceAutoRestart::AllFailures => Self::AllFailures,
}
}
}

impl From<params::InstanceAutoRestart> for InstanceAutoRestart {
fn from(value: params::InstanceAutoRestart) -> Self {
match value {
params::InstanceAutoRestart::Never => Self::Never,
params::InstanceAutoRestart::SledFailuresOnly => {
Self::SledFailuresOnly
}
params::InstanceAutoRestart::AllFailures => Self::AllFailures,
}
}
}

Expand Down
1 change: 1 addition & 0 deletions nexus/db-queries/src/db/datastore/vpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,7 @@ mod tests {
disks: vec![],
ssh_public_keys: None,
start: false,
..Default::default()
},
),
)
Expand Down
34 changes: 34 additions & 0 deletions nexus/types/src/external_api/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,32 @@ pub enum ExternalIpDetach {
Floating { floating_ip: NameOrId },
}

/// A policy determining when an instance should be automatically restarted by
/// the control plane.
#[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum InstanceAutoRestart {
/// The instance should not be automatically restarted by the control plane
/// if it failures.
Never,
/// The instance should be automatically restarted by the control plane if
/// the sled that it's running on reboots or fails, but it should not be
/// restarted automatically if the individual instance fails. This policy
/// will only automatically restart an instance if the control plane is able
/// to definitively determine that the instance failed due to a sled reboot
/// or fault.
///
/// This is the default policy for instances that don't specify an
/// auto-restart policy.
#[default]
SledFailuresOnly,
/// The instance should be automatically restarted by the control plane in
/// the event of any failure. If the instance crashes, or if the sled it's
/// running on reboots, the control plane will always attempt to
/// automatically restart this instance.
AllFailures,
}

/// Create-time parameters for an `Instance`
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceCreate {
Expand Down Expand Up @@ -1032,6 +1058,14 @@ pub struct InstanceCreate {
/// Should this instance be started upon creation; true by default.
#[serde(default = "bool_true")]
pub start: bool,

/// A policy that indicates whether the control plane should automatically
/// restart this instance if it fails.
///
/// If this is not provided, it defaults to the "sled_failures_only"
/// auto-restart policy.
#[serde(default)]
pub auto_restart_policy: InstanceAutoRestart,
}

#[inline]
Expand Down
61 changes: 61 additions & 0 deletions openapi/nexus.json
Original file line number Diff line number Diff line change
Expand Up @@ -15182,6 +15182,56 @@
"time_run_state_updated"
]
},
"InstanceAutoRestart": {
"description": "A policy determining when an instance should be automatically restarted by the control plane.",
"oneOf": [
{
"description": "The instance should not be automatically restarted by the control plane if it failures.",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"never"
]
}
},
"required": [
"type"
]
},
{
"description": "The instance should be automatically restarted by the control plane if the sled that it's running on reboots or fails, but it should not be restarted automatically if the individual instance fails. This policy will only automatically restart an instance if the control plane is able to definitively determine that the instance failed due to a sled reboot or fault.\n\nThis is the default policy for instances that don't specify an auto-restart policy.",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"sled_failures_only"
]
}
},
"required": [
"type"
]
},
{
"description": "The instance should be automatically restarted by the control plane in the event of any failure. If the instance crashes, or if the sled it's running on reboots, the control plane will always attempt to automatically restart this instance.",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"all_failures"
]
}
},
"required": [
"type"
]
}
]
},
"InstanceCpuCount": {
"description": "The number of CPUs in an Instance",
"type": "integer",
Expand All @@ -15192,6 +15242,17 @@
"description": "Create-time parameters for an `Instance`",
"type": "object",
"properties": {
"auto_restart_policy": {
"description": "A policy that indicates whether the control plane should automatically restart this instance if it fails.\n\nIf this is not provided, it defaults to the \"sled_failures_only\" auto-restart policy.",
"default": {
"type": "sled_failures_only"
},
"allOf": [
{
"$ref": "#/components/schemas/InstanceAutoRestart"
}
]
},
"description": {
"type": "string"
},
Expand Down

0 comments on commit 1ad54e7

Please sign in to comment.