From f14ed8d843a55a8ac499621a1f9a965f41d0f895 Mon Sep 17 00:00:00 2001 From: ecpullen Date: Tue, 10 Jan 2023 14:50:20 +0000 Subject: [PATCH] testsys: Use `builder` for EKS CRD creation Previously there was a bug with the builder that prevented it from being used with for EKS crd creation. That bug was resolved in v0.0.5. The error type from the builder was also fixed, so snafu could be used. --- tools/Cargo.lock | 4 -- tools/testsys/Cargo.toml | 1 - tools/testsys/src/aws_ecs.rs | 12 ++--- tools/testsys/src/aws_k8s.rs | 84 ++++++++++++------------------ tools/testsys/src/aws_resources.rs | 5 +- tools/testsys/src/error.rs | 7 ++- tools/testsys/src/migration.rs | 7 ++- tools/testsys/src/sonobuoy.rs | 6 +-- tools/testsys/src/vmware_k8s.rs | 12 ++--- 9 files changed, 56 insertions(+), 82 deletions(-) diff --git a/tools/Cargo.lock b/tools/Cargo.lock index 900b812ed1d..bd5802e328b 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -1708,12 +1708,9 @@ dependencies = [ "base64 0.13.1", "bytes", "chrono", - "http", - "percent-encoding", "serde", "serde-value", "serde_json", - "url", ] [[package]] @@ -3120,7 +3117,6 @@ dependencies = [ "fastrand", "futures", "handlebars", - "k8s-openapi", "kube-client", "log", "maplit", diff --git a/tools/testsys/Cargo.toml b/tools/testsys/Cargo.toml index c8583c0a763..4afbacc3121 100644 --- a/tools/testsys/Cargo.toml +++ b/tools/testsys/Cargo.toml @@ -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" diff --git a/tools/testsys/src/aws_ecs.rs b/tools/testsys/src/aws_ecs.rs index 159eaa6fd9d..acfb5f0cbc3 100644 --- a/tools/testsys/src/aws_ecs.rs +++ b/tools/testsys/src/aws_ecs.rs @@ -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. @@ -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)))) @@ -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)))) diff --git a/tools/testsys/src/aws_k8s.rs b/tools/testsys/src/aws_k8s.rs index 3377a754b4a..496efe3cb38 100644 --- a/tools/testsys/src/aws_k8s.rs +++ b/tools/testsys/src/aws_k8s.rs @@ -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; @@ -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)))) } diff --git a/tools/testsys/src/aws_resources.rs b/tools/testsys/src/aws_resources.rs index 3c87c3bf1b8..285f91e45ee 100644 --- a/tools/testsys/src/aws_resources.rs +++ b/tools/testsys/src/aws_resources.rs @@ -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", }) } diff --git a/tools/testsys/src/error.rs b/tools/testsys/src/error.rs index a7fa8471a30..2ce68b836b3 100644 --- a/tools/testsys/src/error.rs +++ b/tools/testsys/src/error.rs @@ -10,8 +10,11 @@ pub type Result = std::result::Result; 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, + }, #[snafu(display("Unable to build datacenter credentials: {}", source))] CredsBuild { diff --git a/tools/testsys/src/migration.rs b/tools/testsys/src/migration.rs index c3b77b91352..66725d8bdbc 100644 --- a/tools/testsys/src/migration.rs +++ b/tools/testsys/src/migration.rs @@ -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 @@ -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", }) } diff --git a/tools/testsys/src/sonobuoy.rs b/tools/testsys/src/sonobuoy.rs index 245017803b2..6f1e905b8bb 100644 --- a/tools/testsys/src/sonobuoy.rs +++ b/tools/testsys/src/sonobuoy.rs @@ -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. @@ -67,9 +68,8 @@ pub(crate) fn sonobuoy_crd(test_input: TestInput) -> Result { 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", }) } diff --git a/tools/testsys/src/vmware_k8s.rs b/tools/testsys/src/vmware_k8s.rs index 20de66e58db..59f9e9aafde 100644 --- a/tools/testsys/src/vmware_k8s.rs +++ b/tools/testsys/src/vmware_k8s.rs @@ -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; @@ -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, @@ -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,