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

Show Deployments in mirrord ls #1584

Merged
merged 5 commits into from
Jun 25, 2023
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.d/1503.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Deployment to list of targets returnd from `mirrord ls`.
51 changes: 37 additions & 14 deletions mirrord/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use exec::execvp;
use execution::MirrordExecution;
use extension::extension_exec;
use extract::extract_library;
use k8s_openapi::api::core::v1::Pod;
use k8s_openapi::api::{apps::v1::Deployment, core::v1::Pod};
use kube::{api::ListParams, Api};
use miette::JSONReportHandler;
use mirrord_config::{config::MirrordConfig, LayerConfig, LayerFileConfig};
Expand Down Expand Up @@ -195,14 +195,10 @@ async fn exec(args: &ExecArgs, progress: &TaskProgress) -> Result<()> {
/// Returns a list of (pod name, [container names]) pairs.
/// Filtering mesh side cars
async fn get_kube_pods(
namespace: Option<String>,
accept_invalid_certificates: bool,
kubeconfig: Option<String>,
namespace: Option<&str>,
client: &kube::Client,
) -> Result<HashMap<String, Vec<String>>> {
let client = create_kube_api(accept_invalid_certificates, kubeconfig)
.await
.map_err(CliError::KubernetesApiFailed)?;
let api: Api<Pod> = get_k8s_resource_api(&client, namespace.as_deref());
let api: Api<Pod> = get_k8s_resource_api(client, namespace);
let pods = api
.list(
&ListParams::default()
Expand Down Expand Up @@ -237,6 +233,29 @@ async fn get_kube_pods(
Ok(pod_containers_map)
}

async fn get_kube_deployments(
namespace: Option<&str>,
client: &kube::Client,
) -> Result<impl Iterator<Item = String>> {
let api: Api<Deployment> = get_k8s_resource_api(client, namespace);
let deployments = api
.list(&ListParams::default().labels("app!=mirrord"))
.await
.map_err(KubeApiError::from)
.map_err(CliError::KubernetesApiFailed)?;

Ok(deployments
.into_iter()
.filter(|deployment| {
deployment
.status
.as_ref()
.map(|status| status.available_replicas >= Some(1))
.unwrap_or(false)
})
.filter_map(|deployment| deployment.metadata.name))
}

/// Lists all possible target paths for pods.
/// Example: ```[
/// "pod/metalbear-deployment-85c754c75f-982p5",
Expand All @@ -256,12 +275,15 @@ async fn print_pod_targets(args: &ListTargetArgs) -> Result<()> {
(false, None, None)
};

let pods = get_kube_pods(
args.namespace.clone().or(namespace),
accept_invalid_certificates,
kubeconfig,
)
.await?;
let client = create_kube_api(accept_invalid_certificates, kubeconfig)
.await
.map_err(CliError::KubernetesApiFailed)?;

let namespace = args.namespace.as_deref().or(namespace.as_deref());

let pods = get_kube_pods(namespace, &client).await?;
let deployments = get_kube_deployments(namespace, &client).await?;

let mut target_vector = pods
.iter()
.flat_map(|(pod, containers)| {
Expand All @@ -274,6 +296,7 @@ async fn print_pod_targets(args: &ListTargetArgs) -> Result<()> {
.collect::<Vec<String>>()
}
})
.chain(deployments.map(|deployment| format!("deployment/{deployment}")))
.collect::<Vec<String>>();

target_vector.sort();
Expand Down
2 changes: 1 addition & 1 deletion tests/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod target {
assert!(res.success());
let stdout = process.get_stdout();
let targets: Vec<String> = serde_json::from_str(&stdout).unwrap();
let re = Regex::new(r"^pod/.+(/container/.+)?$").unwrap();
let re = Regex::new(r"^(pod|deployment)/.+(/container/.+)?$").unwrap();
targets
.iter()
.for_each(|output| assert!(re.is_match(output)));
Expand Down