Skip to content

Commit 01918f6

Browse files
committed
Apply suggestions
1 parent a9dcd0a commit 01918f6

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

mirrord/cli/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ pub(super) struct ListTargetArgs {
802802

803803
/// Specify the type of target to be retrieved. If `None`, all types are retrieved.
804804
#[arg(short = 't', long)]
805-
pub target_type: Option<TargetType>,
805+
pub target_type: Option<Vec<TargetType>>,
806806
}
807807

808808
impl ListTargetArgs {

mirrord/cli/src/list.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{str::FromStr, sync::LazyLock, time::Instant};
1+
use std::{sync::LazyLock, time::Instant};
22

33
use futures::TryStreamExt;
44
use k8s_openapi::api::core::v1::Namespace;
@@ -12,7 +12,6 @@ use mirrord_kube::{
1212
use mirrord_operator::client::OperatorApi;
1313
use semver::VersionReq;
1414
use serde::{ser::SerializeSeq, Serialize, Serializer};
15-
use serde_json::Value;
1615
use tracing::Level;
1716

1817
use crate::{util, CliError, CliResult, Format, ListTargetArgs};
@@ -245,17 +244,13 @@ pub(super) async fn print_targets(args: ListTargetArgs, rich_output: bool) -> Cl
245244
}
246245

247246
let target_types = if let Some(target_type) = args.target_type {
248-
Some(vec![target_type])
247+
Some(target_type)
249248
} else {
250249
match std::env::var(LS_TARGET_TYPES_ENV)
251250
.ok()
252-
.map(|val| serde_json::from_str::<Vec<Value>>(val.as_ref()))
251+
.map(|val| serde_json::from_str::<Vec<TargetType>>(val.as_ref()))
253252
.transpose()?
254253
.unwrap_or_default()
255-
.into_iter()
256-
.filter_map(|value| value.as_str().map(|str| str.to_string()))
257-
.filter_map(|string| TargetType::from_str(&string).ok())
258-
.collect::<Vec<TargetType>>()
259254
{
260255
vec if vec.is_empty() => None,
261256
vec => Some(vec),

mirrord/config/src/target.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ mirrord-layer failed to parse the provided target!
277277
Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug, JsonSchema, EnumDiscriminants,
278278
)]
279279
#[serde(untagged, deny_unknown_fields)]
280-
#[strum_discriminants(derive(EnumString, Serialize))]
280+
#[strum_discriminants(derive(EnumString, Serialize, Deserialize))]
281281
#[strum_discriminants(name(TargetType))]
282282
#[strum_discriminants(strum(serialize_all = "lowercase"))]
283+
#[strum_discriminants(serde(rename_all="lowercase"))]
283284
pub enum Target {
284285
/// <!--${internal}-->
285286
/// [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/).
@@ -370,7 +371,7 @@ impl Target {
370371

371372
impl fmt::Display for TargetType {
372373
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
373-
let stringifed = match self {
374+
let stringified = match self {
374375
TargetType::Targetless => "targetless",
375376
TargetType::Pod => "pod",
376377
TargetType::Deployment => "deployment",
@@ -382,7 +383,7 @@ impl fmt::Display for TargetType {
382383
TargetType::ReplicaSet => "replicaset",
383384
};
384385

385-
f.write_str(stringifed)
386+
f.write_str(stringified)
386387
}
387388
}
388389

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

+10-24
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ impl KubeResourceSeeker<'_> {
8181
resource_types: Vec<TargetType>,
8282
operator_active: bool,
8383
) -> 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)
84+
Ok(futures::future::try_join_all(
85+
resource_types
86+
.into_iter()
87+
.map(|resource_type| self.filtered_single(resource_type, operator_active)),
88+
)
89+
.await?
90+
.concat())
8991
}
9092

9193
async fn filtered_single(
@@ -114,27 +116,11 @@ impl KubeResourceSeeker<'_> {
114116
TargetType::ReplicaSet if operator_active => {
115117
self.simple_list_resource::<ReplicaSet>("replicaset").await
116118
}
117-
TargetType::Targetless => {
118-
tracing::error!("Cannot list targets with resource type 'targetless'");
119-
Err(KubeApiError::InvalidListTargetType(
120-
resource_type,
121-
"cannot list targets with resource type 'targetless'.".to_string(),
122-
))
123-
}
119+
TargetType::Targetless => Err(KubeApiError::InvalidTargetType(resource_type)),
124120
resource_type if !operator_active => {
125-
tracing::warn!("Cannot list targets with resource type '{resource_type}' unless the operator is enabled.");
126-
Err(KubeApiError::InvalidListTargetType(
127-
resource_type,
128-
"this type requires the operator to be enabled.".to_string(),
129-
))
130-
}
131-
resource_type => {
132-
tracing::error!("Cannot list targets with resource type '{resource_type}' due to a missing implementation. This is a bug.");
133-
Err(KubeApiError::InvalidListTargetType(
134-
resource_type,
135-
"this type cannot be listed. This is a bug.".to_string(),
136-
))
121+
Err(KubeApiError::TargetTypeRequiresOperator(resource_type))
137122
}
123+
resource_type => Err(KubeApiError::InvalidTargetTypeBug(resource_type)),
138124
}
139125
}
140126

mirrord/kube/src/error.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,22 @@ pub enum KubeApiError {
5959
String,
6060
),
6161

62+
/// Attempted to list a specific resource type with `mirrord ls` but was unable to because the
63+
/// operator was required.
64+
#[error(
65+
"The requested resource type `{0}` could not be listed because it requires the operator."
66+
)]
67+
TargetTypeRequiresOperator(TargetType),
68+
69+
/// Attempted to list a specific resource type with `mirrord ls` but was unable to because that
70+
/// type cannot be listed (for example, cannot list targets of type "targetless".
71+
#[error("Targets of type `{0}` cannot be listed.")]
72+
InvalidTargetType(TargetType),
73+
6274
/// Attempted to list a specific resource type with `mirrord ls` but was unable to because
63-
/// either the operator required but is not enabled, or the type was invalid.
64-
#[error("The requested resource type `{0}` could not be listed: {1}")]
65-
InvalidListTargetType(TargetType, String),
75+
/// listing that type has not been implemented.
76+
#[error("Targets of type `{0}` cannot be listed. This is a bug.")]
77+
InvalidTargetTypeBug(TargetType),
6678
}
6779

6880
impl KubeApiError {

0 commit comments

Comments
 (0)