-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
271 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- "v*.*.*" | ||
|
||
jobs: | ||
release: | ||
name: Create Release | ||
runs-on: ubuntu-latest | ||
outputs: | ||
upload_url: ${{ steps.create_release.outputs.upload_url }} | ||
steps: | ||
- name: Get version | ||
id: get_version | ||
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} | ||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ steps.get_version.outputs.VERSION }} | ||
release_name: ${{ steps.get_version.outputs.VERSION }} | ||
draft: false | ||
prerelease: false | ||
|
||
crate-publish: | ||
name: Crate Publish | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
- name: Publish | ||
run: cargo publish --all-features --token ${{ secrets.CRATE_REGISTRY_TOKEN }} | ||
|
||
linux-release: | ||
name: Build on Linux | ||
needs: release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
- name: Build | ||
run: cargo build --release --all-features --bin clean-harbor-images | ||
- name: Get version | ||
id: get_version | ||
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} | ||
- name: Upload assets | ||
uses: actions/upload-release-asset@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
upload_url: ${{ needs.release.outputs.upload_url }} | ||
asset_path: target/release/clean-harbor-images | ||
asset_name: clean-harbor-images-${{ steps.get_version.outputs.VERSION }}-linux-amd64 | ||
asset_content_type: application/octet-stream | ||
|
||
macos-release: | ||
name: Build on MacOS | ||
needs: release | ||
runs-on: macos-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
- name: Build | ||
run: cargo build --release --all-features --bin clean-harbor-images | ||
- name: Get version | ||
id: get_version | ||
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} | ||
- name: Upload assets | ||
uses: actions/upload-release-asset@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
upload_url: ${{ needs.release.outputs.upload_url }} | ||
asset_path: target/release/clean-harbor-images | ||
asset_name: clean-harbor-images-${{ steps.get_version.outputs.VERSION }}-darwin-amd64 | ||
asset_content_type: application/octet-stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/target | ||
Cargo.lock | ||
.idea | ||
/config.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
[package] | ||
name = "harbor_rs" | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
edition = "2021" | ||
description = "Harbor SDK for Rust" | ||
license = "MIT" | ||
homepage = "https://github.com/k8scat/harbor_rs" | ||
authors = ["k8scat <[email protected]>"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
|
@@ -15,3 +17,9 @@ chrono = "0.4.19" | |
base64 = "0.13.0" | ||
serde_json = "1.0.73" | ||
serde = { version = "1.0.132", features = ["derive"] } | ||
clap = "2.34.0" | ||
serde_yaml = "0.8.23" | ||
|
||
[[bin]] | ||
name = "clean-harbor-images" | ||
path = "src/bin/clean-harbor-images.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version = 0.1.1 | ||
upgrade: | ||
sed -i "" 's/^version = "[0-9]*.[0-9]*.[0-9]*"/version = "${version}"/' Cargo.toml | ||
sed -i "" 's/harbor_rs = "[0-9]*.[0-9]*.[0-9]*"/harbor_rs = "${version}"/g' README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
# harbor_rs | ||
|
||
[Harbor](https://goharbor.io/) SDK for Rust | ||
|
||
```toml | ||
[dependencies] | ||
harbor_rs = "0.1.0" | ||
harbor_rs = "0.1.1" | ||
``` | ||
|
||
## clean-harbor-images | ||
|
||
根据时间间隔清理 [Harbor](https://goharbor.io/) 上的镜像 Tag | ||
|
||
```yaml | ||
harbor_base_api: "https://example.com/api" | ||
harbor_username: "admin" | ||
harbor_password": "admin" | ||
clean_interval": 20 # 清理 20 天前构建的镜像 | ||
repos: | ||
- "test/image1" | ||
- "test/image2" | ||
``` | ||
## LICENSE | ||
[MIT](https://github.com/k8scat/harbor_rs/blob/main/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::io::Read; | ||
use std::ops::Sub; | ||
use chrono::Duration; | ||
use chrono::prelude::*; | ||
use anyhow::{anyhow, Result}; | ||
use clap::{App, Arg}; | ||
use harbor_rs::Client; | ||
use serde::{Deserialize, Serialize}; | ||
use harbor_rs::harbor::common::parse_time; | ||
use harbor_rs::harbor::gc::{Schedule, ScheduleType}; | ||
|
||
extern crate base64; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
struct Config { | ||
pub harbor_base_api: String, | ||
pub harbor_username: String, | ||
pub harbor_password: String, | ||
pub clean_interval: u32, | ||
pub repos: Vec<String>, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let matches = App::new("clean-harbor-images") | ||
.version("1.0") | ||
.author("K8sCat <[email protected]>") | ||
.about("Clean harbor images") | ||
.arg(Arg::with_name("config") | ||
.short("c") | ||
.long("config") | ||
.value_name("FILE") | ||
.default_value("config.yml") | ||
.help("Set config file") | ||
.takes_value(true)) | ||
.get_matches(); | ||
let config_file = matches.value_of("config").unwrap(); | ||
let config = load_config(config_file).unwrap(); | ||
let client = harbor_rs::Client::new(config.harbor_base_api, config.harbor_username, config.harbor_password).unwrap(); | ||
let clean_interval = Local::now().sub(Duration::days(config.clean_interval as i64)); | ||
for repo in config.repos { | ||
clean(&client, repo.as_str(), clean_interval).await.unwrap(); | ||
} | ||
} | ||
|
||
fn load_config(path: &str) -> Result<Config> { | ||
let mut f = std::fs::File::open(path)?; | ||
let mut s = String::new(); | ||
f.read_to_string(&mut s)?; | ||
let config: Config = serde_yaml::from_str(&s)?; | ||
Ok(config) | ||
} | ||
|
||
async fn clean(client: &Client, repo: &str, interval: DateTime<Local>) -> Result<()> { | ||
let tags = client.list_tags(repo, None, Some(true)).await?; | ||
for tag in tags { | ||
let push_time = parse_time(tag.push_time)?; | ||
if push_time.le(&interval) { | ||
client.delete_tag(repo, tag.name.as_str()).await?; | ||
println!("deleted {} which pushed at {}", tag.name, push_time.format("%Y-%m-%d %H:%M:%S")); | ||
} | ||
} | ||
Ok(manual_gc(client).await?) | ||
} | ||
|
||
async fn manual_gc(client: &Client) -> Result<()> { | ||
let schedule = Schedule { | ||
schedule_type: ScheduleType::Manual.to_string(), | ||
cron: None, | ||
}; | ||
client.create_schedule(&schedule).await?; | ||
println!("manual gc schedule created"); | ||
|
||
for _ in 1..100 { | ||
let gc_results = client.list_gc_results().await?; | ||
if gc_results.len() == 0 { | ||
return Err(anyhow!("gc results is empty")); | ||
} | ||
let gc_result = gc_results.get(0).unwrap(); | ||
if gc_result.job_status == "finished" { | ||
println!("gc status: finished"); | ||
return Ok(()); | ||
} | ||
println!("gc status: {}", gc_result.job_status); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
Err(anyhow!("gc is still not finished, please check manually")) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
harbor_base_api: "https://example.com/api" | ||
harbor_username: "admin" | ||
harbor_password": "admin" | ||
clean_interval": 20 | ||
repos: | ||
- "test/image1" | ||
- "test/image2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.