Skip to content

Commit 484c16c

Browse files
committed
If no target type given in ls command args, parse env var for target types to list
1 parent 5f54ae6 commit 484c16c

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

mirrord/cli/src/list.rs

+42-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::{sync::LazyLock, time::Instant};
2-
31
use futures::TryStreamExt;
42
use k8s_openapi::api::core::v1::Namespace;
53
use kube::Client;
@@ -12,10 +10,15 @@ use mirrord_kube::{
1210
use mirrord_operator::client::OperatorApi;
1311
use semver::VersionReq;
1412
use serde::{ser::SerializeSeq, Serialize, Serializer};
13+
use serde_json::Value;
14+
use std::str::FromStr;
15+
use std::{sync::LazyLock, time::Instant};
1516
use tracing::Level;
1617

1718
use crate::{util, CliError, CliResult, Format, ListTargetArgs};
1819

20+
const LS_TARGET_TYPES_ENV: &str = "MIRRORD_LS_TARGET_TYPES";
21+
1922
/// A mirrord target found in the cluster.
2023
#[derive(Serialize)]
2124
struct FoundTarget {
@@ -69,7 +72,7 @@ impl FoundTargets {
6972
async fn resolve(
7073
config: LayerConfig,
7174
rich_output: bool,
72-
target_type: Option<TargetType>,
75+
target_types: Option<Vec<TargetType>>,
7376
) -> CliResult<Self> {
7477
let client = create_kube_config(
7578
config.accept_invalid_certificates,
@@ -111,8 +114,10 @@ impl FoundTargets {
111114

112115
let (targets, namespaces) = tokio::try_join!(
113116
async {
114-
let paths = match (operator_api, target_type) {
115-
(None, _) if config.operator == Some(true) => Err(CliError::OperatorNotInstalled),
117+
let paths = match (operator_api, target_types) {
118+
(None, _) if config.operator == Some(true) => {
119+
Err(CliError::OperatorNotInstalled)
120+
}
116121

117122
(Some(api), None)
118123
if !rich_output
@@ -124,19 +129,27 @@ impl FoundTargets {
124129
})
125130
}
126131

127-
(Some(api), Some(target_type))
132+
(Some(api), Some(target_types))
128133
if !rich_output
129134
&& ALL_TARGETS_SUPPORTED_OPERATOR_VERSION
130135
.matches(&api.operator().spec.operator_version) =>
131136
{
132-
seeker.filtered(target_type, true).await.map_err(|error| {
133-
CliError::friendlier_error_or_else(error, CliError::ListTargetsFailed)
137+
seeker.filtered(target_types, true).await.map_err(|error| {
138+
CliError::friendlier_error_or_else(
139+
error,
140+
CliError::ListTargetsFailed,
141+
)
134142
})
135143
}
136144

137-
(None, Some(target_type)) => seeker.filtered(target_type, false).await.map_err(|error| {
138-
CliError::friendlier_error_or_else(error, CliError::ListTargetsFailed)
139-
}),
145+
(None, Some(target_types)) => {
146+
seeker.filtered(target_types, false).await.map_err(|error| {
147+
CliError::friendlier_error_or_else(
148+
error,
149+
CliError::ListTargetsFailed,
150+
)
151+
})
152+
}
140153

141154
_ => seeker.all_open_source().await.map_err(|error| {
142155
CliError::friendlier_error_or_else(error, CliError::ListTargetsFailed)
@@ -237,7 +250,24 @@ pub(super) async fn print_targets(args: ListTargetArgs, rich_output: bool) -> Cl
237250
util::remove_proxy_env();
238251
}
239252

240-
let targets = FoundTargets::resolve(layer_config, rich_output, args.target_type).await?;
253+
let target_types = if let Some(target_type) = args.target_type {
254+
Some(vec![target_type])
255+
} else {
256+
match std::env::var(LS_TARGET_TYPES_ENV)
257+
.ok()
258+
.map(|val| serde_json::from_str::<Vec<Value>>(val.as_ref()))
259+
.transpose()?
260+
.unwrap_or_default()
261+
.into_iter()
262+
.filter_map(|value| value.as_str().map(|str| str.to_string()))
263+
.filter_map(|string| TargetType::from_str(&string).ok())
264+
.collect::<Vec<TargetType>>() {
265+
vec if vec.is_empty() => None,
266+
vec => Some(vec)
267+
}
268+
};
269+
270+
let targets = FoundTargets::resolve(layer_config, rich_output, target_types).await?;
241271

242272
match args.output {
243273
Format::Json => {

mirrord/kube/src/api/kubernetes/seeker.rs

+12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ impl KubeResourceSeeker<'_> {
7777
/// 1. The resource type doesn't require the operator
7878
/// 2. The operator is being used
7979
pub async fn filtered(
80+
&self,
81+
resource_types: Vec<TargetType>,
82+
operator_active: bool,
83+
) -> Result<Vec<String>> {
84+
let mut targets = vec![];
85+
for resource_type in resource_types {
86+
targets.extend(self.filtered_single(resource_type, operator_active).await?);
87+
};
88+
Ok(targets)
89+
}
90+
91+
async fn filtered_single(
8092
&self,
8193
resource_type: TargetType,
8294
operator_active: bool,

0 commit comments

Comments
 (0)