Implement override checking to make sure we've got the data we need in the layers_dir
This commit is contained in:
parent
42ad960b0c
commit
46c3e47c40
38
src/main.rs
38
src/main.rs
|
@ -53,10 +53,18 @@ fn load_settings() -> config::Config {
|
|||
#[derive(Clone, Debug)]
|
||||
struct AppState {
|
||||
conf: config::Config,
|
||||
upstream: String,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
/**
|
||||
* Helper f unction for fishing out the upstream registry URL
|
||||
*/
|
||||
fn upstream(&self) -> String {
|
||||
self.conf.get_str("registry")
|
||||
.expect("`registry` not properly configured, must be a string")
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function returns a true if the configured `layers_dir` has an override
|
||||
* for the given triplet of org/image:digest
|
||||
|
@ -64,15 +72,14 @@ impl AppState {
|
|||
* For example, if we have a `<layers_dir>/library/alpine/latest/` directory
|
||||
* with `*.tar.gz` files within it, then the function would return true.
|
||||
*/
|
||||
fn override_exists(&self, org: String, image: String, digest: String) -> bool {
|
||||
fn override_exists(&self, org: &str, image: &str, digest: &str) -> bool {
|
||||
let layers_dir = self.conf.get_str("layers_dir")
|
||||
.expect("Unable to access `layers_dir` conf variable");
|
||||
|
||||
info!("Looking in directory: {}", layers_dir);
|
||||
let layers_dir = Path::new(&layers_dir);
|
||||
let path = Path::new(&layers_dir).join(org).join(image).join(digest);
|
||||
|
||||
|
||||
return false;
|
||||
path.exists()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,7 +88,7 @@ impl AppState {
|
|||
* back to the client request it.
|
||||
*/
|
||||
async fn proxy_upstream(req: Request<AppState>) -> Result<Response, tide::Error> {
|
||||
let full_url = format!("{}{}", req.state().upstream, req.url().path());
|
||||
let full_url = format!("{}{}", req.state().upstream(), req.url().path());
|
||||
info!("Proxying request upstream to {}", full_url);
|
||||
/*
|
||||
* We need to send the Authorization header along as well, otherwise
|
||||
|
@ -137,7 +144,8 @@ async fn fetch_digest(req: Request<AppState>) -> Result<Response, tide::Error> {
|
|||
let org: String = req.param("org").unwrap_or("".to_string());
|
||||
let image: String = req.param("image").unwrap_or("".to_string());
|
||||
let digest: String = req.param("digest").unwrap_or("".to_string());
|
||||
if req.state().override_exists(org, image, digest) {
|
||||
|
||||
if req.state().override_exists(&org, &image, &digest) {
|
||||
error!("We should not proxy");
|
||||
Ok(Response::new(200))
|
||||
}
|
||||
|
@ -186,7 +194,6 @@ fn main() -> Result<(), std::io::Error> {
|
|||
|
||||
let state = AppState {
|
||||
conf: conf,
|
||||
upstream: upstream_url,
|
||||
};
|
||||
|
||||
task::block_on(async {
|
||||
|
@ -231,4 +238,19 @@ mod tests {
|
|||
let result = checksum_for(&path);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn override_exists_false() {
|
||||
let conf = load_settings();
|
||||
let state = AppState { conf };
|
||||
assert_eq!(false, state.override_exists("jenkins", "jenkins", "latest"));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn override_exists_true() {
|
||||
let conf = load_settings();
|
||||
let state = AppState { conf };
|
||||
assert!(state.override_exists("library", "alpine", "latest"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue