Skip to content

Commit

Permalink
Fix target type format (#2715)
Browse files Browse the repository at this point in the history
* Fixed

* Changelog entry
  • Loading branch information
Razz4780 authored Sep 2, 2024
1 parent 28e1efe commit 69e801b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
1 change: 1 addition & 0 deletions changelog.d/+target-type-format.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed target type formatting and E2E test.
22 changes: 10 additions & 12 deletions mirrord/config/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ impl Target {
/// It's mainly implemented using the `impl_target_display` macro, except for [`Target`]
/// and `TargetHandle`, which manually implement this.
pub trait TargetDisplay {
/// The string version of a [`Target`]'s type, e.g. `Pod` -> `"Pod"`.
/// The string version of a [`Target`]'s type, e.g. `Pod` -> `"pod"`, `StatefulSet` ->
/// `"statefulset"`.
fn type_(&self) -> &str;

/// The `name` of a [`Target`], e.g. `"pod-of-beans"`.
Expand All @@ -324,10 +325,10 @@ pub trait TargetDisplay {

/// Implements the [`TargetDisplay`] and [`fmt::Display`] traits for a target type.
macro_rules! impl_target_display {
($struct_name:ident, $target_type:ident) => {
($struct_name:ident, $target_type:ident, $target_type_display:literal) => {
impl TargetDisplay for $struct_name {
fn type_(&self) -> &str {
stringify!($target_type)
$target_type_display
}

fn name(&self) -> &str {
Expand Down Expand Up @@ -355,12 +356,12 @@ macro_rules! impl_target_display {
};
}

impl_target_display!(PodTarget, pod);
impl_target_display!(DeploymentTarget, deployment);
impl_target_display!(RolloutTarget, rollout);
impl_target_display!(JobTarget, job);
impl_target_display!(CronJobTarget, cron_job);
impl_target_display!(StatefulSetTarget, stateful_set);
impl_target_display!(PodTarget, pod, "pod");
impl_target_display!(DeploymentTarget, deployment, "deployment");
impl_target_display!(RolloutTarget, rollout, "rollout");
impl_target_display!(JobTarget, job, "job");
impl_target_display!(CronJobTarget, cron_job, "cronjob");
impl_target_display!(StatefulSetTarget, stateful_set, "statefulset");

impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -377,7 +378,6 @@ impl fmt::Display for Target {
}

impl TargetDisplay for Target {
#[tracing::instrument(level = "trace", ret)]
fn type_(&self) -> &str {
match self {
Target::Targetless => "targetless",
Expand All @@ -390,7 +390,6 @@ impl TargetDisplay for Target {
}
}

#[tracing::instrument(level = "trace", ret)]
fn name(&self) -> &str {
match self {
Target::Targetless => "targetless",
Expand All @@ -403,7 +402,6 @@ impl TargetDisplay for Target {
}
}

#[tracing::instrument(level = "trace", ret)]
fn container(&self) -> Option<&String> {
match self {
Target::Targetless => None,
Expand Down
20 changes: 11 additions & 9 deletions mirrord/operator/src/crd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use kube_target::{KubeTarget, UnknownTargetType};
pub use mirrord_config::feature::split_queues::QueueId;
use mirrord_config::{
feature::split_queues::{SplitQueuesConfig, SqsMessageFilter},
target::{Target, TargetConfig},
target::{Target, TargetConfig, TargetDisplay},
};
use schemars::JsonSchema;
use semver::Version;
Expand Down Expand Up @@ -47,14 +47,16 @@ impl TargetCrd {
///
/// It's used to connect to a resource through the operator.
pub fn urlfied_name(target: &Target) -> String {
let (type_name, target, container) = match target {
Target::Deployment(target) => ("deploy", &target.deployment, &target.container),
Target::Pod(target) => ("pod", &target.pod, &target.container),
Target::Rollout(target) => ("rollout", &target.rollout, &target.container),
Target::Job(target) => ("job", &target.job, &target.container),
Target::CronJob(target) => ("cronjob", &target.cron_job, &target.container),
Target::StatefulSet(target) => ("statefulset", &target.stateful_set, &target.container),
Target::Targetless => return TARGETLESS_TARGET_NAME.to_string(),
let type_name = target.type_();

let (target, container) = match target {
Target::Deployment(target) => (&target.deployment, &target.container),
Target::Pod(target) => (&target.pod, &target.container),
Target::Rollout(target) => (&target.rollout, &target.container),
Target::Job(target) => (&target.job, &target.container),
Target::CronJob(target) => (&target.cron_job, &target.container),
Target::StatefulSet(target) => (&target.stateful_set, &target.container),
Target::Targetless => return type_name.to_string(),
};

if let Some(container) = container {
Expand Down
35 changes: 15 additions & 20 deletions tests/src/operator/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@ pub async fn mirrord_ls(#[future] service_for_mirrord_ls: KubeService) {
assert!(res.success());
let stdout = process.get_stdout().await;
let targets: Vec<String> = serde_json::from_str(&stdout).unwrap();
let re =
Regex::new(r"^(pod|deployment|stateful_set|cron_job|job)/.+(/container/.+)?$").unwrap();
targets
.iter()
.for_each(|output| assert!(re.is_match(output)));
let re = Regex::new(r"^(pod|deployment|statefulset|cronjob|job)/.+(/container/.+)?$").unwrap();
targets.iter().for_each(|output| {
assert!(
re.is_match(output),
"output line {output} does not match regex {re}"
);
});

assert!(targets
.iter()
.any(|output| output.starts_with(&format!("pod/{}", service.name))));
assert!(targets
.iter()
.any(|output| output.starts_with(&format!("deployment/{}", service.name))));
assert!(targets
.iter()
.any(|output| output.starts_with(&format!("stateful_set/{}", service.name))));
assert!(targets
.iter()
.any(|output| output.starts_with(&format!("cron_job/{}", service.name))));
assert!(targets
.iter()
.any(|output| output.starts_with(&format!("job/{}", service.name))));
for target_type in ["pod", "deployment", "statefulset", "cronjob", "job"] {
assert!(
targets
.iter()
.any(|output| output.starts_with(&format!("{target_type}/{}", service.name))),
"no {target_type} target was found in the output",
);
}
}

0 comments on commit 69e801b

Please sign in to comment.