From 74070b7b7df98a84e4d4739cea0678a420d159bc Mon Sep 17 00:00:00 2001 From: Jacob Morgan Date: Sun, 14 Apr 2024 17:06:04 +0200 Subject: [PATCH 1/5] Add latest version display for workloads and implement next_run function for scheduler --- assets/style.css | 2 +- src/services/scheduler.rs | 23 +++++++++++++++++++++++ src/site/app.rs | 5 +++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/assets/style.css b/assets/style.css index cf37406..5d2967b 100644 --- a/assets/style.css +++ b/assets/style.css @@ -67,6 +67,6 @@ order: 1!important; } -.workload-version, .workload-image, .workload-namespace, .workload-last-scanned { +.workload-version, .workload-image, .workload-namespace, .workload-last-scanned, .workload-latest-version { margin-top: 10px; } diff --git a/src/services/scheduler.rs b/src/services/scheduler.rs index 913481a..ba4eb21 100644 --- a/src/services/scheduler.rs +++ b/src/services/scheduler.rs @@ -6,6 +6,29 @@ use std::str::FromStr; use std::time::Duration; use tokio::time::{sleep_until, Instant as TokioInstant}; +#[cfg(feature = "server")] +pub async fn next_run(schedule: Schedule) { + // Example cron schedule: Every minute + //print next 5 scheduled times + let mut i = 0; + for datetime in schedule.upcoming(chrono::Utc) { + if i < 5 { + println!("Next scheduled time: {}", datetime); + i += 1; + } else { + break; + } + } + let now = chrono::Utc::now(); + if let Some(next) = schedule.upcoming(chrono::Utc).next() { + let duration_until_next = (next - now).to_std().expect("Failed to calculate duration"); + println!("Next scheduled time: {:?}", next); + // Convert std::time::Instant to tokio::time::Instant + refresh_all_workloads().await; + } +} + + #[cfg(feature = "server")] pub async fn scheduler(schedule: &Schedule) { // Example cron schedule: Every minute diff --git a/src/site/app.rs b/src/site/app.rs index ef7d239..266a7e8 100644 --- a/src/site/app.rs +++ b/src/site/app.rs @@ -29,8 +29,6 @@ async fn get_all_workloads() -> Result { use crate::database::client::return_all_workloads; let workloads = return_all_workloads(); Ok(workloads.unwrap().iter().map(|w| w.name.clone()).collect::>().join(", ")) - -// Ok(workloads.unwrap().iter().map(|w| w.name.clone()).collect::>().join(", ")) } @@ -91,6 +89,8 @@ fn WorkloadCard(props: WorkloadCardProps) -> Element { div { class: "workload-image", "Image: {props.workload.image}" }, div { class: "workload-last-scanned", "Last Scanned: {props.workload.last_scanned}" }, if props.workload.update_available == models::models::UpdateStatus::Available { + div { class: "workload-latest-version", "Latest Version Available: {props.workload.latest_version}" } + br {} button { onclick: move |_| { async move { if let Ok(_) = upgrade_workload(data()).await { @@ -170,6 +170,7 @@ fn All() -> Element { div { class: "workload-version", "Current Tag {w.current_version}" }, div { class: "workload-image", "Image: {w.image}" }, div { class: "workload-last-scanned", "Last Scanned: {w.last_scanned}" }, + div { class: "workload-name", "{w.latest_version}" }, if w.update_available == models::models::UpdateStatus::Available { div { class: "workload-update-available", "Update Available" } } From d279d7937ea38beed554ccf3ff017465c9c87f10 Mon Sep 17 00:00:00 2001 From: Jacob Morgan Date: Sun, 14 Apr 2024 17:22:30 +0200 Subject: [PATCH 2/5] Update GitHub Actions workflow for dev branch and improve Docker image build process --- .github/workflows/dev-build.yaml | 36 +++++++++++++++++++++++++++++ .github/workflows/slackwatch.yml | 3 +-- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/site/app.rs | 39 ++++++++++++++++++++------------ 5 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/dev-build.yaml diff --git a/.github/workflows/dev-build.yaml b/.github/workflows/dev-build.yaml new file mode 100644 index 0000000..e3a3daf --- /dev/null +++ b/.github/workflows/dev-build.yaml @@ -0,0 +1,36 @@ +name: Build and Push Slackwatch Image + +on: + push: + branches: + - dev + + paths: + - 'src/**' + - 'Dockerfile' + - 'assets/**' + +concurrency: + group: "slackwatch" + cancel-in-progress: true + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Check out the code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Log in to GitHub Container Registry + run: docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + push: true + tags: ghcr.io/slackspace-io/slackwatch:dev,ghcr.io/slackspace-io/slackwatch:${{ github.sha }} diff --git a/.github/workflows/slackwatch.yml b/.github/workflows/slackwatch.yml index 22a953b..6b1fa2c 100644 --- a/.github/workflows/slackwatch.yml +++ b/.github/workflows/slackwatch.yml @@ -7,7 +7,6 @@ on: paths: - 'src/**' - - '.github/workflows/slackwatch.yml' - 'Dockerfile' - 'assets/**' @@ -34,4 +33,4 @@ jobs: context: ./ file: ./Dockerfile push: true - tags: ghcr.io/slackspace-io/slackwatch:dev,ghcr.io/slackspace-io/slackwatch:${{ github.sha }} + tags: ghcr.io/slackspace-io/slackwatch:preview,ghcr.io/slackspace-io/slackwatch:${{ github.sha }} diff --git a/Cargo.lock b/Cargo.lock index ce722ea..6392dba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,9 +121,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.81" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" [[package]] name = "anymap" diff --git a/Cargo.toml b/Cargo.toml index 4bd7926..bdfdeed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] serde = { version = "1.0.197", features = ["derive"] } dotenv = { version = "0.15.0"} -anyhow = { version = "1.0.81", features = ["std"] } +anyhow = { version = "1.0.82", features = ["std"] } env_logger = { version = "0.11.3" } tokio = { version = "1.37.0", features = ["full"], optional = true } warp = { version = "0.3.7", optional = true } diff --git a/src/site/app.rs b/src/site/app.rs index 266a7e8..ccd2b06 100644 --- a/src/site/app.rs +++ b/src/site/app.rs @@ -65,6 +65,31 @@ fn Home() -> Element { } } +#[component] +fn DebugWorkloadCard(props: WorkloadCardProps) -> Element { + rsx! { + div { + class: if props.workload.update_available == models::models::UpdateStatus::Available { + "workload-card-update-available" + } else { + "workload-card" + }, + div { class: "workload-name", "{props.workload.name}" }, + div { class: "workload- + namespace", "Namespace: {props.workload.namespace}" }, + div { class: "workload-version", "Current Tag {props.workload.current_version}" }, + div { class: "workload-image", "Image: {props.workload.image}" }, + div { class: "workload-last-scanned", "Last Scanned: {props.workload.last_scanned}" }, + if props.workload.update_available == models::models::UpdateStatus::Available { + div { class: "workload-latest-version", "Latest Version Available: {props.workload.latest_version}" } + br {} + } + } + } +} + + + #[component] fn WorkloadCard(props: WorkloadCardProps) -> Element { let data = use_signal(|| {props.workload.clone()}); @@ -106,20 +131,6 @@ fn WorkloadCard(props: WorkloadCardProps) -> Element { pub fn App() -> Element { println!("App started"); rsx! { Router:: {} } - //rsx!{ - // div { - // "App" - // } - //} - //rsx! { All {} } -// rsx! { -// "server data is {workloads():?}" -// div {} -// "server data is {all():?}" -// div { {all().map(|w| rsx! { div {"{w:?}"}})}} -// -// -//} } From 1c46949d4fe9c42b64f06d5790c7228efac26786 Mon Sep 17 00:00:00 2001 From: Jacob Morgan Date: Sun, 14 Apr 2024 17:23:41 +0200 Subject: [PATCH 3/5] Add `/site/` directory to `.gitignore` for exclusion --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index bde1a51..d073835 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ docker-compose*.yml #mdbook book/ .site/ +/site/ From 9b0b117495d33568816982674523c284457e5764 Mon Sep 17 00:00:00 2001 From: Jacob Morgan Date: Sun, 14 Apr 2024 17:24:55 +0200 Subject: [PATCH 4/5] Refactor scheduler service by removing redundant next_run function --- src/services/scheduler.rs | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/services/scheduler.rs b/src/services/scheduler.rs index ba4eb21..662b696 100644 --- a/src/services/scheduler.rs +++ b/src/services/scheduler.rs @@ -6,39 +6,13 @@ use std::str::FromStr; use std::time::Duration; use tokio::time::{sleep_until, Instant as TokioInstant}; -#[cfg(feature = "server")] -pub async fn next_run(schedule: Schedule) { - // Example cron schedule: Every minute - //print next 5 scheduled times - let mut i = 0; - for datetime in schedule.upcoming(chrono::Utc) { - if i < 5 { - println!("Next scheduled time: {}", datetime); - i += 1; - } else { - break; - } - } - let now = chrono::Utc::now(); - if let Some(next) = schedule.upcoming(chrono::Utc).next() { - let duration_until_next = (next - now).to_std().expect("Failed to calculate duration"); - println!("Next scheduled time: {:?}", next); - // Convert std::time::Instant to tokio::time::Instant - refresh_all_workloads().await; - } -} #[cfg(feature = "server")] pub async fn scheduler(schedule: &Schedule) { - // Example cron schedule: Every minute log::info!("Scheduler started"); - //let schedule_str = "0 * * * * *"; // Adjust the cron expression as needed - //let schedule = Schedule::from_str(schedule_str).expect("Failed to parse cron expression"); println!("Cron schedule: {}", schedule); log::info!("Cron schedule: {}", schedule); - // Find the next scheduled time - //print next 5 scheduled times let mut i = 0; for datetime in schedule.upcoming(chrono::Utc) { if i < 5 { From 705b457296826dc4689659dcbcf02c8fdf5bdeb2 Mon Sep 17 00:00:00 2001 From: Jacob Morgan Date: Sun, 14 Apr 2024 18:56:31 +0200 Subject: [PATCH 5/5] Set max_tags to 1500 to ensure all tags return. Handle multiple newer versions to ensure only latest version is returned. Ensure include and exclude filters can be applied. --- src/repocheck/repocheck.rs | 3 ++- src/services/workloads.rs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/repocheck/repocheck.rs b/src/repocheck/repocheck.rs index 1b257cc..d8d7061 100644 --- a/src/repocheck/repocheck.rs +++ b/src/repocheck/repocheck.rs @@ -26,9 +26,10 @@ pub async fn get_tags_for_image(image: &str) -> Result, Box Result Result current_version { // tag_version is greater than current_version // Do something with this tag - println!("Tag {} is newer than current version", tag); - latest_version = tag.clone(); + if latest_version.is_empty() { + log::info!("latest_version is empty - setting to tag {}", tag); + latest_version = tag.clone(); + } else if tag_version > Version::parse(&strip_tag_lettings(&latest_version)).unwrap() { + log::info!("Tag {} is newer than {} current latest_version updating", tag, latest_version); + latest_version = tag.clone(); + } + } } else { // Handle the case where the tag is not a valid SemVer format