diff --git a/src/k8s_helpers.rs b/src/k8s_helpers.rs index b35298f..fdec8ff 100644 --- a/src/k8s_helpers.rs +++ b/src/k8s_helpers.rs @@ -40,12 +40,6 @@ pub fn create_secret_from_files( Ok(create_secret(secret_name, data)) } -pub fn create_selector(key: &str, value: &str) -> BTreeMap { - let mut btree = BTreeMap::new(); - btree.insert(key.to_string(), value.to_string()); - btree -} - #[allow(clippy::too_many_arguments)] pub fn create_replica_set( name: &ValidatorType, diff --git a/src/kubernetes.rs b/src/kubernetes.rs index 538584f..aaf20ff 100644 --- a/src/kubernetes.rs +++ b/src/kubernetes.rs @@ -197,8 +197,4 @@ impl<'a> Kubernetes<'a> { flags } - - pub fn create_selector(&self, key: &str, value: &str) -> BTreeMap { - k8s_helpers::create_selector(key, value) - } } diff --git a/src/lib.rs b/src/lib.rs index 29ca993..079c8d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,7 @@ pub mod genesis; pub mod k8s_helpers; pub mod kubernetes; pub mod release; +pub mod validator; pub mod validator_config; static BUILD: Emoji = Emoji("👷 ", ""); diff --git a/src/main.rs b/src/main.rs index efd24b3..abf5b7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use { genesis::{Genesis, GenesisFlags}, kubernetes::{Kubernetes, PodRequests}, release::{BuildConfig, BuildType, DeployMethod}, + validator::Validator, validator_config::ValidatorConfig, SolanaRoot, ValidatorType, }, @@ -429,22 +430,21 @@ async fn main() { deploy_method, ); - let validator_type = ValidatorType::Bootstrap; - let bootstrap_docker_image = DockerImage::new( + let mut bootstrap_validator = Validator::new(DockerImage::new( matches.value_of("registry_name").unwrap().to_string(), - validator_type, + ValidatorType::Bootstrap, matches.value_of("image_name").unwrap().to_string(), matches .value_of("image_tag") .unwrap_or_default() .to_string(), - ); + )); if build_config.docker_build() { - match docker.build_image(solana_root.get_root_path(), &bootstrap_docker_image) { + match docker.build_image(solana_root.get_root_path(), bootstrap_validator.image()) { Ok(_) => info!( "{} image built successfully", - bootstrap_docker_image.validator_type() + bootstrap_validator.validator_type() ), Err(err) => { error!("Exiting........ {}", err); @@ -452,10 +452,10 @@ async fn main() { } } - match DockerConfig::push_image(&bootstrap_docker_image) { + match DockerConfig::push_image(bootstrap_validator.image()) { Ok(_) => info!( "{} image pushed successfully", - bootstrap_docker_image.validator_type() + bootstrap_validator.validator_type() ), Err(err) => { error!("Exiting........ {}", err); @@ -464,17 +464,18 @@ async fn main() { } } - let bootstrap_secret = match kub_controller - .create_bootstrap_secret("bootstrap-accounts-secret", &config_directory) - { - Ok(secret) => secret, + match kub_controller.create_bootstrap_secret("bootstrap-accounts-secret", &config_directory) { + Ok(secret) => bootstrap_validator.set_secret(secret), Err(err) => { error!("Failed to create bootstrap secret! {}", err); return; } }; - match kub_controller.deploy_secret(&bootstrap_secret).await { + match kub_controller + .deploy_secret(bootstrap_validator.secret()) + .await + { Ok(_) => info!("Deployed Bootstrap Secret"), Err(err) => { error!("{}", err); @@ -482,30 +483,22 @@ async fn main() { } } - // Bootstrap needs two labels. Because it is going to have two services. - // One via Load Balancer, one direct - let mut bootstrap_rs_labels = - kub_controller.create_selector("validator/lb", "load-balancer-selector"); - bootstrap_rs_labels.insert( - "validator/name".to_string(), - "bootstrap-validator-selector".to_string(), - ); - bootstrap_rs_labels.insert("validator/type".to_string(), "bootstrap".to_string()); - + // Create bootstrap labels let identity_path = config_directory.join("bootstrap-validator/identity.json"); let bootstrap_keypair = read_keypair_file(identity_path).expect("Failed to read bootstrap keypair file"); - bootstrap_rs_labels.insert( - "validator/identity".to_string(), - bootstrap_keypair.pubkey().to_string(), - ); + bootstrap_validator.add_label("validator/lb", "load-balancer-selector"); + bootstrap_validator.add_label("validator/name", "bootstrap-validator-selector"); + bootstrap_validator.add_label("validator/type", "bootstrap"); + bootstrap_validator.add_label("validator/identity", bootstrap_keypair.pubkey().to_string()); - let _bootstrap_replica_set = match kub_controller.create_bootstrap_validator_replica_set( - &bootstrap_docker_image, - bootstrap_secret.metadata.name.clone(), - &bootstrap_rs_labels, + // create bootstrap replica set + match kub_controller.create_bootstrap_validator_replica_set( + bootstrap_validator.image(), + bootstrap_validator.secret().metadata.name.clone(), + bootstrap_validator.labels(), ) { - Ok(replica_set) => replica_set, + Ok(replica_set) => bootstrap_validator.set_replica_set(replica_set), Err(err) => { error!("Error creating bootstrap validator replicas_set: {}", err); return; diff --git a/src/validator.rs b/src/validator.rs new file mode 100644 index 0000000..c643a10 --- /dev/null +++ b/src/validator.rs @@ -0,0 +1,61 @@ +use { + crate::{docker::DockerImage, ValidatorType}, + k8s_openapi::api::{apps::v1::ReplicaSet, core::v1::Secret}, + std::{collections::BTreeMap, string::String}, +}; + +pub struct Validator { + validator_type: ValidatorType, + image: DockerImage, + secret: Secret, + replica_set_labels: BTreeMap, + replica_set: ReplicaSet, +} + +impl Validator { + pub fn new(image: DockerImage) -> Self { + Self { + validator_type: image.validator_type(), + image, + secret: Secret::default(), + replica_set_labels: BTreeMap::new(), + replica_set: ReplicaSet::default(), + } + } + + pub fn image(&self) -> &DockerImage { + &self.image + } + + pub fn secret(&self) -> &Secret { + &self.secret + } + + pub fn validator_type(&self) -> &ValidatorType { + &self.validator_type + } + + pub fn add_label(&mut self, key: K, value: V) + where + K: Into, + V: Into, + { + self.replica_set_labels.insert(key.into(), value.into()); + } + + pub fn labels(&self) -> &BTreeMap { + &self.replica_set_labels + } + + pub fn set_secret(&mut self, secret: Secret) { + self.secret = secret; + } + + pub fn set_replica_set(&mut self, replica_set: ReplicaSet) { + self.replica_set = replica_set; + } + + pub fn replica_set(&self) -> &ReplicaSet { + &self.replica_set + } +}