Skip to content

Commit

Permalink
Injecting container in the operator client
Browse files Browse the repository at this point in the history
  • Loading branch information
Razz4780 committed Jan 21, 2025
1 parent db82881 commit 9508263
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/+replace-container.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Operator client now injects the container into the target CRD, if one was not specified by the user.
17 changes: 17 additions & 0 deletions mirrord/kube/src/resolved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ impl<const CHECKED: bool> ResolvedTarget<CHECKED> {
}
}

/// Convenient way of mutating the target container.
///
/// # Panic
///
/// Panics if this is a [`ResolvedTarget::Targetless`] variant.
pub fn container_mut(&mut self) -> &mut Option<String> {
match self {
Self::Deployment(ResolvedResource { container, .. }) => container,
Self::Rollout(ResolvedResource { container, .. }) => container,
Self::Job(ResolvedResource { container, .. }) => container,
Self::Pod(ResolvedResource { container, .. }) => container,
Self::StatefulSet(ResolvedResource { container, .. }) => container,
Self::CronJob(ResolvedResource { container, .. }) => container,
Self::Targetless(..) => panic!("targetless mode involves no target container"),
}
}

/// Is this a [`ResolvedTarget::Deployment`], and is it empty?
pub fn empty_deployment(&self) -> bool {
if let Self::Deployment(ResolvedResource { resource, .. }) = self {
Expand Down
9 changes: 6 additions & 3 deletions mirrord/operator/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,24 +611,27 @@ impl OperatorApi<PreparedClientCert> {
.and_then(|copy_crd| copy_crd.creator_session.id),
)
} else {
let target = target.assert_valid_mirrord_target(self.client()).await?;
let mut target = target.assert_valid_mirrord_target(self.client()).await?;

Check warning on line 615 in mirrord/operator/src/client.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/mirrord/mirrord/mirrord/operator/src/client.rs
// `targetless` has no `RuntimeData`!
if matches!(target, ResolvedTarget::Targetless(_)).not() {
// This check also ensures that the user can see at least one pod from the target set.
let runtime_data = target
.runtime_data(self.client(), target.namespace())
.await?;

if runtime_data.guessed_container {
progress.warning(
format!(
"Target has multiple containers, mirrord picked \"{}\".\
To target a different one, include it in the target path.",
"Target has multiple containers, mirrord picked \"{}\". \
To target a different one, include it in the target path.",
runtime_data.container_name
)
.as_str(),
);
}

target.container_mut().replace(runtime_data.container_name);
}

(
Expand Down

0 comments on commit 9508263

Please sign in to comment.