Skip to content

Commit

Permalink
Merge pull request #712 from amazonlinux/replace-setting-migration-he…
Browse files Browse the repository at this point in the history
…lper

migrations: add `ReplaceStringMigration` migration helper, simplify containerd-config-path migration, host-containers-version migration
  • Loading branch information
etungsten authored Feb 7, 2020
2 parents 9f52acf + 717cefd commit 79d0331
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 77 deletions.
2 changes: 1 addition & 1 deletion workspaces/api/migration/migration-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
apiserver = { path = "../../apiserver" }
snafu = "0.6"
toml = "0.5"
serde_json = "1.0"

[dev-dependencies]
maplit = "1.0"
serde_json = "1.0"
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,68 @@ impl Migration for RemoveSettingMigration {
Ok(input)
}
}

// =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^=

/// We use this migration when we replace a setting's old string value with a new string value.
pub struct ReplaceStringMigration {
pub setting: &'static str,
pub old_val: &'static str,
pub new_val: &'static str,
}

impl Migration for ReplaceStringMigration {
fn forward(&mut self, mut input: MigrationData) -> Result<MigrationData> {
if let Some(data) = input.data.get_mut(self.setting) {
match data {
serde_json::Value::String(data) => {
if data == self.old_val {
*data = self.new_val.to_owned();
println!(
"Changed value of '{}' from '{}' to '{}' on upgrade",
self.setting, self.old_val, self.new_val
);
} else {
println!("'{}' is not set to '{}', leaving alone", self.setting, self.old_val);
}
}
_ => {
println!(
"'{}' is set to non-string value '{}'; ReplaceStringMigration only handles strings",
self.setting, data
);
}
}
} else {
println!("Found no '{}' to change on upgrade", self.setting);
}
Ok(input)
}

fn backward(&mut self, mut input: MigrationData) -> Result<MigrationData> {
if let Some(data) = input.data.get_mut(self.setting) {
match data {
serde_json::Value::String(data) => {
if data == self.new_val {
*data = self.old_val.to_owned();
println!(
"Changed value of '{}' from '{}' to '{}' on downgrade",
self.setting, self.new_val, self.old_val
);
} else {
println!("'{}' is not set to '{}', leaving alone", self.setting, self.new_val);
}
}
_ => {
println!(
"'{}' is set to non-string value '{}'; ReplaceStringMigration only handles strings",
self.setting, data
);
}
}
} else {
println!("Found no '{}' to change on downgrade", self.setting);
}
Ok(input)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![deny(rust_2018_idioms)]

use migration_helpers::{migrate, Migration, MigrationData, Result};
use migration_helpers::{migrate, Result};
use migration_helpers::common_migrations::ReplaceStringMigration;
use std::process;

/// We bumped the versions of the default admin container and the default control container from v0.1 to v0.2
struct HostContainersVersionMigration;
const DEFAULT_ADMIN_CTR_IMG_OLD: &str =
"328549459982.dkr.ecr.us-west-2.amazonaws.com/thar-admin:v0.1";
const DEFAULT_ADMIN_CTR_IMG_NEW: &str =
Expand All @@ -14,53 +13,18 @@ const DEFAULT_CONTROL_CTR_IMG_OLD: &str =
const DEFAULT_CONTROL_CTR_IMG_NEW: &str =
"328549459982.dkr.ecr.us-west-2.amazonaws.com/thar-control:v0.2";

impl Migration for HostContainersVersionMigration {
fn forward(&mut self, mut input: MigrationData) -> Result<MigrationData> {
if let Some(admin_ctr_source) = input.data.get_mut("settings.host-containers.admin.source")
{
// Need to bump versions if the default admin container version source matches its older version
if admin_ctr_source.as_str() == Some(DEFAULT_ADMIN_CTR_IMG_OLD) {
*admin_ctr_source =
serde_json::Value::String(DEFAULT_ADMIN_CTR_IMG_NEW.to_string());
}
}
if let Some(control_ctr_source) = input
.data
.get_mut("settings.host-containers.control.source")
{
// Need to bump versions if the default control container version source matches its older version
if control_ctr_source.as_str() == Some(DEFAULT_CONTROL_CTR_IMG_OLD) {
*control_ctr_source =
serde_json::Value::String(DEFAULT_CONTROL_CTR_IMG_NEW.to_string());
}
}
Ok(input)
}

fn backward(&mut self, mut input: MigrationData) -> Result<MigrationData> {
if let Some(admin_ctr_source) = input.data.get_mut("settings.host-containers.admin.source")
{
// The default admin container v0.2 image needs OS changes adding persistent host container storage
if admin_ctr_source.as_str() == Some(DEFAULT_ADMIN_CTR_IMG_NEW) {
*admin_ctr_source =
serde_json::Value::String(DEFAULT_ADMIN_CTR_IMG_OLD.to_string());
}
}
if let Some(control_ctr_source) = input
.data
.get_mut("settings.host-containers.control.source")
{
if control_ctr_source.as_str() == Some(DEFAULT_CONTROL_CTR_IMG_NEW) {
*control_ctr_source =
serde_json::Value::String(DEFAULT_CONTROL_CTR_IMG_OLD.to_string());
}
}
Ok(input)
}
}

/// We bumped the versions of the default admin container and the default control container from v0.1 to v0.2
fn run() -> Result<()> {
migrate(HostContainersVersionMigration)
migrate(ReplaceStringMigration {
setting: "settings.host-containers.admin.source",
old_val: DEFAULT_ADMIN_CTR_IMG_OLD,
new_val: DEFAULT_ADMIN_CTR_IMG_NEW,
})?;
migrate(ReplaceStringMigration {
setting: "settings.host-containers.control.source",
old_val: DEFAULT_CONTROL_CTR_IMG_OLD,
new_val: DEFAULT_CONTROL_CTR_IMG_NEW,
})
}

// Returning a Result from main makes it print a Debug representation of the error, but with Snafu
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
#![deny(rust_2018_idioms)]

use migration_helpers::{migrate, Migration, MigrationData, Result};
use migration_helpers::{migrate, Result};
use migration_helpers::common_migrations::ReplaceStringMigration;
use std::process;

/// We changed the path to our containerd configuration template so that we could support image
/// variants with different configs. We need to update old images to the new path, and on
/// downgrade, new images to the old path.
struct ContainerdConfigPath;

const SETTING: &str = "configuration-files.containerd-config-toml.template-path";
// Old version with no variant
const DEFAULT_CTRD_CONFIG_OLD: &str = "/usr/share/templates/containerd-config-toml";
// Any users coming from old versions would be using the aws-k8s variant because no other existed :)
const DEFAULT_CTRD_CONFIG_NEW: &str = "/usr/share/templates/containerd-config-toml_aws-k8s";

impl Migration for ContainerdConfigPath {
fn forward(&mut self, mut input: MigrationData) -> Result<MigrationData> {
if let Some(cfg_path) = input.data.get_mut(SETTING) {
if cfg_path.as_str() == Some(DEFAULT_CTRD_CONFIG_OLD) {
*cfg_path = serde_json::Value::String(DEFAULT_CTRD_CONFIG_NEW.to_string());
}
}
Ok(input)
}

fn backward(&mut self, mut input: MigrationData) -> Result<MigrationData> {
if let Some(cfg_path) = input.data.get_mut(SETTING) {
if cfg_path.as_str() == Some(DEFAULT_CTRD_CONFIG_NEW) {
*cfg_path = serde_json::Value::String(DEFAULT_CTRD_CONFIG_OLD.to_string());
}
}
Ok(input)
}
}

/// We changed the path to our containerd configuration template so that we could support image
/// variants with different configs. We need to update old images to the new path, and on
/// downgrade, new images to the old path.
fn run() -> Result<()> {
migrate(ContainerdConfigPath)
migrate(ReplaceStringMigration {
setting: SETTING,
old_val: DEFAULT_CTRD_CONFIG_OLD,
new_val: DEFAULT_CTRD_CONFIG_NEW
})
}

// Returning a Result from main makes it print a Debug representation of the error, but with Snafu
Expand Down

0 comments on commit 79d0331

Please sign in to comment.