Skip to content

Commit 08f5cb9

Browse files
feat: add single zone configuration
Before this change, there was no way to set up a specific zone for EC2 instance. This feature allows configuring a specific zone in Test.toml for a certain variant when launching its EC2 instance(s). Even though a minimum of two zones are needed when creating an EKS cluster, after the cluster is created this feature can limit one specific zone before launching the EC2 instances.
1 parent 56aecba commit 08f5cb9

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

bottlerocket/agents/src/bin/ec2-resource-agent/ec2_provider.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,14 @@ where
235235
for instance_type in instance_types {
236236
info!("Using instance type '{}'", instance_type);
237237

238+
let mut flex_subnet_ids = spec.clone().configuration.subnet_ids;
239+
240+
if flex_subnet_ids.len() <= 2 {
241+
flex_subnet_ids.remove(0);
242+
}
243+
238244
// Run the ec2 instances
239-
for subnet_id in &spec.configuration.subnet_ids {
245+
for subnet_id in flex_subnet_ids {
240246
info!("Launching '{}' in '{}'", instance_type, subnet_id);
241247
memo.current_status = format!("Launching '{}' in '{}'", instance_type, subnet_id);
242248
client
@@ -247,7 +253,7 @@ where
247253
.run_instances()
248254
.min_count(instance_count)
249255
.max_count(instance_count)
250-
.subnet_id(subnet_id)
256+
.subnet_id(subnet_id.clone())
251257
.set_security_group_ids(if spec.configuration.security_groups.is_empty() {
252258
None
253259
} else {

bottlerocket/agents/src/bin/eks-resource-agent/eks_provider.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ use serde_json::Value;
2222
use std::env::temp_dir;
2323
use std::path::Path;
2424
use std::process::Command;
25+
use std::str::FromStr;
2526
use testsys_model::{Configuration, SecretName};
2627

2728
use std::fs::File;
2829
use std::io::Write;
29-
use strum_macros::EnumString;
30+
use strum_macros::{AsRefStr, EnumString};
3031

3132
/// The default region for the cluster.
3233
const DEFAULT_REGION: &str = "us-west-2";
@@ -142,6 +143,17 @@ enum IPFamily {
142143
IPv4,
143144
}
144145

146+
#[derive(Debug, EnumString, AsRefStr)]
147+
#[strum(serialize_all = "kebab-case")]
148+
enum AvailabilityZones {
149+
#[strum(serialize = "us-west-2a")]
150+
UsWest2a,
151+
#[strum(serialize = "us-west-2b")]
152+
UsWest2b,
153+
#[strum(serialize = "us-west-2c")]
154+
UsWest2c,
155+
}
156+
145157
/// Configuration for setting up an EKS cluster using eksctl yaml file.
146158
///
147159
/// # Fields:
@@ -239,10 +251,23 @@ struct ManagedNodeGroup {
239251
min_size: i32,
240252
/// The maximum number of nodes in the managed node group.
241253
max_size: i32,
242-
// The desired number of nodes in the managed node group.
254+
/// The desired number of nodes in the managed node group.
243255
desired_capacity: i32,
244256
}
245257

258+
fn get_remaining_zones(exclude: String) -> Vec<AvailabilityZones> {
259+
let all_zones = vec![
260+
AvailabilityZones::UsWest2a,
261+
AvailabilityZones::UsWest2b,
262+
AvailabilityZones::UsWest2c,
263+
];
264+
265+
all_zones
266+
.into_iter()
267+
.filter(|zone| zone.as_ref() != exclude)
268+
.collect()
269+
}
270+
246271
#[allow(clippy::unwrap_or_default)]
247272
fn create_yaml(
248273
cluster_name: &str,
@@ -256,6 +281,24 @@ fn create_yaml(
256281
IPFamily::IPv4
257282
};
258283

284+
let mut eks_zones: Vec<String> = zones.clone().unwrap();
285+
286+
if zones.is_some() && zones.iter().len() < 2 {
287+
let zones_upwrap = zones.clone().unwrap().first().unwrap().to_string();
288+
if AvailabilityZones::from_str(&zones_upwrap).is_err() {
289+
return Err(ProviderError::new_with_context(
290+
Resources::Clear,
291+
format!("Input zone of {} in Test.toml is invalid", zones_upwrap),
292+
));
293+
}
294+
let remain_zone = get_remaining_zones(zones_upwrap)
295+
.first()
296+
.unwrap()
297+
.as_ref()
298+
.to_string();
299+
eks_zones.insert(0, remain_zone);
300+
}
301+
259302
let cluster = EksctlYamlConfig {
260303
api_version: "eksctl.io/v1alpha5".to_string(),
261304
kind: "ClusterConfig".to_string(),
@@ -264,7 +307,7 @@ fn create_yaml(
264307
region: region.to_string(),
265308
version: version.to_string(),
266309
},
267-
availability_zones: zones.clone().unwrap_or_else(Vec::new),
310+
availability_zones: eks_zones,
268311
kubernetes_network_config: KubernetesNetworkConfig {
269312
ip_family: set_ip_family,
270313
},

0 commit comments

Comments
 (0)