-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
containerd: share duplicate configs #1538
Merged
tjkirch
merged 2 commits into
bottlerocket-os:develop
from
tjkirch:refactor-ctrd-configs
Apr 30, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a candidate for a plural version of our ReplaceStringMigration. I guess we haven't needed that yet because we haven't changed settings that differ across variants. We could also wait until we need it once more and then use this as a template for making a common migration.