Skip to content

Commit

Permalink
Merge pull request #139 from omertuc/proxyorder
Browse files Browse the repository at this point in the history
OCPBUGS-33471: Fix proxy ordering
  • Loading branch information
omertuc authored May 15, 2024
2 parents 7a5bb29 + ab755e8 commit a2e79d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/ocp_postprocess/proxy_rename/etcd_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ pub(crate) async fn fix_storages(etcd_client: &InMemoryK8sEtcd, proxy: &Proxy) -
}

fn generic_proxy_fix(spec: &mut serde_json::Map<String, Value>, proxy: &Proxy) {
spec.insert("HTTPS_PROXY".to_string(), Value::String(proxy.status_proxy.http_proxy.clone()));
spec.insert("HTTP_PROXY".to_string(), Value::String(proxy.status_proxy.https_proxy.clone()));
spec.insert("HTTP_PROXY".to_string(), Value::String(proxy.status_proxy.http_proxy.clone()));
spec.insert("HTTPS_PROXY".to_string(), Value::String(proxy.status_proxy.https_proxy.clone()));
spec.insert("NO_PROXY".to_string(), Value::String(proxy.status_proxy.no_proxy.clone()));
}

Expand Down
36 changes: 28 additions & 8 deletions src/ocp_postprocess/proxy_rename/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ pub(crate) fn fix_containers(config: &mut Value, proxy: &Proxy, prefix: &str) ->
Ok(())
}

// Remove all existing proxy env vars from the container's env and return the index of where the
// first proxy env var should be inserted. Also removes the order in which the proxy env vars
// appeared in the original env.
/// Remove all existing proxy env vars from the container's env and return the index of where the
/// first proxy env var should be inserted. Also returns the order in which the proxy env vars
/// appeared in the original env.
fn remove_existing_proxy_env_vars(container_env: &mut Vec<Value>, is_upper: bool) -> Result<Option<(usize, Vec<String>)>> {
let original_proxy_envs = container_env
.iter()
Expand Down Expand Up @@ -225,34 +225,54 @@ mod tests {

#[test]
fn test_fix_containers() {
let data = r#"{"apiVersion":"apps/v1","kind":"Deployment","spec":{"template":{"spec":{"containers":[{"env":[{"name":"DEFAULT_DESTINATION_CA_PATH","value":"/var/run/configmaps/service-ca/service-ca.crt"},{"name":"HTTP_PROXY","value":"http://squid.corp.redhat.com:3128"},{"name":"HTTPS_PROXY","value":"http://squid.corp.redhat.com:3128"},{"name":"NO_PROXY","value":".cluster.local,.seed.ibo0.redhat.com,.svc,10.128.0.0/14,127.0.0.1,172.30.0.0/16,192.168.126.0/24,api-int.seed.ibo0.redhat.com,api-int.seed.redhat.com,localhost"},{"name":"RELOAD_INTERVAL","value":"5s"},{"name":"STATS_USERNAME_FILE","value":"/var/lib/haproxy/conf/metrics-auth/statsUsername"},{"name":"http_proxy","value":"http://squid.corp.redhat.com:3128"},{"name":"https_proxy","value":"http://squid.corp.redhat.com:3128"},{"name":"no_proxy","value":".cluster.local,.seed.ibo0.redhat.com,.svc,10.128.0.0/14,127.0.0.1,172.30.0.0/16,192.168.126.0/24,api-int.seed.ibo0.redhat.com,api-int.seed.redhat.com,localhost"}],"name":"router"}]}}}}"#;
let data = r#"{"apiVersion":"apps/v1","kind":"Deployment","spec":{"template":{"spec":{"containers":[{"env":[{"name":"DEFAULT_DESTINATION_CA_PATH","value":"/var/run/configmaps/service-ca/service-ca.crt"},{"name":"HTTPS_PROXY","value":"http://squid.corp.redhats.com:3128"},{"name":"HTTP_PROXY","value":"http://squid.corp.redhat.com:3128"},{"name":"NO_PROXY","value":".cluster.local,.seed.ibo0.redhat.com,.svc,10.128.0.0/14,127.0.0.1,172.30.0.0/16,192.168.126.0/24,api-int.seed.ibo0.redhat.com,api-int.seed.redhat.com,localhost"},{"name":"RELOAD_INTERVAL","value":"5s"},{"name":"STATS_USERNAME_FILE","value":"/var/lib/haproxy/conf/metrics-auth/statsUsername"},{"name":"http_proxy","value":"http://squid.corp.redhat.com:3128"},{"name":"https_proxy","value":"http://squid.corp.redhats.com:3128"},{"name":"no_proxy","value":".cluster.local,.seed.ibo0.redhat.com,.svc,10.128.0.0/14,127.0.0.1,172.30.0.0/16,192.168.126.0/24,api-int.seed.ibo0.redhat.com,api-int.seed.redhat.com,localhost"}],"name":"router"}]}}}}"#;

let mut config: Value = serde_json::from_str(data).unwrap();

let proxy = Proxy {
spec_proxy: ProxyConfig {
http_proxy: "http://proxy.example.com".to_string(),
https_proxy: "http://proxy.example.com".to_string(),
https_proxy: "http://proxy.examples.com".to_string(),
no_proxy: "localhost".to_string(),
},
status_proxy: ProxyConfig {
http_proxy: "http://proxy.example.com".to_string(),
https_proxy: "http://proxy.example.com".to_string(),
https_proxy: "http://proxy.examples.com".to_string(),
no_proxy: "localhost".to_string(),
},
};

fix_containers(&mut config, &proxy, "/spec/template/spec").unwrap();

assert!(!serde_json::to_string(&config).unwrap().contains("squid.corp.redhat.com"));

let env = config.pointer("/spec/template/spec/containers/0/env").unwrap().as_array().unwrap();

for (expected_name, expected_value) in [
("HTTP_PROXY", "http://proxy.example.com"),
("HTTPS_PROXY", "http://proxy.examples.com"),
("NO_PROXY", "localhost"),
("http_proxy", "http://proxy.example.com"),
("https_proxy", "http://proxy.examples.com"),
("no_proxy", "localhost"),
] {
for env_var in dbg!(env).iter() {
let name = env_var.pointer("/name").unwrap().as_str().unwrap();
let value = env_var.pointer("/value").unwrap().as_str().unwrap();

if name == expected_name {
assert_eq!(value, expected_value);
}
}
}
}

#[test]
fn test_remove_existing_proxy_env_vars() {
let mut env = vec![
json!({"name": "SOME", "value": "value"}),
json!({"name": "HTTP_PROXY", "value": "http://proxy.example.com"}),
json!({"name": "HTTPS_PROXY", "value": "http://proxy.example.com"}),
json!({"name": "HTTPS_PROXY", "value": "http://proxy.examples.com"}),
json!({"name": "NO_PROXY", "value": "localhost"}),
json!({"name": "OTHER", "value": "value"}),
];
Expand All @@ -275,7 +295,7 @@ mod tests {

let mut env = vec![
json!({"name": "HTTP_PROXY", "value": "http://proxy.example.com"}),
json!({"name": "HTTPS_PROXY", "value": "http://proxy.example.com"}),
json!({"name": "HTTPS_PROXY", "value": "http://proxy.examples.com"}),
json!({"name": "NO_PROXY", "value": "localhost"}),
];

Expand Down

0 comments on commit a2e79d2

Please sign in to comment.