use std::time::Duration;
use kaniop_k8s_util::error::Error;
use thiserror::Error;
use crate::reconcile::identity::RadiusIdentityError;
use crate::reconcile::image::RadiusImageError;
use crate::reconcile::inputs::{InputsError, RadiusPreflightError};
use crate::reconcile::status::ConditionKind;
use crate::reconcile::token::MAX_TOKENS;
use crate::reconcile::workload::RadiusWorkloadError;
const RETRY_CONCURRENT: Duration = Duration::from_secs(5);
const RETRY_CANDIDATE: Duration = Duration::from_secs(30);
const RETRY_TOKEN_BOUND: Duration = Duration::from_secs(60);
#[derive(Debug, Error)]
pub enum ExecError {
#[error("Kanidm account {account} exists but is not owned by this resource")]
AccountNotOwned { account: String },
#[error("{kind} {namespace}/{name} exists but is not controlled by this resource")]
ForeignObject {
kind: String,
namespace: String,
name: String,
},
#[error("config Secret {namespace}/{name} exists with different content")]
SecretConflict { namespace: String, name: String },
#[error("{kind} {namespace}/{name} changed while it was being deleted")]
ConcurrentMutation {
kind: String,
namespace: String,
name: String,
},
#[error("Kanidm rejected the newly minted token for account {account}")]
CandidateRejected { account: String },
#[error("cannot identify the newly minted token on account {account}")]
MintedTokenNotFound { account: String },
#[error("{kind} is missing {field}")]
MissingMetadata {
kind: &'static str,
field: &'static str,
},
#[error(transparent)]
Api(#[from] Error),
}
pub type ExecResult<T> = Result<T, ExecError>;
pub(crate) fn kanidm_error(
operation: &str,
entity: &str,
error: kanidm_client::ClientError,
) -> ExecError {
ExecError::Api(Error::KanidmClientError(
format!("failed to {operation} {entity}"),
Box::new(error),
))
}
pub(crate) fn kube_error(
operation: &str,
kind: &str,
namespace: &str,
name: &str,
error: kube::Error,
) -> ExecError {
ExecError::Api(Error::kube_error(operation, kind, namespace, name, error))
}
#[derive(Debug, Error)]
pub enum RadiusBlock {
#[error("referenced Kanidm {kanidm} does not exist")]
KanidmNotFound { kanidm: String },
#[error("namespace is not selected by radiusServerNamespaceSelector of Kanidm {kanidm}")]
NamespaceNotWatched { kanidm: String },
#[error("{0}")]
Inputs(#[from] RadiusPreflightError),
#[error("{0}")]
Image(#[from] RadiusImageError),
#[error("{0}")]
Workload(#[from] RadiusWorkloadError),
#[error("{0}")]
Identity(#[from] RadiusIdentityError),
#[error(
"a new token is needed but the account already holds {MAX_TOKENS} tokens; waiting for \
unused tokens to be revoked"
)]
TooManyTokens,
#[error("the config Secret of the Deployment template has no readable token")]
BearerUnreadable,
#[error("{error}")]
Exec {
#[source]
error: ExecError,
stage: ConditionKind,
},
}
impl RadiusBlock {
pub fn reason(&self) -> &'static str {
match self {
Self::KanidmNotFound { .. } => "KanidmNotFound",
Self::NamespaceNotWatched { .. } => "NamespaceNotWatched",
Self::Inputs(error) => error.reason(),
Self::Image(_) => "ImageUnresolved",
Self::Workload(_) => "InvalidWorkload",
Self::Identity(_) => "InvalidIdentity",
Self::TooManyTokens => "TooManyTokens",
Self::BearerUnreadable => "BearerUnreadable",
Self::Exec { error, .. } => match error {
ExecError::AccountNotOwned { .. } => "OwnershipConflict",
ExecError::ForeignObject { .. } => "ForeignObject",
ExecError::SecretConflict { .. } => "SecretConflict",
ExecError::ConcurrentMutation { .. } => "ConcurrentMutation",
ExecError::CandidateRejected { .. } => "CandidateRejected",
ExecError::MintedTokenNotFound { .. } => "MintedTokenNotFound",
ExecError::MissingMetadata { .. } => "MissingMetadata",
ExecError::Api(_) => "TransientError",
},
}
}
pub fn condition(&self) -> ConditionKind {
match self {
Self::KanidmNotFound { .. } | Self::NamespaceNotWatched { .. } | Self::Identity(_) => {
ConditionKind::IdentityReady
}
Self::TooManyTokens | Self::BearerUnreadable => ConditionKind::Rotating,
Self::Inputs(_) | Self::Image(_) | Self::Workload(_) => {
ConditionKind::ConfigurationReady
}
Self::Exec { error, stage } => match error {
ExecError::AccountNotOwned { .. } => ConditionKind::IdentityReady,
ExecError::ForeignObject { kind, .. } if kind == "Deployment" => {
ConditionKind::Available
}
ExecError::ForeignObject { kind, .. } if kind == "Service" => {
ConditionKind::Exposed
}
ExecError::CandidateRejected { .. } | ExecError::MintedTokenNotFound { .. } => {
ConditionKind::Rotating
}
_ => *stage,
},
}
}
pub fn retry_after(&self) -> Option<Duration> {
match self {
Self::Exec {
error: ExecError::ConcurrentMutation { .. },
..
} => Some(RETRY_CONCURRENT),
Self::Exec {
error: ExecError::CandidateRejected { .. },
..
} => Some(RETRY_CANDIDATE),
Self::TooManyTokens => Some(RETRY_TOKEN_BOUND),
_ => None,
}
}
}
#[derive(Debug, Error)]
pub enum Failure {
#[error(transparent)]
Block(#[from] RadiusBlock),
#[error("{error}")]
Transient {
condition: ConditionKind,
error: Error,
},
}
impl Failure {
pub fn exec(condition: ConditionKind) -> impl FnOnce(ExecError) -> Failure {
move |error| match error {
ExecError::Api(error) => Failure::Transient { condition, error },
error => Failure::Block(RadiusBlock::Exec {
error,
stage: condition,
}),
}
}
pub fn inputs(error: InputsError) -> Failure {
match error {
InputsError::Invalid(error) => Failure::Block(RadiusBlock::Inputs(error)),
InputsError::Api(error) => Failure::Transient {
condition: ConditionKind::ConfigurationReady,
error,
},
}
}
pub fn transient(condition: ConditionKind) -> impl FnOnce(Error) -> Failure {
move |error| Failure::Transient { condition, error }
}
}