diff --git a/k8s/upgrade/src/bin/upgrade-job/helm/values.rs b/k8s/upgrade/src/bin/upgrade-job/helm/values.rs index a943963a0..8ba8b64e2 100644 --- a/k8s/upgrade/src/bin/upgrade-job/helm/values.rs +++ b/k8s/upgrade/src/bin/upgrade-job/helm/values.rs @@ -73,7 +73,7 @@ pub(crate) fn generate_values_yaml_file( if from_values.io_engine_log_level().eq(log_level_to_replace) && to_values.io_engine_log_level().ne(log_level_to_replace) { - yq.set_string_value( + yq.set_value( YamlKey::try_from(".io_engine.logLevel")?, to_values.io_engine_log_level(), upgrade_values_file.path(), @@ -90,14 +90,9 @@ pub(crate) fn generate_values_yaml_file( .eventing_enabled() .ne(&to_values.eventing_enabled()) { - let value_as_str = match to_values.eventing_enabled() { - true => "true", - false => "false", - }; - - yq.set_string_value( + yq.set_value( YamlKey::try_from(".eventing.enabled")?, - value_as_str, + to_values.eventing_enabled(), upgrade_values_file.path(), )?; } @@ -105,7 +100,7 @@ pub(crate) fn generate_values_yaml_file( // Default options. // Image tag is set because the high_priority file is the user's source options file. // The target's image tag needs to be set for PRODUCT upgrade. - yq.set_string_value( + yq.set_value( YamlKey::try_from(".image.tag")?, to_values.image_tag(), upgrade_values_file.path(), @@ -114,44 +109,44 @@ pub(crate) fn generate_values_yaml_file( // RepoTags fields will be set to empty strings. This is required because we are trying // to get to the target container images, without setting versions for repo-specific // components. - yq.set_string_value( + yq.set_value( YamlKey::try_from(".image.repoTags.controlPlane")?, "", upgrade_values_file.path(), )?; - yq.set_string_value( + yq.set_value( YamlKey::try_from(".image.repoTags.dataPlane")?, "", upgrade_values_file.path(), )?; - yq.set_string_value( + yq.set_value( YamlKey::try_from(".image.repoTags.extensions")?, "", upgrade_values_file.path(), )?; // The CSI sidecar images need to always be the versions set on the chart by default. - yq.set_string_value( + yq.set_value( YamlKey::try_from(".csi.image.provisionerTag")?, to_values.csi_provisioner_image_tag(), upgrade_values_file.path(), )?; - yq.set_string_value( + yq.set_value( YamlKey::try_from(".csi.image.attacherTag")?, to_values.csi_attacher_image_tag(), upgrade_values_file.path(), )?; - yq.set_string_value( + yq.set_value( YamlKey::try_from(".csi.image.snapshotterTag")?, to_values.csi_snapshotter_image_tag(), upgrade_values_file.path(), )?; - yq.set_string_value( + yq.set_value( YamlKey::try_from(".csi.image.snapshotControllerTag")?, to_values.csi_snapshot_controller_image_tag(), upgrade_values_file.path(), )?; - yq.set_string_value( + yq.set_value( YamlKey::try_from(".csi.image.registrarTag")?, to_values.csi_node_driver_registrar_image_tag(), upgrade_values_file.path(), diff --git a/k8s/upgrade/src/bin/upgrade-job/helm/yaml/yq.rs b/k8s/upgrade/src/bin/upgrade-job/helm/yaml/yq.rs index 47c8987be..f6ed50218 100644 --- a/k8s/upgrade/src/bin/upgrade-job/helm/yaml/yq.rs +++ b/k8s/upgrade/src/bin/upgrade-job/helm/yaml/yq.rs @@ -7,7 +7,7 @@ use crate::{ }; use regex::Regex; use snafu::{ensure, ResultExt}; -use std::{ops::Deref, path::Path, process::Command, str}; +use std::{fmt::Display, ops::Deref, path::Path, process::Command, str}; /// This is a container for the String of an input yaml key. pub(crate) struct YamlKey(String); @@ -120,14 +120,14 @@ impl YqV4 { /// - "beta" - "delta" friend: "ferris" /// /// - /// result: - /// ======= - /// foo: - /// bar: "foobar" - /// baz: - /// - "alpha" - /// - "beta" - /// friend: "ferris" + /// result: + /// ======= + /// foo: + /// bar: "foobar" + /// baz: + /// - "alpha" + /// - "beta" + /// friend: "ferris" /// /// Special case: When the default value has changed, and the user has not customised that /// option, special upgrade values yaml updates have to be added to single out specific cases @@ -164,16 +164,34 @@ impl YqV4 { Ok(yq_merge_output.stdout) } - /// This sets in-place yaml string values in yaml files. - pub(crate) fn set_string_value( - &self, - key: YamlKey, - value: &str, - filepath: &Path, - ) -> Result<()> { + /// This sets in-place yaml values in yaml files. + pub(crate) fn set_value(&self, key: YamlKey, value: V, filepath: &Path) -> Result<()> + where + V: Display + Sized, + { + // Assigning value based on if it needs quotes around it or not. + // Strings require quotes. + let value = match format!("{value}").as_str() { + // Boolean yaml values do not need quotes. + "true" => "true".to_string(), + "false" => "false".to_string(), + // Null doesn't need quotes as well. + "null" => "null".to_string(), + // What remains is integers and strings + something_else => 'other: { + // If it's an integer, then no quotes. + if something_else.parse::().is_ok() { + break 'other something_else.to_string(); + } + + // Quotes for a string. + format!(r#""{something_else}""#) + } + }; + let yq_set_args = vec_to_strings![ "-i", - format!(r#"{} = "{value}""#, key.as_str()), + format!(r#"{} = {value}"#, key.as_str()), filepath.to_string_lossy() ]; let yq_set_output =