Skip to content

Commit 76dcf49

Browse files
committed
Fix etcd static pod hostname renaming for non-shell containers
Required due to openshift/cluster-etcd-operator#1606 The `etcd-ensure-env-vars` init container changed from an inline bash script to a Go binary (cluster-etcd-operator ensure-env), which caused `fix_etcd_static_pod_container` to skip renaming hostname references in command args. The function relied on finding a `"#!/bin/sh\n"` prefix to identify args containing hostname-dependent strings, and silently skipped the entire replacement block when no such prefix was found. This left stale `NODE_{old_hostname}_*` references in the container's command args while the env var definitions were correctly renamed, causing etcd to fail at startup with "environment variable `NODE_<old>_ETCD_URL_HOST` must be set". Apply hostname replacements to all command args unconditionally instead of gating on the shebang heuristic.
1 parent 55a6f67 commit 76dcf49

1 file changed

Lines changed: 38 additions & 35 deletions

File tree

src/ocp_postprocess/rename_utils.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ pub(crate) fn fix_etcd_pod_yaml_hostname(pod_yaml: &str, original_hostname: &str
424424
r#"${NODE_{hostname_safe}_ETCD_NAME""#,
425425
),
426426
("${NODE_{original_hostname_safe}_IP", "${NODE_{hostname_safe}_IP"),
427+
("NODE_{original_hostname_safe}_ETCD_URL_HOST", "NODE_{hostname_safe}_ETCD_URL_HOST"),
428+
("NODE_{original_hostname_safe}_ETCD_NAME", "NODE_{hostname_safe}_ETCD_NAME"),
429+
("NODE_{original_hostname_safe}_IP", "NODE_{hostname_safe}_IP"),
427430
(
428431
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-peer-{original_hostname}.crt",
429432
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-peer-{hostname}.crt",
@@ -517,25 +520,8 @@ pub(crate) fn fix_etcd_static_pod(pod: &mut Value, original_hostname: &str, host
517520
}
518521

519522
fn fix_etcd_static_pod_container(container: &mut Value, original_hostname: &str, hostname: &str) -> Result<()> {
520-
'hostname_args_replace: {
521-
let args = container
522-
.pointer_mut("/command")
523-
.context("command not found")?
524-
.as_array_mut()
525-
.context("command not an array")?;
526-
527-
ensure!(!args.is_empty(), "expected at least one arg in etcd static pod container");
528-
529-
let shell_arg = args
530-
.iter_mut()
531-
.find_map(|arg| arg.as_str()?.starts_with("#!/bin/sh\n").then_some(arg));
532-
533-
let shell_arg = match shell_arg {
534-
None => break 'hostname_args_replace,
535-
Some(shell_arg) => shell_arg,
536-
};
537-
538-
for (pattern, replacement) in [
523+
{
524+
let patterns = [
539525
("NODE_{original_hostname_safe}_ETCD_URL_HOST", "NODE_{hostname_safe}_ETCD_URL_HOST"),
540526
("NODE_{original_hostname_safe}_ETCD_NAME", "NODE_{hostname_safe}_ETCD_NAME"),
541527
("NODE_{original_hostname_safe}_IP", "NODE_{hostname_safe}_IP"),
@@ -564,22 +550,39 @@ fn fix_etcd_static_pod_container(container: &mut Value, original_hostname: &str,
564550
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-metrics-{original_hostname}.key",
565551
"/etc/kubernetes/static-pod-certs/secrets/etcd-all-certs/etcd-serving-metrics-{hostname}.key",
566552
),
567-
] {
568-
let pattern = pattern
569-
.replace("{original_hostname}", original_hostname)
570-
.replace("{original_hostname_safe}", &env_var_safe(original_hostname));
571-
572-
let replacement = replacement
573-
.replace("{hostname}", hostname)
574-
.replace("{hostname_safe}", &env_var_safe(hostname));
575-
576-
*shell_arg = serde_json::Value::String(
577-
shell_arg
578-
.as_str()
579-
.context("arg not string")?
580-
.replace(&pattern, &replacement)
581-
.to_string(),
582-
);
553+
];
554+
555+
ensure!(
556+
container
557+
.pointer("/command")
558+
.and_then(|c| c.as_array())
559+
.map_or(false, |a| !a.is_empty()),
560+
"expected at least one arg in etcd static pod container"
561+
);
562+
563+
for field in ["/command", "/args"] {
564+
let Some(values) = container.pointer_mut(field).and_then(|v| v.as_array_mut()) else {
565+
continue;
566+
};
567+
568+
for arg in values.iter_mut() {
569+
let Some(arg_str) = arg.as_str() else { continue };
570+
let mut replaced = arg_str.to_string();
571+
572+
for (pattern, replacement) in &patterns {
573+
let pattern = pattern
574+
.replace("{original_hostname}", original_hostname)
575+
.replace("{original_hostname_safe}", &env_var_safe(original_hostname));
576+
577+
let replacement = replacement
578+
.replace("{hostname}", hostname)
579+
.replace("{hostname_safe}", &env_var_safe(hostname));
580+
581+
replaced = replaced.replace(&pattern, &replacement);
582+
}
583+
584+
*arg = serde_json::Value::String(replaced);
585+
}
583586
}
584587
}
585588

0 commit comments

Comments
 (0)