Skip to content

Commit

Permalink
feat: Refactoring, added e2e tests, change os image to lts, added new…
Browse files Browse the repository at this point in the history
…er microk8s versions
  • Loading branch information
dsieradzki committed Oct 9, 2023
1 parent 2588eb5 commit 54cdc99
Show file tree
Hide file tree
Showing 262 changed files with 2,823 additions and 1,689 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
Cargo.lock
**/*.rs.bk
**/*.iml

.vscode
makoon.json
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[workspace]
resolver = "2"

members = [
"proxmox",
"makoon"
"core",
"web",
"proxmox-client",
"helm-client",
"ssh-client"
]
17 changes: 12 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM docker.io/rust:1.71.1 as build
FROM docker.io/rust:1.73.0 as build
# Install and configure NODE using NVM
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 18.17.1
ENV NODE_VERSION 18.18.0
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
Expand All @@ -16,17 +16,24 @@ RUN npm install -g pnpm
RUN mkdir /build
WORKDIR /build
COPY . .
RUN cd makoon/src-web; \
RUN cd web/src-web; \
pnpm install; \
pnpm build;

RUN cargo test
RUN cargo build --release

FROM debian:bullseye-slim
FROM debian:bookworm-slim
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install libssl3 -y && \
apt-get clean

RUN mkdir /app
WORKDIR /app
COPY --from=build /build/target/release/makoon .
ENV RUST_LOG="info"
ENV MAKOON_DB_PATH="/app/data/makoon.db"
ENV MAKOON_SERVER_PORT=8080
VOLUME /app/data
CMD ["/app/makoon"]
CMD ["/app/makoon"]
23 changes: 23 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pipeline {
agent {
docker { image 'rust:1.73.0-bullseye' }
}
stages {
stage('Checkout') {
steps {
sh 'rm -rf makoon'
sh 'git clone -b integration_tests --depth 1 https://github.com/dsieradzki/makoon'
}
}
stage('Test') {
steps {
dir('makoon') {
withCredentials([usernamePassword(credentialsId: 'proxmox', usernameVariable: 'PROXMOX_USER', passwordVariable: 'PROXMOX_PASSWORD')]) {
sh 'mkdir web/src-web/dist/ -p && touch web/src-web/dist/dummy'
sh 'cargo test --features e2e -- --nocapture'
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="./makoon/src-web/src/assets/images/makonn_logo.svg" width="200" height="200">
<img src="./web/src-web/src/assets/images/makonn_logo.svg" width="200" height="200">
</p>

## Table of contents
Expand Down
21 changes: 21 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "core"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proxmox-client = { path = "../proxmox-client" }
helm-client = { path = "../helm-client" }
ssh-client = { path = "../ssh-client" }
openssl = "0.10"
pem = "3.0"
ssh-keys = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
typeshare = "1.0"
chrono = { version = "0.4", features = ["serde"] }
log = "0.4"
env_logger = "0.10"
uuid = { version = "1.4", features = ["v4", "fast-rng"] }
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::sync::Arc;
use log::info;
use crate::dispatcher::usecase;
use crate::event::Event;
use crate::model::{ClusterStatus, LogEntry};
use crate::Repository;

use crate::operator::dispatcher::usecase;
use crate::operator::event::Event;
use crate::operator::model::{ClusterStatus, LogEntry};
use crate::operator::repository::Repository;

use super::usecase::change_resources;

pub struct Dispatcher {
proxmox_client: Arc<proxmox::Client>,
proxmox_client: Arc<proxmox_client::Client>,
repo: Arc<Repository>,
}

impl Dispatcher {
pub fn new(proxmox_client: Arc<proxmox::Client>, repo: Arc<Repository>) -> Self {
pub fn new(proxmox_client: Arc<proxmox_client::Client>, repo: Arc<Repository>) -> Self {
Dispatcher {
proxmox_client,
repo,
Expand Down Expand Up @@ -200,7 +201,6 @@ impl Dispatcher {
ClusterStatus::Sync,
)?;


let mut cluster = self
.repo
.get_cluster(&cluster_name)?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub use dispatcher::*;

mod dispatcher;
mod utils;
mod usecase;
pub use usecase::HELM_CMD;
pub use usecase::install_helm_app;
mod utils;
pub use usecase::install_cluster_resource;
pub use usecase::install_helm_app;
pub use usecase::HELM_CMD;
143 changes: 143 additions & 0 deletions core/src/dispatcher/usecase/add_node_to_cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use std::collections::HashMap;
use std::sync::Arc;
use log::info;

use proxmox_client::model::AccessData;
use proxmox_client::Client;
use crate::dispatcher::usecase::common;
use crate::model::{Cluster, ClusterNode, ClusterNodeType, LogEntry};
use crate::Repository;


pub(crate) fn execute(
proxmox_client: Arc<Client>,
repo: Arc<Repository>,
access: AccessData,
cluster_name: String,
node_name: String,
) -> Result<(), String> {
info!("Request to add node to the cluster has been received");
let proxmox_client = proxmox_client.operations(access);

repo.save_log(LogEntry::info(
&cluster_name,
"Start creating node".to_string(),
))?;

let cluster = repo
.get_cluster(&cluster_name)?
.ok_or("Cannot find cluster")?;
let master_node = cluster
.nodes
.iter()
.find(|i| i.node_type == ClusterNodeType::Master && i.name != node_name)
.cloned()
.ok_or("Cannot find any master node".to_string())?;

let exising_cluster_hosts = cluster
.nodes
.iter()
.filter(|i| i.name != node_name)
.map(|i| {
(
format!("{}-{}", cluster.cluster_name, i.name),
i.ip_address.clone(),
)
})
.collect::<HashMap<String, String>>();

let existing_nodes = common::vm::get_existing_vms(&proxmox_client, &cluster)?
.into_iter()
.filter(|i| i.name != node_name)
.collect();

let node_to_add = cluster
.nodes
.iter()
.find(|i| i.name == node_name)
.ok_or("Cannot find node to create")?;

common::vm::create(&proxmox_client, repo.clone(), &cluster, node_to_add)?;

proxmox_client
.start_vm(&cluster.node, node_to_add.vm_id)
.map_err(|e| format!("Cannot start VM [{}]: {}", node_to_add.vm_id, e))?;
repo.save_log(LogEntry::info(
&cluster.cluster_name,
format!("Starting VM [{}]", node_to_add.vm_id),
))?;

common::vm::wait_for_start(&proxmox_client, &cluster, &node_to_add)
.map_err(|e| format!("Cannot start VM [{}]: {}", node_to_add.vm_id, e))?;
repo.save_log(LogEntry::info(
&cluster.cluster_name,
format!("VM [{}] has been started", node_to_add.vm_id),
))?;

common::vm::restart_vm_if_necessary(&proxmox_client, repo.clone(), &cluster, node_to_add)?;

setup_vm(repo.clone(), &cluster, node_to_add, exising_cluster_hosts)?;

add_new_node_host_to_existing_cluster(repo.clone(), &cluster, existing_nodes)?;

common::cluster::install_kubernetes(repo.clone(), &cluster, node_to_add)?;

common::cluster::wait_for_ready_kubernetes(repo.clone(), &cluster, node_to_add)?;

common::cluster::join_node_to_cluster(repo.clone(), &cluster, &master_node, node_to_add)?;

Ok(())
}

fn add_new_node_host_to_existing_cluster(
repo: Arc<Repository>,
cluster: &Cluster,
existing_nodes: Vec<ClusterNode>,
) -> Result<(), String> {
let hosts = cluster
.nodes
.iter()
.map(|i| {
(
format!("{}-{}", cluster.cluster_name, i.name),
i.ip_address.clone(),
)
})
.collect::<HashMap<String, String>>();

for node in existing_nodes.iter() {
repo.save_log(LogEntry::info(
&cluster.cluster_name,
format!("Add new node hostname to exising VM [{}]", node.vm_id),
))?;
common::vm::setup_vm(cluster, node, &hosts)?;
}
Ok(())
}

fn setup_vm(
repo: Arc<Repository>,
cluster: &Cluster,
node: &ClusterNode,
current_cluster_hosts: HashMap<String, String>,
) -> Result<(), String> {
let mut hosts = cluster
.nodes
.iter()
.map(|i| {
(
format!("{}-{}", cluster.cluster_name, i.name),
i.ip_address.clone(),
)
})
.collect::<HashMap<String, String>>();

hosts.extend(current_cluster_hosts);

repo.save_log(LogEntry::info(
&cluster.cluster_name,
format!("Configure VM [{}]", node.vm_id),
))?;
common::vm::setup_vm(cluster, node, &hosts)?;
Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::sync::Arc;
use log::info;

use proxmox::{
use proxmox_client::{
model::{AccessData, VmConfig},
Client,
};
use crate::dispatcher::usecase::common;
use crate::model::LogEntry;
use crate::Repository;

use crate::operator::{model::LogEntry, Repository};
use crate::operator::dispatcher::usecase::common;

pub(crate) fn execute(
proxmox_client: Arc<Client>,
Expand Down
Loading

0 comments on commit 54cdc99

Please sign in to comment.