Skip to content

Commit

Permalink
Merge pull request #1538 from tjkirch/refactor-ctrd-configs
Browse files Browse the repository at this point in the history
containerd: share duplicate configs
  • Loading branch information
tjkirch authored Apr 30, 2021
2 parents 5052c31 + 4fdc69f commit 2587bda
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 37 deletions.
1 change: 1 addition & 0 deletions Release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ version = "1.0.8"
"migrate_v1.1.0_kubelet-server-tls-bootstrap.lz4",
"migrate_v1.1.0_kubelet-cloud-provider.lz4",
"migrate_v1.1.0_kubelet-registry-qps-registry-burst.lz4",
"migrate_v1.1.0_shared-containerd-configs.lz4",
]
14 changes: 0 additions & 14 deletions packages/containerd/containerd-config-toml_aws-ecs-1

This file was deleted.

14 changes: 0 additions & 14 deletions packages/containerd/containerd-config-toml_vmware-dev

This file was deleted.

8 changes: 3 additions & 5 deletions packages/containerd/containerd.spec
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ License: Apache-2.0
URL: https://%{goimport}
Source0: https://%{goimport}/archive/v%{gover}/%{gorepo}-%{gover}.tar.gz
Source1: containerd.service
Source2: containerd-config-toml_aws-k8s
Source3: containerd-config-toml_aws-dev
Source4: containerd-config-toml_aws-ecs-1
Source2: containerd-config-toml_k8s
Source3: containerd-config-toml_basic
Source5: containerd-tmpfiles.conf
Source6: containerd-config-toml_vmware-dev
Source1000: clarify.toml

# TODO: submit this upstream.
Expand Down Expand Up @@ -84,7 +82,7 @@ install -p -m 0644 %{S:1} %{buildroot}%{_cross_unitdir}/containerd.service

install -d %{buildroot}%{_cross_templatedir}
install -d %{buildroot}%{_cross_factorydir}%{_cross_sysconfdir}/containerd
install -p -m 0644 %{S:2} %{S:3} %{S:4} %{S:6} %{buildroot}%{_cross_templatedir}
install -p -m 0644 %{S:2} %{S:3} %{buildroot}%{_cross_templatedir}

install -d %{buildroot}%{_cross_tmpfilesdir}
install -p -m 0644 %{S:5} %{buildroot}%{_cross_tmpfilesdir}/containerd.conf
Expand Down
9 changes: 9 additions & 0 deletions sources/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ members = [
"api/migration/migrations/v1.1.0/kubelet-server-tls-bootstrap",
"api/migration/migrations/v1.1.0/kubelet-cloud-provider",
"api/migration/migrations/v1.1.0/kubelet-registry-qps-registry-burst",
"api/migration/migrations/v1.1.0/shared-containerd-configs",

"bottlerocket-release",

Expand Down
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"
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);
}
}
2 changes: 1 addition & 1 deletion sources/models/src/aws-dev/defaults.d/50-aws-dev.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[configuration-files.containerd-config-toml]
# No override to path
template-path = "/usr/share/templates/containerd-config-toml_aws-dev"
template-path = "/usr/share/templates/containerd-config-toml_basic"

# Docker
[services.docker]
Expand Down
2 changes: 1 addition & 1 deletion sources/models/src/aws-ecs-1/defaults.d/50-aws-ecs-1.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[configuration-files.containerd-config-toml]
# No override to path
template-path = "/usr/share/templates/containerd-config-toml_aws-ecs-1"
template-path = "/usr/share/templates/containerd-config-toml_basic"

# Docker
[services.docker]
Expand Down
2 changes: 1 addition & 1 deletion sources/models/src/aws-k8s-1.19/defaults.d/50-aws-k8s.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[configuration-files.containerd-config-toml]
# No override to path
template-path = "/usr/share/templates/containerd-config-toml_aws-k8s"
template-path = "/usr/share/templates/containerd-config-toml_k8s"

# Kubernetes.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[configuration-files.containerd-config-toml]
# No override to path
template-path = "/usr/share/templates/containerd-config-toml_vmware-dev"
template-path = "/usr/share/templates/containerd-config-toml_basic"

# Docker
[services.docker]
Expand Down

0 comments on commit 2587bda

Please sign in to comment.