-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1538 from tjkirch/refactor-ctrd-configs
containerd: share duplicate configs
- Loading branch information
Showing
14 changed files
with
134 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sources/api/migration/migrations/v1.1.0/shared-containerd-configs/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "shared-containerd-configs" | ||
version = "0.1.0" | ||
authors = ["Tom Kirchner <tjk@amazon.com>"] | ||
license = "Apache-2.0 OR MIT" | ||
edition = "2018" | ||
publish = false | ||
# Don't rebuild crate just because of changes to README. | ||
exclude = ["README.md"] | ||
|
||
[dependencies] | ||
lazy_static = "1.4" | ||
migration-helpers = { path = "../../../migration-helpers" } | ||
serde_json = "1.0" |
102 changes: 102 additions & 0 deletions
102
sources/api/migration/migrations/v1.1.0/shared-containerd-configs/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#![deny(rust_2018_idioms)] | ||
|
||
use lazy_static::lazy_static; | ||
use migration_helpers::{migrate, Migration, MigrationData, Result}; | ||
use std::process; | ||
|
||
const SETTING: &'static str = "configuration-files.containerd-config-toml.template-path"; | ||
|
||
lazy_static! { | ||
static ref TEMPLATE_CHANGES: &'static [(&'static str, &'static str)] = &[ | ||
( | ||
"/usr/share/templates/containerd-config-toml_aws-dev", | ||
"/usr/share/templates/containerd-config-toml_basic" | ||
), | ||
( | ||
"/usr/share/templates/containerd-config-toml_aws-ecs-1", | ||
"/usr/share/templates/containerd-config-toml_basic" | ||
), | ||
( | ||
"/usr/share/templates/containerd-config-toml_aws-k8s", | ||
"/usr/share/templates/containerd-config-toml_k8s" | ||
), | ||
( | ||
"/usr/share/templates/containerd-config-toml_vmware-dev", | ||
"/usr/share/templates/containerd-config-toml_basic" | ||
), | ||
]; | ||
} | ||
|
||
/// We refactored containerd config file templates to share data where possible, instead of | ||
/// duplicating them for variants with identical configs. thar-be-settings runs at startup and | ||
/// regenerates all files based on templates, so if we change the source during migration (early in | ||
/// boot) it'll automatically be written out based on the new template. | ||
fn run() -> Result<()> { | ||
migrate(SharedContainerdConfigs {}) | ||
} | ||
|
||
pub struct SharedContainerdConfigs {} | ||
|
||
impl SharedContainerdConfigs { | ||
fn migrate( | ||
&mut self, | ||
mut input: MigrationData, | ||
transforms: &[(&str, &str)], | ||
action: &'static str, | ||
) -> Result<MigrationData> { | ||
if let Some(data) = input.data.get_mut(SETTING) { | ||
match data { | ||
serde_json::Value::String(string) => { | ||
for (outgoing, incoming) in transforms { | ||
if string == outgoing { | ||
*data = (*incoming).into(); | ||
println!( | ||
"Changed '{}' from {:?} to {:?} on {}", | ||
SETTING, outgoing, incoming, action | ||
); | ||
// We've done what we came to do - the transformations don't | ||
// overlap, so we do one at most. (Without this, Rust knows that | ||
// we still have a reference to 'data' for another iteration, and | ||
// it won't let us change it. So smart.) | ||
break; | ||
} else { | ||
println!("'{}' is not set to {:?}, leaving alone", SETTING, outgoing); | ||
} | ||
} | ||
} | ||
_ => { | ||
println!( | ||
"'{}' is set to non-string value '{}'; SharedContainerdConfigs only handles strings", | ||
SETTING, data | ||
); | ||
} | ||
} | ||
} else { | ||
println!("Found no setting '{}'", SETTING); | ||
} | ||
|
||
Ok(input) | ||
} | ||
} | ||
|
||
impl Migration for SharedContainerdConfigs { | ||
fn forward(&mut self, input: MigrationData) -> Result<MigrationData> { | ||
self.migrate(input, *TEMPLATE_CHANGES, "upgrade") | ||
} | ||
|
||
fn backward(&mut self, input: MigrationData) -> Result<MigrationData> { | ||
let transforms: Vec<(&str, &str)> = | ||
TEMPLATE_CHANGES.iter().map(|(a, b)| (*b, *a)).collect(); | ||
self.migrate(input, &transforms, "downgrade") | ||
} | ||
} | ||
|
||
// Returning a Result from main makes it print a Debug representation of the error, but with Snafu | ||
// we have nice Display representations of the error, so we wrap "main" (run) and print any error. | ||
// https://github.com/shepmaster/snafu/issues/110 | ||
fn main() { | ||
if let Err(e) = run() { | ||
eprintln!("{}", e); | ||
process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters