Skip to content

Commit b0bfc32

Browse files
feat(stackable-operator/s3connection): Add region field defaulting to us-east-1 (#959)
* feat(stackable-operator/s3connection): Add region field defaulting to us-east-1 * refactor(stackable-operator/s3connection: Make the region field more complex to cater to the various use-cases: 1. Using S3 buckets from within AWS (users can choose to auto-discover via IMDS) 2. Using S3 buckets from outside AWS (users can choose the default, or set an optimal region based on the clients location). 3. Not using AWS S3 buckets, eg: MinIO or IONOS (users can ignore setting the field and the default region will be set to keep the AWS SDK happy, even though the region is not used for bucket operations). * test(stackable-operator): Add region new fields in helpers * chore(stackable-operator): Remove strum until needed, set serde rename styles * chore(stackable-operator): Rename associated function to name (since the struct is already representing the "region" * docs(stackable-operator): Improve docs * docs(stackable-operator): Improve docs * Apply suggestions from code review Co-authored-by: Techassi <git@techassi.dev> * chore(stackable-operator): Update changelog --------- Co-authored-by: Techassi <git@techassi.dev>
1 parent 6b6f802 commit b0bfc32

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

crates/stackable-operator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- Add `region` field to S3ConnectionSpec ([#959]).
10+
11+
[#959]: https://github.com/stackabletech/operator-rs/pull/959
12+
713
## [0.86.0] - 2025-01-30
814

915
### Added

crates/stackable-operator/src/commons/s3/crd.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ pub struct S3ConnectionSpec {
5656
#[serde(default, skip_serializing_if = "Option::is_none")]
5757
pub port: Option<u16>,
5858

59+
/// AWS service API region used by the AWS SDK when using AWS S3 buckets.
60+
///
61+
/// This defaults to `us-east-1` and can be ignored if not using AWS S3
62+
/// buckets.
63+
///
64+
/// NOTE: This is not the bucket region, and is used by the AWS SDK to
65+
/// construct endpoints for various AWS service APIs. It is only useful when
66+
/// using AWS S3 buckets.
67+
///
68+
/// When using AWS S3 buckets, you can configure optimal AWS service API
69+
/// connections in the following ways:
70+
/// - From **inside** AWS: Use an auto-discovery source (eg: AWS IMDS).
71+
/// - From **outside** AWS, or when IMDS is disabled, explicity set the
72+
/// region name nearest to where the client application is running from.
73+
#[serde(default)]
74+
pub region: AwsRegion,
75+
5976
/// Which access style to use.
6077
/// Defaults to virtual hosted-style as most of the data products out there.
6178
/// Have a look at the [AWS documentation](https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html).
@@ -85,3 +102,57 @@ pub enum S3AccessStyle {
85102
#[default]
86103
VirtualHosted,
87104
}
105+
106+
/// Set a named AWS region, or defer to an auto-discovery mechanism.
107+
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
108+
#[serde(rename_all = "camelCase")]
109+
pub enum AwsRegion {
110+
/// Defer region detection to an auto-discovery mechanism.
111+
Source(AwsRegionAutoDiscovery),
112+
113+
/// An explicit region, eg: eu-central-1
114+
Name(String),
115+
}
116+
117+
impl AwsRegion {
118+
/// Get the AWS region name.
119+
///
120+
/// Returns `None` if an auto-discovery source has been selected. Otherwise,
121+
/// it returns the configured region name.
122+
///
123+
/// Example usage:
124+
///
125+
/// ```
126+
/// # use stackable_operator::commons::s3::AwsRegion;
127+
/// # fn set_property(key: &str, value: String) {}
128+
/// # fn example(aws_region: AwsRegion) {
129+
/// if let Some(region_name) = aws_region.name() {
130+
/// // set some property if the region is set, or is the default.
131+
/// set_property("aws.region", region_name);
132+
/// };
133+
/// # }
134+
/// ```
135+
pub fn name(self) -> Option<String> {
136+
match self {
137+
AwsRegion::Name(name) => Some(name),
138+
AwsRegion::Source(_) => None,
139+
}
140+
}
141+
}
142+
143+
impl Default for AwsRegion {
144+
fn default() -> Self {
145+
Self::Name("us-east-1".to_owned())
146+
}
147+
}
148+
149+
/// AWS region auto-discovery mechanism.
150+
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
151+
#[serde(rename_all = "PascalCase")]
152+
pub enum AwsRegionAutoDiscovery {
153+
/// AWS Instance Meta Data Service.
154+
///
155+
/// This variant should result in no region being given to the AWS SDK,
156+
/// which should, in turn, query the AWS IMDS.
157+
AwsImds,
158+
}

crates/stackable-operator/src/commons/s3/helpers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ mod tests {
201201
access_style: Default::default(),
202202
credentials: None,
203203
tls: TlsClientDetails { tls: None },
204+
region: Default::default(),
204205
};
205206
let (volumes, mounts) = s3.volumes_and_mounts().unwrap();
206207

@@ -226,6 +227,7 @@ mod tests {
226227
}),
227228
}),
228229
},
230+
region: Default::default(),
229231
};
230232
let (mut volumes, mut mounts) = s3.volumes_and_mounts().unwrap();
231233

@@ -278,6 +280,7 @@ mod tests {
278280
verification: TlsVerification::None {},
279281
}),
280282
},
283+
region: Default::default(),
281284
};
282285
let (volumes, mounts) = s3.volumes_and_mounts().unwrap();
283286

0 commit comments

Comments
 (0)