From d9494fed21429b56445b0abc381aff385ea3fdcf Mon Sep 17 00:00:00 2001 From: Erikson Tung Date: Tue, 24 Jan 2023 14:53:59 -0800 Subject: [PATCH 1/2] migration-helpers: add new helper 'NoOpMigration' This adds a new migration helper for cases where we don't want to migrate any data but just copy the datastore as is during the migration. This is useful for conditional migration where we selectively choose what data to migrate depending on the variant. --- .../src/common_migrations.rs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sources/api/migration/migration-helpers/src/common_migrations.rs b/sources/api/migration/migration-helpers/src/common_migrations.rs index ec1d86d261a..ce06e895e07 100644 --- a/sources/api/migration/migration-helpers/src/common_migrations.rs +++ b/sources/api/migration/migration-helpers/src/common_migrations.rs @@ -1369,3 +1369,25 @@ mod test_replace_metadata { ); } } + +// =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= + +/// When we add conditional migrations that can only run for specific variants, we need to run this +/// migration helper for cases where the migration does NOT apply so migrator will still create a valid +/// intermediary datastore that the host can transition to. +#[derive(Debug)] +pub struct NoOpMigration; + +impl Migration for NoOpMigration { + /// No work to do on forward migrations, copy the same datastore + fn forward(&mut self, input: MigrationData) -> Result { + println!("NoOpMigration has no work to do on upgrade.",); + Ok(input) + } + + /// No work to do on backward migrations, copy the same datastore + fn backward(&mut self, input: MigrationData) -> Result { + println!("NoOpMigration has no work to do on downgrade.",); + Ok(input) + } +} From e23e19d9a2a7dcb58b3264d5c29d29466041aa4b Mon Sep 17 00:00:00 2001 From: Erikson Tung Date: Tue, 24 Jan 2023 14:55:42 -0800 Subject: [PATCH 2/2] migrations: call 'NoOpMigration' for when no data migration necessary Conditional migrations need to call migrate regardless of whether data is being migrated or not in order to generate a valid datastore for migrator to transition between during host updates. --- .../src/main.rs | 8 +++++--- .../add-k8s-autoscaling-warm-pool-setting/src/main.rs | 8 +++++--- .../v1.12.0/oci-defaults-setting-metadata/src/main.rs | 6 ++++-- .../migrations/v1.12.0/oci-defaults-setting/src/main.rs | 6 ++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting-metadata/src/main.rs b/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting-metadata/src/main.rs index ae1239f30e7..8a4e76d6ad7 100644 --- a/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting-metadata/src/main.rs +++ b/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting-metadata/src/main.rs @@ -1,6 +1,6 @@ #![deny(rust_2018_idioms)] -use migration_helpers::common_migrations::{AddMetadataMigration, SettingMetadata}; +use migration_helpers::common_migrations::{AddMetadataMigration, NoOpMigration, SettingMetadata}; use migration_helpers::{migrate, Result}; use std::process; @@ -10,8 +10,10 @@ fn run() -> Result<()> { migrate(AddMetadataMigration(&[SettingMetadata { metadata: &["affected-services"], setting: "settings.autoscaling", - }])); - }; + }]))?; + } else { + migrate(NoOpMigration)?; + } Ok(()) } diff --git a/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting/src/main.rs b/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting/src/main.rs index f81bd14d99e..ad14000b688 100644 --- a/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting/src/main.rs +++ b/sources/api/migration/migrations/v1.12.0/add-k8s-autoscaling-warm-pool-setting/src/main.rs @@ -1,6 +1,6 @@ #![deny(rust_2018_idioms)] -use migration_helpers::common_migrations::AddPrefixesMigration; +use migration_helpers::common_migrations::{AddPrefixesMigration, NoOpMigration}; use migration_helpers::{migrate, Result}; use std::process; @@ -12,8 +12,10 @@ fn run() -> Result<()> { "settings.autoscaling", "services.autoscaling-warm-pool", "configuration-files.warm-pool-wait-toml", - ])); - }; + ]))?; + } else { + migrate(NoOpMigration)?; + } Ok(()) } diff --git a/sources/api/migration/migrations/v1.12.0/oci-defaults-setting-metadata/src/main.rs b/sources/api/migration/migrations/v1.12.0/oci-defaults-setting-metadata/src/main.rs index 76e827e73ca..f8610cc70ce 100644 --- a/sources/api/migration/migrations/v1.12.0/oci-defaults-setting-metadata/src/main.rs +++ b/sources/api/migration/migrations/v1.12.0/oci-defaults-setting-metadata/src/main.rs @@ -1,5 +1,5 @@ #![deny(rust_2018_idioms)] -use migration_helpers::common_migrations::{AddMetadataMigration, SettingMetadata}; +use migration_helpers::common_migrations::{AddMetadataMigration, NoOpMigration, SettingMetadata}; use migration_helpers::{migrate, Result}; use std::process; @@ -12,7 +12,9 @@ fn run() -> Result<()> { metadata: &["affected-services"], setting: "settings.oci-defaults", }]))? - }; + } else { + migrate(NoOpMigration)?; + } Ok(()) } diff --git a/sources/api/migration/migrations/v1.12.0/oci-defaults-setting/src/main.rs b/sources/api/migration/migrations/v1.12.0/oci-defaults-setting/src/main.rs index 00f7af72bdd..84438b42287 100644 --- a/sources/api/migration/migrations/v1.12.0/oci-defaults-setting/src/main.rs +++ b/sources/api/migration/migrations/v1.12.0/oci-defaults-setting/src/main.rs @@ -1,6 +1,6 @@ #![deny(rust_2018_idioms)] -use migration_helpers::common_migrations; +use migration_helpers::common_migrations::{AddPrefixesMigration, NoOpMigration}; use migration_helpers::{migrate, Result}; use std::process; @@ -10,11 +10,13 @@ use std::process; /// `settings.oci-defaults.resource-limits` fn run() -> Result<()> { if cfg!(variant_runtime = "k8s") { - migrate(common_migrations::AddPrefixesMigration(vec![ + migrate(AddPrefixesMigration(vec![ "settings.oci-defaults", "services.oci-defaults", "configuration-files.oci-defaults", ]))? + } else { + migrate(NoOpMigration)?; } Ok(())