Skip to content

Commit

Permalink
Suggest mfT when user uses HTTP filter (#2747)
Browse files Browse the repository at this point in the history
* Add warning for users using http filters without mft

* Show users either multipod or http filter warning if both apply

* Add changelog

* Fix choosing random option for warning to display

* Move selection logic to function
  • Loading branch information
gememma authored Sep 10, 2024
1 parent e7211c5 commit 54fcc95
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 35 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ rand = "0.8"
streammap-ext = "0.1"
num-traits = "0.2"
regex = { version = "1", features = ["unicode-case"] }
miette = "5"
fancy-regex = { version = "0.13" }
enum_dispatch = "0.3"

Expand Down
1 change: 1 addition & 0 deletions changelog.d/2701.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Suggest mfT when user uses HTTP filter, only show user one warning for multipod/ HTTP filter.
1 change: 1 addition & 0 deletions mirrord/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ tokio-rustls = "0.26"
tokio-stream = { workspace = true, features = ["net"] }
tokio-retry = "0.3"
regex.workspace = true
mid = "2.1.0"


[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
136 changes: 102 additions & 34 deletions mirrord/cli/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use mirrord_kube::{
};
use mirrord_operator::client::{OperatorApi, OperatorSessionConnection};
use mirrord_progress::{
messages::MULTIPOD_WARNING, IdeAction, IdeMessage, NotificationLevel, Progress,
messages::{HTTP_FILTER_WARNING, MULTIPOD_WARNING},
IdeAction, IdeMessage, NotificationLevel, Progress,
};
use mirrord_protocol::{ClientMessage, DaemonMessage};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -120,40 +121,33 @@ where
return Err(CliError::FeatureRequiresOperatorError("copy_target".into()));
}

if matches!(
config.target,
mirrord_config::target::TargetConfig {
path: Some(
mirrord_config::target::Target::Deployment { .. }
| mirrord_config::target::Target::Rollout(..)
),
..
}
match (
// user in mutipod without operator
matches!(
config.target,
mirrord_config::target::TargetConfig {
path: Some(
mirrord_config::target::Target::Deployment { .. }
| mirrord_config::target::Target::Rollout(..)
),
..
}
),
// user using http filter(s) without operator
config.feature.network.incoming.http_filter.is_filter_set(),
) {
// Send to IDEs that we're in multi-pod without operator.
progress.ide(serde_json::to_value(IdeMessage {
id: MULTIPOD_WARNING.0.to_string(),
level: NotificationLevel::Warning,
text: MULTIPOD_WARNING.1.to_string(),
actions: {
let mut actions = HashSet::new();
actions.insert(IdeAction::Link {
label: "Get started (read the docs)".to_string(),
link: "https://mirrord.dev/docs/overview/teams/?utm_source=multipodwarn&utm_medium=plugin".to_string(),
});
actions.insert(IdeAction::Link {
label: "Try it now".to_string(),
link: "https://app.metalbear.co/".to_string(),
});

actions
},
})?);
// This is CLI Only because the extensions also implement this check with better messaging.
progress.print("When targeting multi-pod deployments, mirrord impersonates the first pod in the deployment.");
progress.print("Support for multi-pod impersonation requires the mirrord operator, which is part of mirrord for Teams.");
progress.print("You can get started with mirrord for Teams at this link: https://mirrord.dev/docs/overview/teams/?utm_source=multipodwarn&utm_medium=cli");
}
(true, true) => {
// only show user one of the two msgs - each user should always be shown same msg
if user_persistent_random_message_select() {
show_multipod_warning(progress)?
} else {
show_http_filter_warning(progress)?
}
}
(true, false) => show_multipod_warning(progress)?,
(false, true) => show_http_filter_warning(progress)?,
_ => (),
};

let k8s_api = KubernetesAPI::create(config)
.await
Expand Down Expand Up @@ -185,3 +179,77 @@ where
AgentConnection { sender, receiver },
))
}

fn user_persistent_random_message_select() -> bool {
mid::get("mirrord")
.inspect_err(|error| tracing::error!(%error, "failed to obtain machine ID"))
.ok()
.unwrap_or_default()
.as_bytes()
.iter()
.copied()
.reduce(u8::wrapping_add)
.unwrap_or_default()
% 2
== 0
}

pub(crate) fn show_multipod_warning<P>(progress: &mut P) -> Result<(), CliError>
where
P: Progress + Send + Sync,
{
// Send to IDEs that we're in multi-pod without operator.
progress.ide(serde_json::to_value(IdeMessage {
id: MULTIPOD_WARNING.0.to_string(),
level: NotificationLevel::Warning,
text: MULTIPOD_WARNING.1.to_string(),
actions: {
let mut actions = HashSet::new();
actions.insert(IdeAction::Link {
label: "Get started (read the docs)".to_string(),
link: "https://mirrord.dev/docs/overview/teams/?utm_source=multipodwarn&utm_medium=plugin".to_string(),
});
actions.insert(IdeAction::Link {
label: "Try it now".to_string(),
link: "https://app.metalbear.co/".to_string(),
});

actions
},
})?);
// This is CLI Only because the extensions also implement this check with better messaging.
progress.print("When targeting multi-pod deployments, mirrord impersonates the first pod in the deployment.");
progress.print("Support for multi-pod impersonation requires the mirrord operator, which is part of mirrord for Teams.");
progress.print("You can get started with mirrord for Teams at this link: https://mirrord.dev/docs/overview/teams/?utm_source=multipodwarn&utm_medium=cli");
Ok(())
}

pub(crate) fn show_http_filter_warning<P>(progress: &mut P) -> Result<(), CliError>
where
P: Progress + Send + Sync,
{
// Send to IDEs that at an HTTP filter is set without operator.
progress.ide(serde_json::to_value(IdeMessage {
id: HTTP_FILTER_WARNING.0.to_string(),
level: NotificationLevel::Warning,
text: HTTP_FILTER_WARNING.1.to_string(),
actions: {
let mut actions = HashSet::new();
actions.insert(IdeAction::Link {
label: "Get started (read the docs)".to_string(),
link: "https://mirrord.dev/docs/overview/teams/?utm_source=httpfilter&utm_medium=plugin".to_string(),
});
actions.insert(IdeAction::Link {
label: "Try it now".to_string(),
link: "https://app.metalbear.co/".to_string(),
});

actions
},
})?);
// This is CLI Only because the extensions also implement this check with better messaging.
progress.print("You're using an HTTP filter, which generally indicates the use of a shared environment. If so, we recommend");
progress.print("considering mirrord for Teams, which is better suited to shared environments.");
progress.print("You can get started with mirrord for Teams at this link: https://mirrord.dev/docs/overview/teams/?utm_source=httpfilter&utm_medium=cli");
Ok(())
}
9 changes: 9 additions & 0 deletions mirrord/progress/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ pub const MULTIPOD_WARNING: (&str, &str) = (
which is part of mirrord for Teams.",
);

/// Warning when user is using an HTTP filter without MfT.
pub const HTTP_FILTER_WARNING: (&str, &str) = (
"http_filter_warning",
"You're using an HTTP filter, which generally indicates \
the use of a shared environment. If so, we recommend \
considering mirrord for Teams, which is better suited \
to shared environments.",
);

/// Warning when user tries to run `mirrord exec docker` (for example), instead of the correct
/// `mirrord container ...`.
pub const EXEC_CONTAINER_BINARY: &str = "`mirrord exec <docker|podman|nerdctl> ...` detected! \
Expand Down

0 comments on commit 54fcc95

Please sign in to comment.