|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use serde::Deserialize; |
| 4 | +use snafu::{OptionExt, ResultExt, Snafu}; |
| 5 | +use tracing::debug; |
| 6 | +use urlencoding::encode; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + constants::{HELM_OCI_BASE, HELM_REPO_NAME_DEV, HELM_REPO_NAME_STABLE, HELM_REPO_NAME_TEST}, |
| 10 | + utils::chartsource::{ChartSourceEntry, ChartSourceMetadata}, |
| 11 | +}; |
| 12 | + |
| 13 | +#[derive(Debug, Snafu)] |
| 14 | +pub enum Error { |
| 15 | + #[snafu(display("cannot get repositories"))] |
| 16 | + GetRepositories { source: reqwest::Error }, |
| 17 | + |
| 18 | + #[snafu(display("cannot parse repositories"))] |
| 19 | + ParseRepositories { source: reqwest::Error }, |
| 20 | + |
| 21 | + #[snafu(display("cannot get artifacts"))] |
| 22 | + GetArtifacts { source: reqwest::Error }, |
| 23 | + |
| 24 | + #[snafu(display("cannot parse artifacts"))] |
| 25 | + ParseArtifacts { source: reqwest::Error }, |
| 26 | + |
| 27 | + #[snafu(display("unexpected OCI repository name"))] |
| 28 | + UnexpectedOciRepositoryName, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Deserialize, Debug)] |
| 32 | +pub struct OciRepository { |
| 33 | + pub name: String, |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Deserialize, Debug)] |
| 37 | +pub struct Tag { |
| 38 | + pub name: String, |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Deserialize, Debug)] |
| 42 | +pub struct Artifact { |
| 43 | + pub digest: String, |
| 44 | + pub tags: Option<Vec<Tag>>, |
| 45 | +} |
| 46 | + |
| 47 | +pub async fn get_oci_index<'a>() -> Result<HashMap<&'a str, ChartSourceMetadata>, Error> { |
| 48 | + let mut source_index_files: HashMap<&str, ChartSourceMetadata> = HashMap::new(); |
| 49 | + |
| 50 | + // initialize map |
| 51 | + for repo_name in [ |
| 52 | + HELM_REPO_NAME_STABLE, |
| 53 | + HELM_REPO_NAME_TEST, |
| 54 | + HELM_REPO_NAME_DEV, |
| 55 | + ] { |
| 56 | + source_index_files.insert( |
| 57 | + repo_name, |
| 58 | + ChartSourceMetadata { |
| 59 | + entries: HashMap::new(), |
| 60 | + }, |
| 61 | + ); |
| 62 | + } |
| 63 | + let base_url = format!("https://{}/api/v2.0", HELM_OCI_BASE); |
| 64 | + |
| 65 | + // fetch all operators |
| 66 | + let url = format!( |
| 67 | + "{}/repositories?page_size={}&q=name=~sdp-charts/", |
| 68 | + base_url, 100 |
| 69 | + ); |
| 70 | + |
| 71 | + // reuse connections |
| 72 | + let client = reqwest::Client::new(); |
| 73 | + |
| 74 | + let repositories: Vec<OciRepository> = client |
| 75 | + .get(&url) |
| 76 | + .send() |
| 77 | + .await |
| 78 | + .context(GetRepositoriesSnafu)? |
| 79 | + .json() |
| 80 | + .await |
| 81 | + .context(ParseRepositoriesSnafu)?; |
| 82 | + |
| 83 | + debug!("OCI repos {:?}", repositories); |
| 84 | + |
| 85 | + for repository in &repositories { |
| 86 | + // fetch all artifacts pro operator |
| 87 | + let (project_name, repository_name) = repository |
| 88 | + .name |
| 89 | + .split_once('/') |
| 90 | + .context(UnexpectedOciRepositoryNameSnafu)?; |
| 91 | + |
| 92 | + debug!("OCI repo parts {} and {}", project_name, repository_name); |
| 93 | + |
| 94 | + let mut artifacts = Vec::new(); |
| 95 | + let mut page = 1; |
| 96 | + let page_size = 20; |
| 97 | + |
| 98 | + while let Ok(artifacts_page) = client |
| 99 | + .get(format!( |
| 100 | + "{}/projects/{}/repositories/{}/artifacts?page_size={}&page={}", |
| 101 | + base_url, |
| 102 | + encode(project_name), |
| 103 | + encode(repository_name), |
| 104 | + page_size, |
| 105 | + page |
| 106 | + )) |
| 107 | + .send() |
| 108 | + .await |
| 109 | + .context(GetArtifactsSnafu)? |
| 110 | + .json::<Vec<Artifact>>() |
| 111 | + .await |
| 112 | + .context(ParseArtifactsSnafu) |
| 113 | + { |
| 114 | + let count = artifacts_page.len(); |
| 115 | + artifacts.extend(artifacts_page); |
| 116 | + if count < page_size { |
| 117 | + break; |
| 118 | + } |
| 119 | + page += 1; |
| 120 | + } |
| 121 | + |
| 122 | + for artifact in &artifacts { |
| 123 | + if let Some(release_artifact) = |
| 124 | + artifact.tags.as_ref().and_then(|tags| tags.iter().next()) |
| 125 | + { |
| 126 | + let release_version = release_artifact |
| 127 | + .name |
| 128 | + .replace("-arm64", "") |
| 129 | + .replace("-amd64", ""); |
| 130 | + |
| 131 | + debug!( |
| 132 | + "OCI resolved artifact {}, {}, {}", |
| 133 | + release_version.to_string(), |
| 134 | + repository_name.to_string(), |
| 135 | + release_artifact.name.to_string() |
| 136 | + ); |
| 137 | + |
| 138 | + let entry = ChartSourceEntry { |
| 139 | + name: repository_name.to_string(), |
| 140 | + version: release_version.to_string(), |
| 141 | + }; |
| 142 | + |
| 143 | + match release_version.as_str() { |
| 144 | + "0.0.0-dev" => { |
| 145 | + if let Some(repo) = source_index_files.get_mut(HELM_REPO_NAME_DEV) { |
| 146 | + repo.entries |
| 147 | + .entry(repository_name.to_string()) |
| 148 | + .or_default() |
| 149 | + .push(entry) |
| 150 | + } |
| 151 | + } |
| 152 | + version if version.contains("-pr") => { |
| 153 | + if let Some(repo) = source_index_files.get_mut(HELM_REPO_NAME_TEST) { |
| 154 | + repo.entries |
| 155 | + .entry(repository_name.to_string()) |
| 156 | + .or_default() |
| 157 | + .push(entry) |
| 158 | + } |
| 159 | + } |
| 160 | + _ => { |
| 161 | + if let Some(repo) = source_index_files.get_mut(HELM_REPO_NAME_STABLE) { |
| 162 | + repo.entries |
| 163 | + .entry(repository_name.to_string()) |
| 164 | + .or_default() |
| 165 | + .push(entry) |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + Ok(source_index_files) |
| 173 | +} |
0 commit comments