controller: implement enhanced image name format support

This commit is contained in:
Alex Zenla 2024-02-24 22:05:06 +00:00
parent 89e4f1a23d
commit 6511f2f0fa
No known key found for this signature in database
GPG Key ID: 067B238899B51269
5 changed files with 73 additions and 27 deletions

View File

@ -6,6 +6,9 @@ use std::ops::DerefMut;
use ureq::{Agent, Request, Response};
use url::Url;
const MANIFEST_PICKER_PLATFORM: Os = Os::Linux;
const MANIFEST_PICKER_ARCHITECTURE: Arch = Arch::Amd64;
pub struct RegistryClient {
agent: Agent,
url: Url,
@ -86,7 +89,9 @@ impl RegistryClient {
fn pick_manifest(&mut self, index: ImageIndex) -> Option<Descriptor> {
for item in index.manifests() {
if let Some(platform) = item.platform() {
if *platform.os() == Os::Linux && *platform.architecture() == Arch::Amd64 {
if *platform.os() == MANIFEST_PICKER_PLATFORM
&& *platform.architecture() == MANIFEST_PICKER_ARCHITECTURE
{
return Some(item.clone());
}
}

View File

@ -2,6 +2,9 @@ use anyhow::Result;
use std::fmt;
use url::Url;
const DOCKER_HUB_MIRROR: &str = "mirror.gcr.io";
const DEFAULT_IMAGE_TAG: &str = "latest";
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImageName {
pub hostname: String,
@ -33,20 +36,39 @@ impl Default for ImageName {
impl ImageName {
pub fn parse(name: &str) -> Result<Self> {
let (hostname, name) = name
let full_name = name.to_string();
let name = full_name.clone();
let (mut hostname, mut name) = name
.split_once('/')
.unwrap_or(("registry-1.docker.io", name));
let (hostname, port) = if let Some((hostname, port)) = hostname.split_once(':') {
(hostname, Some(str::parse(port)?))
.map(|x| (x.0.to_string(), x.1.to_string()))
.unwrap_or_else(|| (DOCKER_HUB_MIRROR.to_string(), format!("library/{}", name)));
// heuristic to find any docker hub image formats
// that may be in the hostname format. for example:
// abc/xyz:latest will trigger this if check, but abc.io/xyz:latest will not,
// and neither will abc/hello/xyz:latest
if !hostname.contains('.') && full_name.chars().filter(|x| *x == '/').count() == 1 {
name = format!("{}/{}", hostname, name);
hostname = DOCKER_HUB_MIRROR.to_string();
}
let (hostname, port) = if let Some((hostname, port)) = hostname
.split_once(':')
.map(|x| (x.0.to_string(), x.1.to_string()))
{
(hostname, Some(str::parse(&port)?))
} else {
(hostname, None)
};
let (name, reference) = name.split_once(':').unwrap_or((name, "latest"));
let (name, reference) = name
.split_once(':')
.map(|x| (x.0.to_string(), x.1.to_string()))
.unwrap_or((name.to_string(), DEFAULT_IMAGE_TAG.to_string()));
Ok(ImageName {
hostname: hostname.to_string(),
hostname,
port,
name: name.to_string(),
reference: reference.to_string(),
name,
reference,
})
}

View File

@ -0,0 +1,29 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/.."
if [ -z "${RUST_LOG}" ]
then
RUST_LOG="INFO"
fi
CARGO_BUILD_FLAGS=""
if [ "${KRATA_BUILD_QUIET}" = "1" ]
then
CARGO_BUILD_FLAGS="-q"
fi
build_and_run() {
EXE_TARGET="${1}"
shift
if [ "${KRATA_BUILD_INITRD}" = "1" ]
then
./initrd/build.sh -q
sudo cp "target/initrd/initrd" "/var/lib/krata/default/initrd"
fi
cargo build ${CARGO_BUILD_FLAGS} --target x86_64-unknown-linux-gnu --bin "${EXE_TARGET}"
exec sudo RUST_LOG="${RUST_LOG}" "target/x86_64-unknown-linux-gnu/debug/${EXE_TARGET}" "${@}"
}

View File

@ -1,14 +1,8 @@
#!/bin/sh
set -e
if [ -z "${RUST_LOG}" ]
then
RUST_LOG="INFO"
fi
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/.."
./initrd/build.sh -q
sudo cp "target/initrd/initrd" "/var/lib/krata/default/initrd"
cargo build --target x86_64-unknown-linux-gnu --bin kratactl
exec sudo RUST_LOG="${RUST_LOG}" target/x86_64-unknown-linux-gnu/debug/kratactl "${@}"
# shellcheck source-path=krata-debug-common.sh
. "$(dirname "${REAL_SCRIPT}")/krata-debug-common.sh"
KRATA_BUILD_INITRD=1 build_and_run kratactl "${@}"

View File

@ -1,12 +1,8 @@
#!/bin/sh
set -e
if [ -z "${RUST_LOG}" ]
then
RUST_LOG="INFO"
fi
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/.."
cargo build --target x86_64-unknown-linux-gnu --bin kratanet
exec sudo RUST_LOG="${RUST_LOG}" target/x86_64-unknown-linux-gnu/debug/kratanet "${@}"
# shellcheck source-path=krata-debug-common.sh
. "$(dirname "${REAL_SCRIPT}")/krata-debug-common.sh"
build_and_run kratanet "${@}"