mirror of
https://github.com/imjasonh/krust
synced 2026-07-13 08:57:22 +00:00
Add Cargo.toml-based configuration for base image
- Add support for [package.metadata.krust] in Cargo.toml - Change default base image to cgr.dev/chainguard/static:latest - Add ProjectConfig struct to load project-specific settings - Update documentation and example to show configuration usage - Fix integration tests to explicitly pass directory argument - This is the idiomatic way for Rust build tools to be configured
This commit is contained in:
parent
5d254c7eca
commit
11083dc527
6 changed files with 74 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
@ -45,7 +45,7 @@ pub struct RegistryAuth {
|
|||
}
|
||||
|
||||
fn default_base_image() -> String {
|
||||
"gcr.io/distroless/static:nonroot".to_string()
|
||||
"cgr.dev/chainguard/static:latest".to_string()
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
|
|
@ -59,6 +59,14 @@ impl Default for Config {
|
|||
}
|
||||
}
|
||||
|
||||
/// Project-specific configuration from Cargo.toml
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct ProjectConfig {
|
||||
/// Base image for this project
|
||||
#[serde(rename = "base-image")]
|
||||
pub base_image: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> anyhow::Result<Self> {
|
||||
if let Some(config_dir) = dirs::config_dir() {
|
||||
|
|
@ -71,4 +79,27 @@ impl Config {
|
|||
}
|
||||
Ok(Config::default())
|
||||
}
|
||||
|
||||
/// Load project-specific configuration from Cargo.toml
|
||||
pub fn load_project_config(project_path: &Path) -> anyhow::Result<ProjectConfig> {
|
||||
let cargo_toml_path = project_path.join("Cargo.toml");
|
||||
if !cargo_toml_path.exists() {
|
||||
return Ok(ProjectConfig::default());
|
||||
}
|
||||
|
||||
let content = std::fs::read_to_string(&cargo_toml_path)?;
|
||||
let value: toml::Value = toml::from_str(&content)?;
|
||||
|
||||
// Look for [package.metadata.krust] section
|
||||
if let Some(metadata) = value
|
||||
.get("package")
|
||||
.and_then(|p| p.get("metadata"))
|
||||
.and_then(|m| m.get("krust"))
|
||||
{
|
||||
let project_config: ProjectConfig = metadata.clone().try_into()?;
|
||||
return Ok(project_config);
|
||||
}
|
||||
|
||||
Ok(ProjectConfig::default())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_default_config() {
|
||||
let config = Config::default();
|
||||
assert_eq!(config.base_image, "gcr.io/distroless/static:nonroot");
|
||||
assert_eq!(config.base_image, "cgr.dev/chainguard/static:latest");
|
||||
assert!(config.default_registry.is_none());
|
||||
assert!(config.registries.is_empty());
|
||||
}
|
||||
|
|
|
|||
11
src/main.rs
11
src/main.rs
|
|
@ -38,6 +38,14 @@ async fn main() -> Result<()> {
|
|||
let config = Config::load()?;
|
||||
let project_path = path.unwrap_or_else(|| PathBuf::from("."));
|
||||
|
||||
// Load project-specific config from Cargo.toml
|
||||
let project_config = Config::load_project_config(&project_path)?;
|
||||
|
||||
// Determine base image (project config takes precedence)
|
||||
let base_image = project_config
|
||||
.base_image
|
||||
.unwrap_or(config.base_image.clone());
|
||||
|
||||
// Determine the image name
|
||||
let image_ref = if let Some(image) = image {
|
||||
// Use explicit image if provided
|
||||
|
|
@ -56,8 +64,7 @@ async fn main() -> Result<()> {
|
|||
let binary_path = builder.build()?;
|
||||
|
||||
// Build container image
|
||||
let image_builder =
|
||||
ImageBuilder::new(binary_path, config.base_image.clone(), platform.clone());
|
||||
let image_builder = ImageBuilder::new(binary_path, base_image, platform.clone());
|
||||
|
||||
let (config_data, layer_data, manifest) = image_builder.build()?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue