Skip to content

Commit

Permalink
Merge pull request #2724 from ecpullen/testsys-builder-eks
Browse files Browse the repository at this point in the history
testsys: Use `builder` for EKS CRD creation
  • Loading branch information
ecpullen authored Jan 12, 2023
2 parents 4f2bf9b + f14ed8d commit 42ce2ba
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 82 deletions.
4 changes: 0 additions & 4 deletions tools/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tools/testsys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ clap = { version = "3", features = ["derive", "env"] }
env_logger = "0.9"
futures = "0.3.8"
handlebars = "4.3"
k8s-openapi = { version = "0.16", features = ["v1_20", "api"], default-features = false }
kube-client = { version = "0.75"}
log = "0.4"
maplit = "1.0.2"
Expand Down
12 changes: 5 additions & 7 deletions tools/testsys/src/aws_ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bottlerocket_types::agent_config::{ClusterType, EcsClusterConfig, EcsTestCon
use log::debug;
use maplit::btreemap;
use model::{Crd, DestructionPolicy};
use snafu::OptionExt;
use snafu::{OptionExt, ResultExt};
use std::collections::BTreeMap;

/// A `CrdCreator` responsible for creating crd related to `aws-ecs` variants.
Expand Down Expand Up @@ -91,9 +91,8 @@ impl CrdCreator for AwsEcsCreator {
)
.set_secrets(Some(cluster_input.crd_input.config.secrets.clone()))
.build(cluster_input.cluster_name)
.map_err(|e| error::Error::Build {
what: "ECS cluster CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "ECS cluster CRD",
})?;

Ok(CreateCrdOutput::NewCrd(Box::new(Crd::Resource(ecs_crd))))
Expand Down Expand Up @@ -169,9 +168,8 @@ impl CrdCreator for AwsEcsCreator {
cluster_resource_name,
test_input.name_suffix.unwrap_or_default()
))
.map_err(|e| error::Error::Build {
what: "ECS test CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "ECS test CRD",
})?;

Ok(CreateCrdOutput::NewCrd(Box::new(Crd::Test(test_crd))))
Expand Down
84 changes: 33 additions & 51 deletions tools/testsys/src/aws_k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ use crate::sonobuoy::sonobuoy_crd;
use bottlerocket_types::agent_config::{
ClusterType, CreationPolicy, EksClusterConfig, EksctlConfig, K8sVersion,
};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use maplit::btreemap;
use model::constants::NAMESPACE;
use model::{Agent, Configuration, Crd, DestructionPolicy, Resource, ResourceSpec};
use model::{Crd, DestructionPolicy};
use snafu::{OptionExt, ResultExt};
use std::collections::BTreeMap;
use std::str::FromStr;
Expand Down Expand Up @@ -80,54 +78,38 @@ impl CrdCreator for AwsK8sCreator {
version: cluster_input.crd_input.variant.to_string(),
})?;

let eks_crd = Resource {
metadata: ObjectMeta {
name: Some(cluster_input.cluster_name.to_string()),
namespace: Some(NAMESPACE.into()),
labels: Some(labels),
..Default::default()
},
spec: ResourceSpec {
depends_on: None,
conflicts_with: None,
agent: Agent {
name: "eks-provider".to_string(),
image: cluster_input
.crd_input
.images
.eks_resource_agent_image
.to_owned()
.expect("Missing default image for EKS resource agent"),
pull_secret: cluster_input
.crd_input
.images
.testsys_agent_pull_secret
.clone(),
keep_running: false,
timeout: None,
configuration: Some(
EksClusterConfig {
creation_policy: Some(CreationPolicy::IfNotExists),
assume_role: cluster_input.crd_input.config.agent_role.clone(),
config: EksctlConfig::Args {
cluster_name: cluster_input.cluster_name.to_string(),
region: Some(self.region.clone()),
zones: None,
version: Some(cluster_version),
},
}
.into_map()
.context(error::IntoMapSnafu {
what: "eks crd config".to_string(),
})?,
),
secrets: Some(cluster_input.crd_input.config.secrets.clone()),
..Default::default()
},
destruction_policy: DestructionPolicy::Never,
},
status: None,
};
let eks_crd = EksClusterConfig::builder()
.creation_policy(CreationPolicy::IfNotExists)
.assume_role(cluster_input.crd_input.config.agent_role.clone())
.config(EksctlConfig::Args {
cluster_name: cluster_input.cluster_name.to_string(),
region: Some(self.region.clone()),
zones: None,
version: Some(cluster_version),
})
.image(
cluster_input
.crd_input
.images
.eks_resource_agent_image
.to_owned()
.expect("Missing default image for EKS resource agent"),
)
.set_image_pull_secret(
cluster_input
.crd_input
.images
.testsys_agent_pull_secret
.clone(),
)
.set_labels(Some(labels))
.set_secrets(Some(cluster_input.crd_input.config.secrets.clone()))
.destruction_policy(DestructionPolicy::Never)
.build(cluster_input.cluster_name.to_string())
.context(error::BuildSnafu {
what: "EKS cluster CRD",
})?;

Ok(CreateCrdOutput::NewCrd(Box::new(Crd::Resource(eks_crd))))
}

Expand Down
5 changes: 2 additions & 3 deletions tools/testsys/src/aws_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ pub(crate) async fn ec2_crd<'a>(
let suffix: String = repeat_with(fastrand::lowercase).take(4).collect();
ec2_builder
.build(format!("{}-instances-{}", cluster_name, suffix))
.map_err(|e| error::Error::Build {
what: "EC2 instance provider CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "EC2 instance provider CRD",
})
}
7 changes: 5 additions & 2 deletions tools/testsys/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ pub type Result<T> = std::result::Result<T, Error>;
pub enum Error {
// `error` must be used instead of `source` because the build function returns
// `std::error::Error` but not `std::error::Error + Sync + Send`.
#[snafu(display("Unable to build '{}': {}", what, error))]
Build { what: String, error: String },
#[snafu(display("Unable to build '{}': {}", what, source))]
Build {
what: String,
source: Box<dyn std::error::Error + Sync + Send>,
},

#[snafu(display("Unable to build datacenter credentials: {}", source))]
CredsBuild {
Expand Down
7 changes: 3 additions & 4 deletions tools/testsys/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::error::{self, Result};
use bottlerocket_types::agent_config::MigrationConfig;
use maplit::btreemap;
use model::Test;
use snafu::OptionExt;
use snafu::{OptionExt, ResultExt};

/// Create a CRD for migrating Bottlerocket instances using SSM commands.
/// `aws_region_override` allows the region that's normally derived from the cluster resource CRD to be overridden
Expand Down Expand Up @@ -88,8 +88,7 @@ pub(crate) fn migration_crd(
cluster_resource_name,
migration_input.name_suffix.unwrap_or_default()
))
.map_err(|e| error::Error::Build {
what: "migration CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "migration CRD",
})
}
6 changes: 3 additions & 3 deletions tools/testsys/src/sonobuoy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::run::KnownTestType;
use bottlerocket_types::agent_config::{SonobuoyConfig, SonobuoyMode};
use maplit::btreemap;
use model::Test;
use snafu::ResultExt;
use std::fmt::Display;

/// Create a Sonobuoy CRD for K8s conformance and quick testing.
Expand Down Expand Up @@ -67,9 +68,8 @@ pub(crate) fn sonobuoy_crd(test_input: TestInput) -> Result<Test> {
cluster_resource_name,
test_input.name_suffix.unwrap_or("-test")
))
.map_err(|e| error::Error::Build {
what: "sonobuoy CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "Sonobuoy CRD",
})
}

Expand Down
12 changes: 5 additions & 7 deletions tools/testsys/src/vmware_k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bottlerocket_types::agent_config::{
use maplit::btreemap;
use model::{Crd, DestructionPolicy, SecretName};
use pubsys_config::vmware::Datacenter;
use snafu::OptionExt;
use snafu::{OptionExt, ResultExt};
use std::collections::BTreeMap;
use std::iter::repeat_with;
use std::str::FromStr;
Expand Down Expand Up @@ -150,9 +150,8 @@ impl CrdCreator for VmwareK8sCreator {
))
.privileged(true)
.build(cluster_input.cluster_name)
.map_err(|e| error::Error::Build {
what: "vSphere K8s cluster CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "vSphere K8s cluster CRD",
})?;
Ok(CreateCrdOutput::NewCrd(Box::new(Crd::Resource(
vsphere_k8s_crd,
Expand Down Expand Up @@ -230,9 +229,8 @@ impl CrdCreator for VmwareK8sCreator {
))
.depends_on(cluster_name)
.build(format!("{}-vms-{}", cluster_name, suffix))
.map_err(|e| error::Error::Build {
what: "vSphere VM CRD".to_string(),
error: e.to_string(),
.context(error::BuildSnafu {
what: "vSphere VM CRD",
})?;
Ok(CreateCrdOutput::NewCrd(Box::new(Crd::Resource(
vsphere_vm_crd,
Expand Down

0 comments on commit 42ce2ba

Please sign in to comment.