Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract cdviz-db as a standalone module #45

Merged
merged 14 commits into from
Mar 24, 2024
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
26 changes: 10 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ permissions:
contents: read

jobs:
tests:
runs-on: ${{ matrix.os }}
ci:
runs-on: "ubuntu-latest"
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
folder:
- cdviz-collector
# - cdviz-db
- charts
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: full
Expand All @@ -23,16 +26,7 @@ jobs:
- uses: jdx/mise-action@v2
with:
experimental: true
- name: Cache cargo registry
uses: actions/cache@v4
continue-on-error: false
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- run: just check
- run: just lint_rust # megalinter is running via an other workflow
- run: just test
- run: mise install
working-directory: ${{ matrix.folder }}
- run: task ci
working-directory: ${{ matrix.folder }}

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
"cdviz",
"sdlc"
],
"rust-analyzer.linkedProjects": [
"./cdviz-collector/Cargo.toml"
]
}
43 changes: 0 additions & 43 deletions Cargo.toml

This file was deleted.

File renamed without changes.
15 changes: 15 additions & 0 deletions cdviz-collector/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[env]
# RUSTC_WRAPPER = "sccache"
RUST_WITHOUT = "rust-docs"
# ASDF_RUST_PROFILE = "minimal"
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = "http://127.0.0.1:4317"
# OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "grpc"
OTEL_TRACES_SAMPLER = "always_on"

[tools]
task = '*' # to have a set of simple commands for repetive task (and CI)
rust = '1.77.0' # the rust tool stack (with cargo, fmt, clippy) to build source
cargo-binstall = 'latest'

[plugins]
cargo-binstall = "https://github.com/davidB/asdf-cargo-binstall"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 34 additions & 22 deletions cdviz-collector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
[package]
name = "cdviz-collector"
description = "The service to collect cdevents"
readme = "README.md"
keywords = ["cd"]
categories = []
edition.workspace = true
version.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
publish.workspace = true
edition = "2021"
version = "0.1.0"
authors = ["David Bernard"]
license = "AGPL-3.0-or-later"
repository = "https://github.com/davidB/cdviz"
rust-version = "1.77"
publish = false

[dependencies]
axum = { version = "0.7", optional = true }
axum-tracing-opentelemetry = { version = "0.18", optional = true }
cdevents-sdk = { git = "https://github.com/cdevents/sdk-rust" }
clap = { workspace = true }
clap-verbosity-flag = "2.2.0"
# cloudevents-sdk = { version = "0.7", features = ["axum"] } // not compatible with axum 0.7
chrono = "0.4"
clap = { version = "4", features = ["derive", "env"] }
clap-verbosity-flag = "2.1.2"
enum_dispatch = "0.3"
figment = { version = "0.10", features = ["toml", "env"] }
futures = "0.3"
humantime-serde = "1.1.1"
init-tracing-opentelemetry = { workspace = true }
init-tracing-opentelemetry = { version = "0.18", features = [
"otlp",
"tracing_subscriber_ext",
"logfmt",
] }
opendal = { version = "0.45", default-features = false, features = [
"rustls",
"services-fs",
Expand All @@ -33,27 +36,30 @@ opendal = { version = "0.45", default-features = false, features = [
reqwest = "0.11"
reqwest-middleware = "0.2"
reqwest-tracing = "0.4"
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = "3.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_with = "3.6"
sqlx = { version = "0.7", features = [
"postgres",
"runtime-tokio",
"tls-rustls",
"macros",
"time",
"json",
"migrate", # for test but simpler to declare once
], default-features = false, optional = true }
time = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-opentelemetry-instrumentation-sdk = { workspace = true }
thiserror = "1"
time = "0.3"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-opentelemetry-instrumentation-sdk = { version = "0.18" }

[dev-dependencies]
axum-test = "14"
rstest = { workspace = true }
# axum-test = "14"
cdevents-sdk = { git = "https://github.com/cdevents/sdk-rust", features = [
"testkit",
] }
proptest = "1"
rstest = "0.18"
rustainers = "0.11"
tracing-subscriber = "0.3"

Expand All @@ -62,3 +68,9 @@ default = ["source_http", "source_opendal", "sink_db"]
sink_db = ["dep:sqlx"]
source_http = ["dep:axum", "dep:axum-tracing-opentelemetry"]
source_opendal = ["dep:opendal"]

[package.metadata.release]
pre-release-commit-message = "🚀 (cargo-release) version {{version}}"
tag-prefix = ""
tag-name = "{{prefix}}{{version}}"
tag-message = "🔖 {{version}}"
22 changes: 1 addition & 21 deletions Dockerfile → cdviz-collector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# see https://edu.chainguard.dev/chainguard/chainguard-images/reference/rust/image_specs/
# checkov:skip=CKV_DOCKER_7:Ensure the base image uses a non latest version tag
# trivy:ignore:AVD-DS-0001
FROM cgr.dev/chainguard/rust as build
FROM cgr.dev/chainguard/rust:1.77.0 as build
ARG PROFILE=release
USER nonroot
WORKDIR /work
Expand All @@ -28,23 +28,3 @@ ENV \
OTEL_TRACES_SAMPLER="always_off"
HEALTHCHECK NONE
CMD ["cdviz-collector"]

#---------------------------------------------------------------------------------------------------
# checkov:skip=CKV_DOCKER_7:Ensure the base image uses a non latest version tag
# trivy:ignore:AVD-DS-0001
FROM cgr.dev/chainguard/rust as build-sqlx
USER nonroot
RUN cargo install sqlx-cli --no-default-features --features rustls,postgres
HEALTHCHECK NONE

#---------------------------------------------------------------------------------------------------
# checkov:skip=CKV_DOCKER_7:Ensure the base image uses a non latest version tag
# trivy:ignore:AVD-DS-0001
FROM cgr.dev/chainguard/glibc-dynamic AS cdviz-dbmigration
LABEL org.opencontainers.image.source="https://github.com/davidB/cdviz"
LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0"
USER nonroot
COPY --from=build-sqlx /home/nonroot/.cargo/bin/sqlx /usr/local/bin/sqlx
COPY migrations /migrations
HEALTHCHECK NONE
ENTRYPOINT ["/usr/local/bin/sqlx"]
13 changes: 12 additions & 1 deletion cdviz-collector/cdviz-collector.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
[sinks.debug]
type = "debug"

[sinks.cdviz_db]
type = "postgresql"
url = "postgresql://postgres:passwd@localhost:5432/cdviz"
pool_connections_min = 1
pool_connections_max = 10

[sources.cdevents_examples]
parameters = { root = "../cdevents-spec/examples" }
type = "opendal"
kind = "fs"
polling_interval = "10s"
parameters = { root = "../cdevents-spec/examples" }

[sources.cdevents_webhook]
type = "http"
host = "0.0.0.0"
port = 8080
2 changes: 2 additions & 0 deletions cdviz-collector/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.77.0"
21 changes: 18 additions & 3 deletions cdviz-collector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use figment::{
};
use futures::future::TryJoinAll;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
// use time::OffsetDateTime;
use tokio::sync::broadcast;

#[derive(Debug, Clone, clap::Parser)]
Expand All @@ -40,15 +40,15 @@ type Receiver<T> = tokio::sync::broadcast::Receiver<T>;

#[derive(Clone, Debug)]
struct Message {
received_at: OffsetDateTime,
// received_at: OffsetDateTime,
cdevent: CDEvent,
//raw: serde_json::Value,
}

impl From<CDEvent> for Message {
fn from(value: CDEvent) -> Self {
Self {
received_at: OffsetDateTime::now_utc(),
// received_at: OffsetDateTime::now_utc(),
cdevent: value,
}
}
Expand Down Expand Up @@ -123,3 +123,18 @@ async fn main() -> Result<()> {
//futures::try_join!(handlers);
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

impl proptest::arbitrary::Arbitrary for Message {
type Parameters = ();
type Strategy = proptest::strategy::BoxedStrategy<Self>;

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
(any::<CDEvent>()).prop_map(Message::from).boxed()
}
}
}
Loading