Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 124 additions & 88 deletions core/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "3"
members = [
"common",
"api",
"src/components/conntracker",
"src/components/identity",
Expand Down
1 change: 1 addition & 0 deletions core/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tonic = "0.14.0"
tonic-prost = "0.14.0"
tracing = "0.1.41"
aya = "0.13.1"
cortexbrain-common = { path = "../common" }
tonic-reflection = "0.14.0"
tonic-build = "0.14.0"
tracing-subscriber = "0.3.19"
Expand Down
1 change: 1 addition & 0 deletions core/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ WORKDIR /usr/src/app/agent

# Copy Cargo manifest and sources
COPY . .
COPY common ../common

# Fetch dependencies and build release
RUN cargo fetch
Expand Down
13 changes: 2 additions & 11 deletions core/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// module imports
use tonic::transport::{Error, Server};
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
use cortexbrain_common::logger;

mod agent;
mod api;
Expand All @@ -20,16 +20,7 @@ use tracing::{error, info};
#[main]
async fn main() -> Result<(), Error> {
//init tracing subscriber
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.with_level(true)
.with_span_events(FmtSpan::NONE)
.with_file(false)
.pretty()
.with_env_filter(EnvFilter::new("info"))
.with_line_number(false)
.init();
logger::init_default_logger();

info!("Starting agent server...");
info!("fetching data");
Expand Down
9 changes: 9 additions & 0 deletions core/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "cortexbrain-common"
version = "0.1.0"
edition = "2021"

[dependencies]
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
anyhow = "1.0"
7 changes: 7 additions & 0 deletions core/common/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Environment variable name for the BPF program file path.
/// Used by all components to load their eBPF programs.
pub const BPF_PATH: &str = "BPF_PATH";

/// Environment variable name for the BPF map pinning path.
/// Used for sharing maps between eBPF programs.
pub const PIN_MAP_PATH: &str = "PIN_MAP_PATH";
2 changes: 2 additions & 0 deletions core/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod constants;
pub mod logger;
37 changes: 37 additions & 0 deletions core/common/src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};

/// Initialize the default logger configuration used across CortexBrain components.
///
/// This configures tracing with:
/// - INFO level logging
/// - Pretty formatting
/// - No target, file, or line number information
/// - Environment-based filtering
pub fn init_default_logger() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.with_level(true)
.with_span_events(FmtSpan::NONE)
.with_file(false)
.pretty()
.with_env_filter(EnvFilter::new("info"))
.with_line_number(false)
.init();
}

/// Initialize logger without timestamp information.
/// Used by components that don't need timestamp logging.
pub fn init_logger_without_time() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.with_level(true)
.with_span_events(FmtSpan::NONE)
.with_file(false)
.without_time()
.pretty()
.with_env_filter(EnvFilter::new("info"))
.with_line_number(false)
.init();
}
1 change: 1 addition & 0 deletions core/src/components/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
libc = "0.2.172"
bytemuck = {version ="1.23.0",features = ["derive"]}
bytemuck_derive = "1.10.1"
cortexbrain-common = { path = "../../../common" }
nix = { version = "0.30.1", features = ["net"] }
kube = {version = "2.0.1",features = ["client"]}
k8s-openapi = {version ="0.26.0", features = ["v1_34"]}
Expand Down
1 change: 1 addition & 0 deletions core/src/components/identity/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WORKDIR /usr/src/app/identity-service
# Copy Cargo manifest and sources
COPY Cargo.toml .
COPY src ./src
COPY common ../../../common

# Fetch dependencies and build release
RUN cargo fetch && cargo build --release
Expand Down
2 changes: 2 additions & 0 deletions core/src/components/identity/build-identity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ popd

echo "Copying connection tracker binaries"
cp -r ../../../target/bpfel-unknown-none/release/conntracker conntracker
cp -r ../../../common common

# Run docker build
docker build -t identity:0.0.1 .

# Cleanup
echo "Cleaning building files"
rm -rf conntracker
rm -rf common
6 changes: 1 addition & 5 deletions core/src/components/identity/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ use std::{
},
};
use tracing::{error, info, warn};

/*
* decleare bpf path env variable
*/
const BPF_PATH: &str = "BPF_PATH";
use cortexbrain_common::constants;

/*
* TryFrom Trait implementation for IpProtocols enum
Expand Down
21 changes: 4 additions & 17 deletions core/src/components/identity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,14 @@ use std::{ convert::TryInto, path::Path, sync::{ Arc, Mutex, atomic::{ AtomicBoo
use anyhow::{ Context, Ok };
use tokio::{ fs, signal };
use tracing::{ error, info };
use tracing_subscriber::{ EnvFilter, fmt::format::FmtSpan };

const BPF_PATH: &str = "BPF_PATH"; //BPF env path
const PIN_MAP_PATH: &str = "PIN_MAP_PATH";
use cortexbrain_common::{constants, logger};

use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
//init tracing subscriber
tracing_subscriber
::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.with_level(true)
.with_span_events(FmtSpan::NONE)
.with_file(false)
.pretty()
.with_env_filter(EnvFilter::new("info"))
.with_line_number(false)
.init();
logger::init_default_logger();

info!("Starting identity service...");
info!("fetching data");
Expand All @@ -60,13 +47,13 @@ async fn main() -> Result<(), anyhow::Error> {
let link_ids = Arc::new(Mutex::new(HashMap::<String, SchedClassifierLinkId>::new()));

//init conntracker data path
let bpf_path = std::env::var(BPF_PATH).context("BPF_PATH environment variable required")?;
let bpf_path = std::env::var(constants::BPF_PATH).context("BPF_PATH environment variable required")?;
let data = fs::read(Path::new(&bpf_path)).await.context("failed to load file from path")?;

//init bpf data
let bpf = Arc::new(Mutex::new(Bpf::load(&data)?));
let bpf_map_save_path = std::env
::var(PIN_MAP_PATH)
::var(constants::PIN_MAP_PATH)
.context("PIN_MAP_PATH environment variable required")?;

match init_bpf_maps(bpf.clone()) {
Expand Down
1 change: 1 addition & 0 deletions core/src/components/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ tracing = "0.1.41"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
libc = "0.2.172"
bytemuck = "1.23.0"
cortexbrain-common = { path = "../../../common" }
nix ={version="0.30.1",features=["net"]}
1 change: 1 addition & 0 deletions core/src/components/metrics/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WORKDIR /usr/src/app/metrics
# Copy Cargo manifest and sources
COPY Cargo.toml .
COPY src ./src
COPY common ../../../common

# Fetch dependencies and build release
RUN cargo fetch && cargo build --release
Expand Down
2 changes: 2 additions & 0 deletions core/src/components/metrics/build-metrics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ popd

echo "Copying metrics_tracer binaries"
cp -r ../../../target/bpfel-unknown-none/release/metrics_tracer metrics_tracer
cp -r ../../../common common

# Run docker build
docker build -t metrics:0.0.1 .

# Cleanup
echo "Cleaning building files"
rm -rf metrics_tracer
rm -rf common
20 changes: 4 additions & 16 deletions core/src/components/metrics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use std::{

use anyhow::{Context, Ok};
use tracing::{error, info};
use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan};

const BPF_PATH: &str = "BPF_PATH"; //BPF env path
const PIN_MAP_PATH: &str = "PIN_MAP_PATH";
use cortexbrain_common::{constants, logger};

mod helpers;
use crate::{helpers::event_listener, maps_handlers::map_pinner, program_handlers::load_and_attach_tcp_programs};
Expand All @@ -31,21 +28,12 @@ mod structs;
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
//init tracing subscriber
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.with_level(true)
.with_span_events(FmtSpan::NONE)
.with_file(false)
.pretty()
.with_env_filter(EnvFilter::new("info"))
.with_line_number(false)
.init();
logger::init_default_logger();

info!("Starting metrics service...");
info!("fetching data");

let bpf_path = env::var(BPF_PATH).context("BPF_PATH environment variable required")?;
let bpf_path = env::var(constants::BPF_PATH).context("BPF_PATH environment variable required")?;
let data = fs::read(Path::new(&bpf_path)).context("Failed to load file from path")?;
let bpf = Arc::new(Mutex::new(Ebpf::load(&data)?));
let tcp_bpf = bpf.clone();
Expand All @@ -54,7 +42,7 @@ async fn main() -> Result<(), anyhow::Error> {
info!("Running Ebpf logger");
info!("loading programs");
let bpf_map_save_path =
std::env::var(PIN_MAP_PATH).context("PIN_MAP_PATH environment variable required")?;
std::env::var(constants::PIN_MAP_PATH).context("PIN_MAP_PATH environment variable required")?;

match init_ebpf_maps(bpf.clone()) {
std::result::Result::Ok(maps) => {
Expand Down