Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
omertuc committed Jan 25, 2024
1 parent 61909e4 commit 8cbaf0d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
25 changes: 15 additions & 10 deletions src/ocp_postprocess/cluster_domain_rename/rename_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub(crate) fn fix_cluster_backup_sh(cluster_backup_sh: &str, original_hostname:
let cluster_backup = cluster_backup_sh.to_string();
let pattern = format!(r"NODE_{original_hostname}_IP");
let replacement = format!(r"NODE_{}_IP", env_var_safe(hostname));
Ok(cluster_backup.replace(dbg!(&pattern), &replacement))
Ok(cluster_backup.replace(&pattern, &replacement)
}

pub(crate) async fn fix_kubeconfig(cluster_name: &str, cluster_domain: &str, kubeconfig: &mut Value) -> Result<()> {
Expand Down Expand Up @@ -219,7 +219,7 @@ fn fix_kubeconfig_server(cluster: &mut serde_json::Map<String, Value>, cluster_d
// Could be something like `https://localhost:6443`, ignore
return Ok(());
}
.context("no previous value")?;
.context("no previous value")?;

Ok(())
}
Expand Down Expand Up @@ -288,7 +288,10 @@ pub(crate) fn fix_kcm_pod(pod: &mut Value, generated_infra_id: &str) -> Result<(

#[cfg(test)]
mod test_fix_etcd_static_pod {
use crate::{file_utils::read_file_to_string_sync, ocp_postprocess::cluster_domain_rename::rename_utils::{fix_etcd_pod_yaml, fix_etcd_static_pod,fix_cluster_backup_sh}};
use crate::{
file_utils::read_file_to_string_sync,
ocp_postprocess::cluster_domain_rename::rename_utils::{fix_cluster_backup_sh, fix_etcd_pod_yaml, fix_etcd_static_pod},
};
use anyhow::Result;
use serde_json::Value;

Expand Down Expand Up @@ -396,28 +399,30 @@ pub(crate) fn fix_etcd_pod_yaml(pod_yaml: &str, original_hostname: &str, hostnam
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-metrics-{hostname}.crt",
),
(
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-metrics-{original_hostname}.crt",
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-metrics-{original_hostname}.key",
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-metrics-{hostname}.key",
),
(
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-{original_hostname}.key",
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-{hostname}.key",
),
(
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-{original_hostname}.key",
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-{original_hostname}.crt",
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-{hostname}.crt",
),
("--target-name={original_hostname}", "--target-name={hostname}"),
];

for (pattern, replacement) in patterns {
let pattern = pattern.replace("{original_hostname}", original_hostname);
let replacement = replacement.replace("{hostname}", &hostname);
let pattern = pattern
.replace("{original_hostname}", original_hostname)
.replace("{original_hostname_safe}", &env_var_safe(original_hostname));

let pattern = pattern.replace("{original_hostname_safe}", &env_var_safe(original_hostname));
let replacement = replacement.replace("{hostname_safe}", &env_var_safe(hostname));
let replacement = replacement
.replace("{hostname}", &hostname)
.replace("{hostname_safe}", &env_var_safe(hostname));

pod_yaml = pod_yaml.replace(dbg!(&pattern), dbg!(&replacement)).to_string();
pod_yaml = pod_yaml.replace(&pattern, &replacement).to_string();
}

Ok(pod_yaml)
Expand Down
55 changes: 29 additions & 26 deletions src/ocp_postprocess/hostname_rename/filesystem_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,37 @@ pub(crate) async fn fix_filesystem_etcd_configmap_pod_yaml(original_hostname: &s
}

pub(crate) async fn fix_filesystem_etcd_scripts_cluster_backup_sh(original_hostname: &str, hostname: &str, dir: &Path) -> Result<()> {
join_all(file_utils::globvec(dir, "**/etcd-scripts/cluster-backup.sh")?.into_iter().map(|file_path| {
let cluster_backup_path = file_path.clone();
let original_hostname = original_hostname.to_string();
let hostname = hostname.to_string();
tokio::spawn(async move {
async move {
let contents = read_file_to_string(&file_path)
.await
.context("reading cluster-backup.sh")?;
join_all(
file_utils::globvec(dir, "**/etcd-scripts/cluster-backup.sh")?
.into_iter()
.map(|file_path| {
let cluster_backup_path = file_path.clone();
let original_hostname = original_hostname.to_string();
let hostname = hostname.to_string();
tokio::spawn(async move {
async move {
let contents = read_file_to_string(&file_path).await.context("reading cluster-backup.sh")?;

commit_file(
file_path,
dbg!(rename_utils::fix_cluster_backup_sh(&contents, &original_hostname, &hostname).context("fixing cluster-backup.sh")?),
)
.await
.context("writing cluster-backup.sh to disk")?;
commit_file(
file_path,
rename_utils::fix_cluster_backup_sh(&contents, &original_hostname, &hostname)
.context("fixing cluster-backup.sh")?,
)
.await
.context("writing cluster-backup.sh to disk")?;

anyhow::Ok(())
}
.await
.context(format!("fixing cluster-backup.sh {:?}", cluster_backup_path))
})
}))
.await
.into_iter()
.collect::<core::result::Result<Vec<_>, _>>()?
.into_iter()
.collect::<Result<Vec<_>>>()?;
anyhow::Ok(())
}
.await
.context(format!("fixing cluster-backup.sh {:?}", cluster_backup_path))
})
}),
)
.await
.into_iter()
.collect::<core::result::Result<Vec<_>, _>>()?
.into_iter()
.collect::<Result<Vec<_>>>()?;

Ok(())
}

0 comments on commit 8cbaf0d

Please sign in to comment.