Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MIRRORD_IMPERSONATED_CONTAINER_NAME var #182

Merged
merged 2 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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