-
Notifications
You must be signed in to change notification settings - Fork 3
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
12 changed files
with
282 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
use std::collections::HashMap; | ||
|
||
use serde::Deserialize; | ||
use snafu::{OptionExt, ResultExt, Snafu}; | ||
use tracing::debug; | ||
use urlencoding::encode; | ||
|
||
use crate::{ | ||
constants::{HELM_OCI_BASE, HELM_REPO_NAME_DEV, HELM_REPO_NAME_STABLE, HELM_REPO_NAME_TEST}, | ||
utils::chartsource::{ChartSourceEntry, ChartSourceMetadata}, | ||
}; | ||
|
||
#[derive(Debug, Snafu)] | ||
pub enum Error { | ||
#[snafu(display("cannot get repositories"))] | ||
GetRepositories { source: reqwest::Error }, | ||
|
||
#[snafu(display("cannot parse repositories"))] | ||
ParseRepositories { source: reqwest::Error }, | ||
|
||
#[snafu(display("cannot get artifacts"))] | ||
GetArtifacts { source: reqwest::Error }, | ||
|
||
#[snafu(display("cannot parse artifacts"))] | ||
ParseArtifacts { source: reqwest::Error }, | ||
|
||
#[snafu(display("unexpected OCI repository name"))] | ||
UnexpectedOciRepositoryName, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct OciRepository { | ||
pub name: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Tag { | ||
pub name: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Artifact { | ||
pub digest: String, | ||
pub tags: Option<Vec<Tag>>, | ||
} | ||
|
||
pub async fn get_oci_index<'a>() -> Result<HashMap<&'a str, ChartSourceMetadata>, Error> { | ||
let mut source_index_files: HashMap<&str, ChartSourceMetadata> = HashMap::new(); | ||
|
||
// initialize map | ||
for repo_name in [ | ||
HELM_REPO_NAME_STABLE, | ||
HELM_REPO_NAME_TEST, | ||
HELM_REPO_NAME_DEV, | ||
] { | ||
source_index_files.insert( | ||
repo_name, | ||
ChartSourceMetadata { | ||
entries: HashMap::new(), | ||
}, | ||
); | ||
} | ||
let base_url = format!("https://{}/api/v2.0", HELM_OCI_BASE); | ||
|
||
// fetch all operators | ||
let url = format!( | ||
"{}/repositories?page_size={}&q=name=~sdp-charts/", | ||
base_url, 100 | ||
); | ||
|
||
// reuse connections | ||
let client = reqwest::Client::new(); | ||
|
||
let repositories: Vec<OciRepository> = client | ||
.get(&url) | ||
.send() | ||
.await | ||
.context(GetRepositoriesSnafu)? | ||
.json() | ||
.await | ||
.context(ParseRepositoriesSnafu)?; | ||
|
||
debug!("OCI repos {:?}", repositories); | ||
|
||
for repository in &repositories { | ||
// fetch all artifacts pro operator | ||
let (project_name, repository_name) = repository | ||
.name | ||
.split_once('/') | ||
.context(UnexpectedOciRepositoryNameSnafu)?; | ||
|
||
debug!("OCI repo parts {} and {}", project_name, repository_name); | ||
|
||
let mut artifacts = Vec::new(); | ||
let mut page = 1; | ||
let page_size = 20; | ||
|
||
while let Ok(artifacts_page) = client | ||
.get(format!( | ||
"{}/projects/{}/repositories/{}/artifacts?page_size={}&page={}", | ||
base_url, | ||
encode(project_name), | ||
encode(repository_name), | ||
page_size, | ||
page | ||
)) | ||
.send() | ||
.await | ||
.context(GetArtifactsSnafu)? | ||
.json::<Vec<Artifact>>() | ||
.await | ||
.context(ParseArtifactsSnafu) | ||
{ | ||
let count = artifacts_page.len(); | ||
artifacts.extend(artifacts_page); | ||
if count < page_size { | ||
break; | ||
} | ||
page += 1; | ||
} | ||
|
||
for artifact in &artifacts { | ||
if let Some(release_artifact) = | ||
artifact.tags.as_ref().and_then(|tags| tags.iter().next()) | ||
{ | ||
let release_version = release_artifact | ||
.name | ||
.replace("-arm64", "") | ||
.replace("-amd64", ""); | ||
|
||
debug!( | ||
"OCI resolved artifact {}, {}, {}", | ||
release_version.to_string(), | ||
repository_name.to_string(), | ||
release_artifact.name.to_string() | ||
); | ||
|
||
let entry = ChartSourceEntry { | ||
name: repository_name.to_string(), | ||
version: release_version.to_string(), | ||
}; | ||
|
||
match release_version.as_str() { | ||
"0.0.0-dev" => { | ||
if let Some(repo) = source_index_files.get_mut(HELM_REPO_NAME_DEV) { | ||
repo.entries | ||
.entry(repository_name.to_string()) | ||
.or_default() | ||
.push(entry) | ||
} | ||
} | ||
version if version.contains("-pr") => { | ||
if let Some(repo) = source_index_files.get_mut(HELM_REPO_NAME_TEST) { | ||
repo.entries | ||
.entry(repository_name.to_string()) | ||
.or_default() | ||
.push(entry) | ||
} | ||
} | ||
_ => { | ||
if let Some(repo) = source_index_files.get_mut(HELM_REPO_NAME_STABLE) { | ||
repo.entries | ||
.entry(repository_name.to_string()) | ||
.or_default() | ||
.push(entry) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Ok(source_index_files) | ||
} |
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,14 @@ | ||
use std::collections::HashMap; | ||
|
||
use serde::Deserialize; | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct ChartSourceMetadata { | ||
pub entries: HashMap<String, Vec<ChartSourceEntry>>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct ChartSourceEntry { | ||
pub name: String, | ||
pub version: String, | ||
} |
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 @@ | ||
pub mod chartsource; | ||
pub mod check; | ||
pub mod k8s; | ||
pub mod params; | ||
|
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.