Skip to main content

K8sClient

Struct K8sClient 

Source
pub struct K8sClient { /* private fields */ }
Expand description

A namespaced kube client plus this process’s instance identity.

Implementations§

Source§

impl K8sClient

Source

pub async fn connect(namespace: impl Into<String>) -> Result<Self, K8sError>

Install the rustls CryptoProvider, connect a client from the ambient config (in-cluster ServiceAccount or kubeconfig), and scope it to namespace.

Source

pub fn from_client(client: Client, namespace: String) -> Self

Wrap an already-constructed client (used by tests and callers that share a client across backends).

Source

pub fn namespace(&self) -> &str

The namespace Pods are created in.

Source

pub fn instance_id(&self) -> &str

This process’s instance identity (fakecloud-<pid>), used for the labels::INSTANCE label.

Source

pub fn client(&self) -> &Client

The underlying client, for callers that need raw API access.

Source

pub fn pods(&self) -> Api<Pod>

Namespaced Pod API handle.

Source

pub async fn create_pod(&self, pod: &Pod) -> Result<(), K8sError>

Create pod, first deleting any stale Pod with the same name left behind by a previous process (which would otherwise make create return 409 Conflict). Retries the create a few times while the API server finishes deleting the old Pod.

Source

pub async fn wait_for_pod_ip( &self, name: &str, timeout: Duration, ) -> Result<String, K8sError>

Poll until the Pod has a non-empty status.podIP and phase Running, returning the IP. Errors if the Pod reaches a terminal phase (Failed/Succeeded) during startup or if timeout elapses first.

Source

pub async fn wait_for_tcp( ip: &str, port: u16, timeout: Duration, ) -> Result<(), K8sError>

TCP-handshake ip:port until it accepts a connection or timeout elapses. A Pod being Running doesn’t guarantee the process inside it is listening yet, so backends follow wait_for_pod_ip with this.

Source

pub async fn exec( &self, pod: &str, container: Option<&str>, cmd: &[&str], ) -> Result<ExecOutput, K8sError>

Run cmd inside pod (in container, or the default container when None) and collect stdout/stderr/exit-code. This is the k8s equivalent of docker exec — used for operations like issuing redis-cli commands or copying a file out of a Pod.

Source

pub async fn exec_with_stdin( &self, pod: &str, container: Option<&str>, cmd: &[&str], stdin: &[u8], ) -> Result<ExecOutput, K8sError>

Like exec but writes stdin to the command’s standard input first (then closes it). Used for piping a SQL dump into psql/mysql during a restore — the k8s equivalent of docker exec -i ... < dump.

Source

pub async fn pod_logs( &self, pod: &str, container: Option<&str>, ) -> Result<String, K8sError>

Fetch a Pod container’s logs (the k8s equivalent of docker logs) — used for log-marker readiness on engines that don’t expose a connect-based probe (Oracle / SQL Server / Db2).

Source

pub async fn delete_pod(&self, name: &str)

Delete a Pod by name. Idempotent — a 404 (already gone) is treated as success; other errors are logged but not returned, since teardown is best-effort.

Source

pub async fn reap_stale(&self, service: &str) -> usize

Delete Pods of the given service left behind by a different process. Lists Pods labelled with both labels::MANAGED_BY and the service value, and deletes those whose labels::INSTANCE differs from this process’s. Mirrors the Docker reaper so a restart doesn’t leak the previous run’s Pods. Returns the count reaped.

Source

pub fn network_policies(&self) -> Api<NetworkPolicy>

Namespaced NetworkPolicy API handle.

Source

pub async fn apply_network_policy(&self, np: &NetworkPolicy)

Create or replace a NetworkPolicy (delete-then-create, like create_pod, so a re-apply with changed rules always lands). Best-effort: errors are logged, not propagated, since a failed policy apply must never fail the originating EC2 API call.

Source

pub async fn prune_network_policies(&self, keep: &HashSet<String>)

Delete every NetworkPolicy owned by this process (managed-by + this instance label) whose name is not in keep. Prunes policies for instances that have since terminated. Best-effort.

Source

pub async fn cni_component_names(&self) -> Vec<String>

Best-effort detection of the cluster CNI from Pod names across the namespaces CNIs commonly install into (e.g. calico-node-*, cilium-*, kindnet-*). Returns the matched component names; the caller maps them to a driver. An empty result (lists failed or no recognizable CNI) maps to “unknown”.

Scans kube-system plus the operator namespaces Calico/Cilium use (calico-system, tigera-operator, cilium) so a Tigera-operator or dedicated-namespace install isn’t mis-reported as non-enforcing (bug-hunt 2026-06-18 finding 1.6). Per-namespace list errors (RBAC / absent namespace) are swallowed.

Trait Implementations§

Source§

impl Clone for K8sClient

Source§

fn clone(&self) -> K8sClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more