Skip to content

Commit

Permalink
refactor. add validator struct to clean up main
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcusack committed Apr 2, 2024
1 parent b5d9dfb commit 7b43c3e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 42 deletions.
6 changes: 0 additions & 6 deletions src/k8s_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
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,
Expand Down
4 changes: 0 additions & 4 deletions src/kubernetes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,4 @@ impl<'a> Kubernetes<'a> {

flags
}

pub fn create_selector(&self, key: &str, value: &str) -> BTreeMap<String, String> {
k8s_helpers::create_selector(key, value)
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("👷 ", "");
Expand Down
57 changes: 25 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
genesis::{Genesis, GenesisFlags},
kubernetes::{Kubernetes, PodRequests},
release::{BuildConfig, BuildType, DeployMethod},
validator::Validator,
validator_config::ValidatorConfig,
SolanaRoot, ValidatorType,
},
Expand Down Expand Up @@ -429,33 +430,32 @@ 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);
return;
}
}

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);
Expand All @@ -464,48 +464,41 @@ 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);
return;
}
}

// 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;
Expand Down
61 changes: 61 additions & 0 deletions src/validator.rs
Original file line number Diff line number Diff line change
@@ -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<String, String>,
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<K, V>(&mut self, key: K, value: V)
where
K: Into<String>,
V: Into<String>,
{
self.replica_set_labels.insert(key.into(), value.into());
}

pub fn labels(&self) -> &BTreeMap<String, String> {
&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
}
}

0 comments on commit 7b43c3e

Please sign in to comment.