From 082e231c70f2d36a64b3edf6be25e47395e87da6 Mon Sep 17 00:00:00 2001 From: Erikson Tung Date: Tue, 11 Jan 2022 13:32:12 -0800 Subject: [PATCH] vmware-variants: migrate host-container versions to latest versions We haven't been migrating host-container versions for vmware-variants. This migration migrates all previous host-container versions to the latest host-container version on upgrade. It is unclear what version of the host-containers we should downgrade to since it could be any of the older host-container versions. We can just stay on the latest host-container version since there are no breaking changes. --- Release.toml | 3 + sources/Cargo.lock | 8 ++ sources/Cargo.toml | 1 + .../v1.6.0/vmware-host-containers/Cargo.toml | 13 ++ .../v1.6.0/vmware-host-containers/src/main.rs | 111 ++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 sources/api/migration/migrations/v1.6.0/vmware-host-containers/Cargo.toml create mode 100644 sources/api/migration/migrations/v1.6.0/vmware-host-containers/src/main.rs diff --git a/Release.toml b/Release.toml index 20881343f58..da228667fe2 100644 --- a/Release.toml +++ b/Release.toml @@ -90,3 +90,6 @@ version = "1.5.2" "migrate_v1.5.1_control-container-v0-5-4.lz4", ] "(1.5.1, 1.5.2)" = [] +"(1.5.2, 1.6.0)" = [ + "migrate_v1.6.0_vmware-host-containers.lz4", +] diff --git a/sources/Cargo.lock b/sources/Cargo.lock index 64df57ae45b..2e7ebd701e4 100644 --- a/sources/Cargo.lock +++ b/sources/Cargo.lock @@ -3799,6 +3799,14 @@ dependencies = [ "thiserror", ] +[[package]] +name = "vmware-host-containers" +version = "0.1.0" +dependencies = [ + "migration-helpers", + "serde_json", +] + [[package]] name = "walkdir" version = "2.3.2" diff --git a/sources/Cargo.toml b/sources/Cargo.toml index aad75b766ac..d87d714f0ce 100644 --- a/sources/Cargo.toml +++ b/sources/Cargo.toml @@ -34,6 +34,7 @@ members = [ "api/migration/migrations/v1.5.0/oci-hooks-setting", "api/migration/migrations/v1.5.0/oci-hooks-setting-metadata", "api/migration/migrations/v1.5.1/control-container-v0-5-4", + "api/migration/migrations/v1.6.0/vmware-host-containers", "bottlerocket-release", diff --git a/sources/api/migration/migrations/v1.6.0/vmware-host-containers/Cargo.toml b/sources/api/migration/migrations/v1.6.0/vmware-host-containers/Cargo.toml new file mode 100644 index 00000000000..e7d6cec5bd3 --- /dev/null +++ b/sources/api/migration/migrations/v1.6.0/vmware-host-containers/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "vmware-host-containers" +version = "0.1.0" +authors = ["Erikson Tung "] +license = "Apache-2.0 OR MIT" +edition = "2021" +publish = false +# Don't rebuild crate just because of changes to README. +exclude = ["README.md"] + +[dependencies] +serde_json = "1" +migration-helpers = { path = "../../../migration-helpers", version = "0.1.0"} diff --git a/sources/api/migration/migrations/v1.6.0/vmware-host-containers/src/main.rs b/sources/api/migration/migrations/v1.6.0/vmware-host-containers/src/main.rs new file mode 100644 index 00000000000..ff80696caa7 --- /dev/null +++ b/sources/api/migration/migrations/v1.6.0/vmware-host-containers/src/main.rs @@ -0,0 +1,111 @@ +#![deny(rust_2018_idioms)] + +use migration_helpers::{migrate, Migration, MigrationData, Result}; +use std::process; + +const ADMIN_CONTAINER_SOURCE_SETTING_NAME: &str = "settings.host-containers.admin.source"; +const ADMIN_CONTAINER_IMAGE_REPOSITORY: &str = "public.ecr.aws/bottlerocket/bottlerocket-admin"; +const PREVIOUS_ADMIN_CONTAINER_VERSIONS: &[&str] = &["v0.7.0", "v0.7.1", "v0.7.2"]; +const TARGET_ADMIN_CONTAINER_VERSION: &str = "v0.7.3"; + +const CONTROL_CONTAINER_SOURCE_SETTING_NAME: &str = "settings.host-containers.control.source"; +const CONTROL_CONTAINER_IMAGE_REPOSITORY: &str = "public.ecr.aws/bottlerocket/bottlerocket-control"; +const PREVIOUS_CONTROL_CONTAINER_VERSIONS: &[&str] = &["v0.5.0", "v0.5.1", "v0.5.2", "v0.5.3"]; +const TARGET_CONTROL_CONTAINER_VERSION: &str = "v0.5.4"; + +pub struct VmwareHostContainerVersions; + +impl Migration for VmwareHostContainerVersions { + fn forward(&mut self, mut input: MigrationData) -> Result { + // For admin container + if let Some(data) = input.data.get_mut(ADMIN_CONTAINER_SOURCE_SETTING_NAME) { + match data { + serde_json::Value::String(source) => { + for ver in PREVIOUS_ADMIN_CONTAINER_VERSIONS { + let prev_source = format!("{}:{}", ADMIN_CONTAINER_IMAGE_REPOSITORY, ver); + if *source == prev_source { + *source = format!( + "{}:{}", + ADMIN_CONTAINER_IMAGE_REPOSITORY, TARGET_ADMIN_CONTAINER_VERSION + ); + println!( + "Changed value of '{}' from '{}' to '{}' on upgrade", + ADMIN_CONTAINER_SOURCE_SETTING_NAME, prev_source, source + ); + break; + } + } + } + _ => { + println!( + "'{}' is set to non-string value '{}'", + ADMIN_CONTAINER_SOURCE_SETTING_NAME, data + ); + } + } + } else { + println!( + "Found no '{}' to change on upgrade", + ADMIN_CONTAINER_SOURCE_SETTING_NAME + ); + } + + // For control container + if let Some(data) = input.data.get_mut(CONTROL_CONTAINER_SOURCE_SETTING_NAME) { + match data { + serde_json::Value::String(source) => { + for ver in PREVIOUS_CONTROL_CONTAINER_VERSIONS { + let prev_source = format!("{}:{}", CONTROL_CONTAINER_IMAGE_REPOSITORY, ver); + if *source == prev_source { + *source = format!( + "{}:{}", + CONTROL_CONTAINER_IMAGE_REPOSITORY, + TARGET_CONTROL_CONTAINER_VERSION + ); + println!( + "Changed value of '{}' from '{}' to '{}' on upgrade", + CONTROL_CONTAINER_SOURCE_SETTING_NAME, prev_source, source + ); + break; + } + } + } + _ => { + println!( + "'{}' is set to non-string value '{}'", + CONTROL_CONTAINER_SOURCE_SETTING_NAME, data + ); + } + } + } else { + println!( + "Found no '{}' to change on upgrade", + CONTROL_CONTAINER_SOURCE_SETTING_NAME + ); + } + + Ok(input) + } + + fn backward(&mut self, input: MigrationData) -> Result { + // It's unclear what version of the host-containers we should downgrade to since it could + // be any of the older host-container versions. + // We can just stay on the latest host-container version since there are no breaking changes. + println!("Vmware host-container versions migration has no work to do on downgrade"); + Ok(input) + } +} + +fn run() -> Result<()> { + migrate(VmwareHostContainerVersions) +} + +// 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); + } +}