Skip to content

Commit

Permalink
Add MIRRORD_IMPERSONATED_CONTAINER_NAME var (#182)
Browse files Browse the repository at this point in the history
* Add MIRRORD_IMPERSONATED_CONTAINER_NAME var

* Use info log, iterate to find container name
  • Loading branch information
camerondurham authored Jul 3, 2022
1 parent 92936c2 commit 78a5c36
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
## [Unreleased]
### Added
- mirrord-cli `exec` subcommand accepts `--extract-path` argument to set the directory to extract the library to. Used for tests mainly.
- mirrord-layer provides `MIRRORD_IMPERSONATED_CONTAINER_NAME` environment variable to specify container name to impersonate. mirrord-cli accepts argument to set variable.

### Changed
- Refactor e2e, enable only Node HTTP mirroring test.
Expand Down
4 changes: 4 additions & 0 deletions mirrord-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub(super) struct ExecArgs {
#[clap(long, value_parser)]
pub agent_ttl: Option<u16>,

/// Select container name to impersonate. Default is first container.
#[clap(long, value_parser)]
pub impersonate_container_name: Option<String>,

/// Accept/reject invalid certificates.
#[clap(short = 'c', long, value_parser)]
pub accept_invalid_certificates: bool,
Expand Down
7 changes: 7 additions & 0 deletions mirrord-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ fn exec(args: &ExecArgs) -> Result<()> {
std::env::set_var("MIRRORD_AGENT_NAMESPACE", namespace.clone());
}

if let Some(impersonate_container_name) = &args.impersonate_container_name {
std::env::set_var(
"MIRRORD_IMPERSONATED_CONTAINER_NAME",
impersonate_container_name,
);
}

if let Some(log_level) = &args.agent_log_level {
std::env::set_var("MIRRORD_AGENT_RUST_LOG", log_level.clone());
}
Expand Down
3 changes: 3 additions & 0 deletions mirrord-layer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub struct LayerConfig {
#[envconfig(from = "MIRRORD_AGENT_IMPERSONATED_POD_NAMESPACE", default = "default")]
pub impersonated_pod_namespace: String,

#[envconfig(from = "MIRRORD_IMPERSONATED_CONTAINER_NAME")]
pub impersonated_container_name: Option<String>,

#[envconfig(from = "MIRRORD_ACCEPT_INVALID_CERTIFICATES", default = "false")]
pub accept_invalid_certificates: bool,

Expand Down
35 changes: 28 additions & 7 deletions mirrord-layer/src/pod_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use kube::{
};
use rand::distributions::{Alphanumeric, DistString};
use serde_json::json;
use tracing::{error, warn};
use tracing::{error, info, warn};

use crate::config::LayerConfig;

Expand All @@ -22,15 +22,34 @@ struct RuntimeData {
}

impl RuntimeData {
async fn from_k8s(client: Client, pod_name: &str, pod_namespace: &str) -> Self {
async fn from_k8s(
client: Client,
pod_name: &str,
pod_namespace: &str,
container_name: &Option<String>,
) -> Self {
let pods_api: Api<Pod> = Api::namespaced(client, pod_namespace);
let pod = pods_api.get(pod_name).await.unwrap();
let node_name = &pod.spec.unwrap().node_name;
let container_statuses = pod.status.unwrap().container_statuses.unwrap();
let container_info = container_statuses
.first()
.unwrap()
.container_id
let container_statuses = &pod.status.unwrap().container_statuses.unwrap();
let container_info = if let Some(container_name) = container_name {
&container_statuses
.iter()
.find(|&status| &status.name == container_name)
.with_context(|| {
format!(
"no container named {} found in namespace={}, pod={}",
&container_name, &pod_namespace, &pod_name
)
})
.unwrap()
.container_id
} else {
info!("No container name specified, defaulting to first container found");
&container_statuses.first().unwrap().container_id
};

let container_info = container_info
.as_ref()
.unwrap()
.split("://")
Expand Down Expand Up @@ -61,6 +80,7 @@ pub async fn create_agent(config: LayerConfig) -> Result<Portforwarder> {
image_pull_policy,
impersonated_pod_name,
impersonated_pod_namespace,
impersonated_container_name,
..
} = config;

Expand All @@ -83,6 +103,7 @@ pub async fn create_agent(config: LayerConfig) -> Result<Portforwarder> {
client.clone(),
&impersonated_pod_name,
&impersonated_pod_namespace,
&impersonated_container_name,
)
.await;

Expand Down
1 change: 1 addition & 0 deletions tests/src/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ mod tests {
env.insert("MIRRORD_AGENT_IMAGE", "test");
env.insert("MIRRORD_CHECK_VERSION", "false");
env.insert("MIRRORD_AGENT_RUST_LOG", "debug");
env.insert("MIRRORD_IMPERSONATED_CONTAINER_NAME", "test");
env.insert("RUST_LOG", "debug");
let server = Command::new(path)
.args(args.clone())
Expand Down

0 comments on commit 78a5c36

Please sign in to comment.