Skip to content

[Merged by Bors] - Added resolve to S3ConnectionDef #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/commons/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct S3BucketSpec {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bucket_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub connection: Option<ConnectionDef>,
pub connection: Option<S3ConnectionDef>,
}

impl S3BucketSpec {
Expand All @@ -57,14 +57,10 @@ impl S3BucketSpec {
namespace: Option<&str>,
) -> OperatorResult<InlinedS3BucketSpec> {
match self.connection.as_ref() {
Some(ConnectionDef::Reference(res_name)) => Ok(InlinedS3BucketSpec {
connection: Some(S3ConnectionSpec::get(res_name, client, namespace).await?),
Some(connection_def) => Ok(InlinedS3BucketSpec {
connection: Some(connection_def.resolve(client, namespace).await?),
bucket_name: self.bucket_name.clone(),
}),
Some(ConnectionDef::Inline(conn_spec)) => Ok(InlinedS3BucketSpec {
bucket_name: self.bucket_name.clone(),
connection: Some(conn_spec.clone()),
}),
None => Ok(InlinedS3BucketSpec {
bucket_name: self.bucket_name.clone(),
connection: None,
Expand Down Expand Up @@ -135,8 +131,8 @@ impl S3BucketDef {
) -> OperatorResult<InlinedS3BucketSpec> {
match self {
S3BucketDef::Inline(s3_bucket) => s3_bucket.inlined(client, namespace).await,
S3BucketDef::Reference(_s3_bucket) => {
S3BucketSpec::get(_s3_bucket.as_str(), client, namespace)
S3BucketDef::Reference(s3_bucket) => {
S3BucketSpec::get(s3_bucket.as_str(), client, namespace)
.await?
.inlined(client, namespace)
.await
Expand All @@ -145,14 +141,30 @@ impl S3BucketDef {
}
}

/// S3 connection definition used by [S3BucketSpec]
/// Operators are expected to define fields for this type in order to work with S3 connections.
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ConnectionDef {
pub enum S3ConnectionDef {
Inline(S3ConnectionSpec),
Reference(String),
}

impl S3ConnectionDef {
/// Returns an [S3ConnectionSpec].
pub async fn resolve(
&self,
client: &Client,
namespace: Option<&str>,
) -> OperatorResult<S3ConnectionSpec> {
match self {
S3ConnectionDef::Inline(s3_connection_spec) => Ok(s3_connection_spec.clone()),
S3ConnectionDef::Reference(s3_conn_reference) => {
S3ConnectionSpec::get(s3_conn_reference, client, namespace).await
}
}
}
}

/// S3 connection definition as CRD.
#[derive(CustomResource, Clone, Debug, Default, Deserialize, JsonSchema, PartialEq, Serialize)]
#[kube(
Expand Down Expand Up @@ -197,14 +209,14 @@ impl S3ConnectionSpec {

#[cfg(test)]
mod test {
use crate::commons::s3::ConnectionDef;
use crate::commons::s3::S3ConnectionDef;
use crate::commons::s3::{S3BucketSpec, S3ConnectionSpec};

#[test]
fn test_ser_inline() {
let bucket = S3BucketSpec {
bucket_name: Some("test-bucket-name".to_owned()),
connection: Some(ConnectionDef::Inline(S3ConnectionSpec {
connection: Some(S3ConnectionDef::Inline(S3ConnectionSpec {
host: Some("host".to_owned()),
port: Some(8080),
secret_class: None,
Expand Down