### Build and Serialize Repository List (Rust) Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Shows how to construct a `RepositoryList` using the `RepositoryListBuilder` and populate it with repository names. The example also demonstrates serializing the `RepositoryList` into a JSON string, which is useful for API responses. ```rust use oci_spec::distribution::RepositoryListBuilder; // Create repository list let repo_list = RepositoryListBuilder::default() .repositories(vec![ "library/nginx".to_string(), "library/alpine".to_string(), "myorg/myapp".to_string(), ]) .build() .expect("build repository list"); // Access repositories for repo in repo_list.repositories() { println!("Repository: {}", repo); } // Serialize to JSON for API response let json = serde_json::to_string_pretty(&repo_list).unwrap(); println!("{}", json); // Output: // { // "repositories": [ // "library/nginx", // "library/alpine", // "myorg/myapp" // ] // } ``` -------------------------------- ### Create OCI Distribution Repository List (Rust) Source: https://github.com/youki-dev/oci-spec-rs/blob/main/README.md Shows how to create an OCI Distribution Repository List using the `oci-spec` crate's builder pattern. This example constructs a list containing a single repository name. ```rust use oci_spec::distribution::RepositoryListBuilder; let list = RepositoryListBuilder::default() .repositories(vec!["busybox".to_owned()]) .build().unwrap(); ``` -------------------------------- ### Build and Serialize Tag List (Rust) Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Illustrates creating a `TagList` using `TagListBuilder`, specifying the repository name and a vector of tags. The example includes printing the repository name and tags, and serializing the `TagList` to a pretty-printed JSON string for API interactions. ```rust use oci_spec::distribution::TagListBuilder; // Create tag list let tag_list = TagListBuilder::default() .name("library/nginx") .tags(vec![ "latest".to_string(), "1.21".to_string(), "1.21.6".to_string(), "1.20".to_string(), "alpine".to_string(), ]) .build() .expect("build tag list"); println!("Repository: {}", tag_list.name()); println!("Tags: {:?}", tag_list.tags()); // Serialize for API response let json = serde_json::to_string_pretty(&tag_list).unwrap(); ``` -------------------------------- ### Install oci-spec-rs Rust Crate Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Add the oci-spec crate to your Cargo.toml file. You can enable specific features like 'image' or 'runtime' to reduce build size and dependencies. ```toml [dependencies] oci-spec = "0.8.4" ``` ```toml [dependencies] oci-spec = { version = "0.8.4", default-features = false, features = ["image", "runtime"] } ``` -------------------------------- ### Manage OCI Container Runtime Configuration (Spec) in Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Demonstrates loading, creating default, creating rootless, and building custom OCI runtime specifications using the `Spec` struct and its builders. Handles configuration details like version, root filesystem, process, mounts, and Linux namespaces. ```rust use oci_spec::runtime::{ Spec, SpecBuilder, RootBuilder, ProcessBuilder, MountBuilder, LinuxBuilder, LinuxNamespaceBuilder, LinuxNamespaceType }; // Load spec from file let spec = Spec::load("config.json").unwrap(); println!("OCI Version: {}", spec.version()); if let Some(root) = spec.root() { println!("Root path: {}", root.path().display()); } // Create default spec (Linux container with sensible defaults) let default_spec = Spec::default(); default_spec.save("config.json").unwrap(); // Create rootless container spec let rootless_spec = Spec::rootless(1000, 1000); rootless_spec.save("rootless-config.json").unwrap(); // Create custom spec using builders let spec = SpecBuilder::default() .version("1.0.2") .root( RootBuilder::default() .path("rootfs") .readonly(true) .build() .unwrap() ) .hostname("my-container") .process( ProcessBuilder::default() .terminal(true) .user(Default::default()) .args(vec!["/bin/bash".to_string()]) .env(vec![ "PATH=/usr/local/bin:/usr/bin:/bin".to_string(), "TERM=xterm-256color".to_string(), ]) .cwd("/") .no_new_privileges(true) .build() .unwrap() ) .mounts(vec![ MountBuilder::default() .destination("/proc") .typ("proc") .source("proc") .build() .unwrap(), MountBuilder::default() .destination("/dev") .typ("tmpfs") .source("tmpfs") .options(vec!["nosuid".to_string(), "strictatime".to_string()]) .build() .unwrap(), ]) .linux( LinuxBuilder::default() .namespaces(vec![ LinuxNamespaceBuilder::default() .typ(LinuxNamespaceType::Pid) .build() .unwrap(), LinuxNamespaceBuilder::default() .typ(LinuxNamespaceType::Network) .build() .unwrap(), LinuxNamespaceBuilder::default() .typ(LinuxNamespaceType::Mount) .build() .unwrap(), ]) .build() .unwrap() ) .build() .expect("build spec"); spec.save("custom-config.json").unwrap(); ``` -------------------------------- ### Create OCI Image Manifest with Builder (Rust) Source: https://github.com/youki-dev/oci-spec-rs/blob/main/README.md Illustrates creating a new OCI Image Manifest programmatically using the builder pattern provided by the `oci-spec` crate. This includes defining configuration descriptors and layer descriptors, then assembling them into a complete manifest, and finally saving it to a pretty-printed JSON file. ```rust use std::str::FromStr; use oci_spec::image::{ Descriptor, DescriptorBuilder, ImageManifest, ImageManifestBuilder, MediaType, Sha256Digest, SCHEMA_VERSION }; let config = DescriptorBuilder::default() .media_type(MediaType::ImageConfig) .size(7023u64) .digest(Sha256Digest::from_str("b5b2b2c507a0944348e0303114d8d93aaaa081732b86451d9bce1f432a537bc7").unwrap()) .build() .expect("build config descriptor"); let layers: Vec = [ ( 32654u64, "9834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f0", ), ( 16724, "3c3a4604a545cdc127456d94e421cd355bca5b528f4a9c1905b15da2eb4a4c6b", ), ( 73109, "ec4b8955958665577945c89419d1af06b5f7636b4ac3da7f12184802ad867736", ), ] .iter() .map(|l| { DescriptorBuilder::default() .media_type(MediaType::ImageLayerGzip) .size(l.0) .digest(Sha256Digest::from_str(l.1).unwrap()) .build() .expect("build layer") }) .collect(); let image_manifest = ImageManifestBuilder::default() .schema_version(SCHEMA_VERSION) .config(config) .layers(layers) .build() .expect("build image manifest"); image_manifest.to_file_pretty("my-manifest.json").unwrap(); ``` -------------------------------- ### Load and Create ImageConfiguration in Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Demonstrates loading an existing ImageConfiguration from a JSON file and programmatically creating a new one using builders. It covers setting various runtime parameters like entrypoint, environment variables, exposed ports, and layer history. Dependencies include the `oci_spec::image` crate. ```rust use oci_spec::image::{ Arch, ConfigBuilder, ImageConfiguration, ImageConfigurationBuilder, Os, RootFsBuilder, HistoryBuilder }; // Load configuration from file let config = ImageConfiguration::from_file("config.json").unwrap(); println!("Architecture: {}", config.architecture()); println!("OS: {}", config.os()); if let Some(cfg) = config.config() { if let Some(entrypoint) = cfg.entrypoint() { println!("Entrypoint: {:?}", entrypoint); } if let Some(env) = cfg.env() { println!("Environment: {:?}", env); } } // Create image configuration let runtime_config = ConfigBuilder::default() .user("appuser") .exposed_ports(vec!["8080/tcp".to_string(), "443/tcp".to_string()]) .env(vec![ "PATH=/usr/local/bin:/usr/bin:/bin".to_string(), "APP_ENV=production".to_string(), ]) .entrypoint(vec!["/app/server".to_string()]) .cmd(vec!["--config".to_string(), "/etc/app/config.yaml".to_string()]) .working_dir("/app") .build() .expect("build config"); let image_config = ImageConfigurationBuilder::default() .created("2024-01-15T10:30:00Z") .author("developer@example.com") .architecture(Arch::Amd64) .os(Os::Linux) .config(runtime_config) .rootfs( RootFsBuilder::default() .diff_ids(vec![ "sha256:abc123...".to_string(), "sha256:def456...".to_string(), ]) .build() .expect("build rootfs") ) .history(vec![ HistoryBuilder::default() .created("2024-01-15T10:00:00Z") .created_by("ADD base.tar /") .build() .expect("build history"), HistoryBuilder::default() .created("2024-01-15T10:30:00Z") .created_by("COPY app /app") .empty_layer(false) .build() .expect("build history"), ]) .build() .expect("build image configuration"); image_config.to_file_pretty("my-config.json").unwrap(); ``` -------------------------------- ### Create and Manage OCI Image Manifests in Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Demonstrates how to load, create, and save OCI Image Manifests using the oci-spec-rs library in Rust. It covers using builder patterns for descriptors and manifests, and serializing to JSON. ```rust use std::str::FromStr; use oci_spec::image::{ Descriptor, DescriptorBuilder, ImageManifest, ImageManifestBuilder, MediaType, Sha256Digest, SCHEMA_VERSION }; // Load manifest from file let manifest = ImageManifest::from_file("manifest.json").unwrap(); println!("Layers: {}", manifest.layers().len()); println!("Config digest: {}", manifest.config().digest()); // Create manifest using builder pattern let config = DescriptorBuilder::default() .media_type(MediaType::ImageConfig) .size(7023u64) .digest(Sha256Digest::from_str( "b5b2b2c507a0944348e0303114d8d93aaaa081732b86451d9bce1f432a537bc7" ).unwrap()) .build() .expect("build config descriptor"); let layers: Vec = vec![ (32654u64, "9834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f0"), (16724u64, "3c3a4604a545cdc127456d94e421cd355bca5b528f4a9c1905b15da2eb4a4c6b"), ] .iter() .map(|(size, digest)| { DescriptorBuilder::default() .media_type(MediaType::ImageLayerGzip) .size(*size) .digest(Sha256Digest::from_str(digest).unwrap()) .build() .expect("build layer") }) .collect(); let image_manifest = ImageManifestBuilder::default() .schema_version(SCHEMA_VERSION) .config(config) .layers(layers) .build() .expect("build image manifest"); // Save to file (pretty-printed JSON) image_manifest.to_file_pretty("my-manifest.json").unwrap(); // Convert to JSON string let json_string = image_manifest.to_string_pretty().unwrap(); println!("{}", json_string); ``` -------------------------------- ### Manage OCI Container Runtime State in Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Illustrates how to load, create, save, and check the status of OCI container runtime states using the `State` struct and `StateBuilder`. This includes managing container ID, status, PID, bundle path, and annotations. ```rust use oci_spec::runtime::{State, StateBuilder, ContainerState}; use std::path::PathBuf; use std::collections::HashMap; // Load state from file let state = State::load("state.json").unwrap(); println!("Container ID: {}", state.id()); println!("Status: {}", state.status()); println!("PID: {:?}", state.pid()); // Create and save container state let mut annotations = HashMap::new(); annotations.insert("com.example.key".to_string(), "value".to_string()); let state = StateBuilder::default() .version("1.0.2") .id("container-12345") .status(ContainerState::Running) .pid(12345) .bundle(PathBuf::from("/var/run/containers/container-12345")) .annotations(annotations) .build() .expect("build state"); state.save("state.json").unwrap(); // Check container state match state.status() { ContainerState::Creating => println!("Container is being created"), ContainerState::Created => println!("Container created but not started"), ContainerState::Running => println!("Container is running"), ContainerState::Stopped => println!("Container has stopped"), } ``` -------------------------------- ### Parse and Manipulate Container Image References (Rust) Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Demonstrates parsing container image references from strings, extracting components like registry, repository, tag, and digest. It also shows how to programmatically create references and resolve registry addresses. This functionality is crucial for tools that need to understand and interact with container image locations. ```rust use oci_spec::distribution::Reference; // Parse image reference from string let reference: Reference = "docker.io/library/nginx:1.21".parse().unwrap(); println!("Registry: {}", reference.registry()); // "docker.io" println!("Repository: {}", reference.repository()); // "library/nginx" println!("Tag: {:?}", reference.tag()); // Some("1.21") println!("Digest: {:?}", reference.digest()); // None println!("Full reference: {}", reference.whole()); // Parse digest reference let digest_ref: Reference = "ghcr.io/owner/image@sha256:abc123def456...".parse().unwrap(); println!("Digest: {:?}", digest_ref.digest()); // Create reference programmatically let ref_with_tag = Reference::with_tag( "my-registry.io".to_string(), "my-org/my-image".to_string(), "v1.2.3".to_string(), ); let ref_with_digest = Reference::with_digest( "my-registry.io".to_string(), "my-org/my-image".to_string(), "sha256:abcdef1234567890...".to_string(), ); // Reference with both tag and digest let ref_both = Reference::with_tag_and_digest( "docker.io".to_string(), "library/nginx".to_string(), "1.21".to_string(), "sha256:abc123...".to_string(), ); // Resolve registry address (handles docker.io -> index.docker.io) let resolved = reference.resolve_registry(); println!("Resolved registry: {}", resolved); // "index.docker.io" // Short image names are normalized let short: Reference = "nginx".parse().unwrap(); println!("Registry: {}", short.registry()); // "docker.io" println!("Repository: {}", short.repository()); // "library/nginx" println!("Tag: {:?}", short.tag()); // Some("latest") ``` -------------------------------- ### OCI Media Type Handling in Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Illustrates how to use the `MediaType` enum in Rust for standard OCI media types, including conversion from strings and custom types. It also shows how to convert OCI media types to their Docker V2 Schema 2 equivalents for compatibility. Requires the `oci_spec::image` crate. ```rust use oci_spec::image::{MediaType, ToDockerV2S2}; // Standard OCI media types let manifest_type = MediaType::ImageManifest; println!("{}", manifest_type); // "application/vnd.oci.image.manifest.v1+json" let layer_type = MediaType::ImageLayerGzip; println!("{}", layer_type); // "application/vnd.oci.image.layer.v1.tar+gzip" // Convert from string let media_type: MediaType = "application/vnd.oci.image.config.v1+json".into(); assert_eq!(media_type, MediaType::ImageConfig); // Custom/unknown media types let custom: MediaType = "application/custom+json".into(); if let MediaType::Other(s) = custom { println!("Custom media type: {}", s); } // Convert to Docker V2 Schema 2 format for compatibility let docker_manifest = MediaType::ImageManifest.to_docker_v2s2().unwrap(); println!("{}", docker_manifest); // "application/vnd.docker.distribution.manifest.v2+json" ``` -------------------------------- ### Load OCI Image Manifest from File (Rust) Source: https://github.com/youki-dev/oci-spec-rs/blob/main/README.md Demonstrates how to load an OCI Image Manifest from a JSON file using the `oci-spec` crate. It deserializes the file content into an `ImageManifest` struct and asserts the number of layers. ```rust use oci_spec::image::ImageManifest; let image_manifest = ImageManifest::from_file("manifest.json").unwrap(); assert_eq!(image_manifest.layers().len(), 5); ``` -------------------------------- ### Create and Manage OCI Image Index in Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Shows how to load and create OCI Image Index structures in Rust using the oci-spec-rs library. This is used for multi-platform container images, referencing platform-specific manifests. ```rust use std::str::FromStr; use oci_spec::image::{ Arch, DescriptorBuilder, ImageIndex, ImageIndexBuilder, MediaType, Os, PlatformBuilder, Sha256Digest, SCHEMA_VERSION }; // Load index from file let index = ImageIndex::from_file("index.json").unwrap(); for manifest in index.manifests() { if let Some(platform) = manifest.platform() { println!("Platform: {}/{}", platform.os(), platform.architecture()); } } // Create multi-platform index let amd64_manifest = DescriptorBuilder::default() .media_type(MediaType::ImageManifest) .digest(Sha256Digest::from_str( "5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" ).unwrap()) .size(7682u64) .platform( PlatformBuilder::default() .architecture(Arch::Amd64) .os(Os::Linux) .build() .expect("build amd64 platform") ) .build() .expect("build amd64 manifest descriptor"); let arm64_manifest = DescriptorBuilder::default() .media_type(MediaType::ImageManifest) .digest(Sha256Digest::from_str( "e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f" ).unwrap()) .size(7143u64) .platform( PlatformBuilder::default() .architecture(Arch::ARM64) .os(Os::Linux) .build() .expect("build arm64 platform") ) .build() .expect("build arm64 manifest descriptor"); let image_index = ImageIndexBuilder::default() .schema_version(SCHEMA_VERSION) .manifests(vec![amd64_manifest, arm64_manifest]) .build() .expect("build image index"); image_index.to_file_pretty("my-index.json").unwrap(); ``` -------------------------------- ### Configure Linux Cgroup Resources with Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Defines CPU, memory, PID, and device access limits for Linux containers. It utilizes builders for each resource type and allows fine-grained control over resource allocation and restrictions. Dependencies include the `oci_spec::runtime` module. ```rust use oci_spec::runtime::{ LinuxResourcesBuilder, LinuxMemoryBuilder, LinuxCpuBuilder, LinuxPidsBuilder, LinuxDeviceCgroupBuilder, LinuxDeviceType }; let resources = LinuxResourcesBuilder::default() // Memory limits .memory( LinuxMemoryBuilder::default() .limit(536870912i64) // 512 MB .reservation(268435456i64) // 256 MB soft limit .swap(1073741824i64) // 1 GB total (memory + swap) .disable_oom_killer(false) .build() .unwrap() ) // CPU limits .cpu( LinuxCpuBuilder::default() .shares(1024u64) // CPU shares (relative weight) .quota(100000i64) // 100ms per period .period(100000u64) // 100ms period .cpus("0-3") // CPUs 0, 1, 2, 3 .mems("0") // NUMA node 0 .build() .unwrap() ) // Process limits .pids( LinuxPidsBuilder::default() .limit(100i64) // Max 100 processes .build() .unwrap() ) // Device access control .devices(vec![ LinuxDeviceCgroupBuilder::default() .allow(false) .typ(LinuxDeviceType::A) // All devices .access("rwm") .build() .unwrap(), LinuxDeviceCgroupBuilder::default() .allow(true) .typ(LinuxDeviceType::C) // Character device .major(1i64) .minor(3i64) // /dev/null .access("rwm") .build() .unwrap(), ]) .build() .expect("build resources"); ``` -------------------------------- ### Configure Linux Seccomp Filters with Rust Source: https://context7.com/youki-dev/oci-spec-rs/llms.txt Sets up seccomp system call filtering rules to enhance container security. It defines a default action for disallowed syscalls and specifies allowed syscalls with optional argument conditions. Requires the `oci_spec::runtime` module. ```rust use oci_spec::runtime::{ LinuxSeccompBuilder, LinuxSyscallBuilder, LinuxSeccompAction, LinuxSeccompArgBuilder, LinuxSeccompOperator, Arch }; let seccomp = LinuxSeccompBuilder::default() .default_action(LinuxSeccompAction::ScmpActErrno) .default_errno_ret(1u32) // EPERM .architectures(vec![Arch::ScmpArchX86_64, Arch::ScmpArchAarch64]) .syscalls(vec![ // Allow common syscalls LinuxSyscallBuilder::default() .names(vec![ "read".to_string(), "write".to_string(), "open".to_string(), "close".to_string(), "stat".to_string(), "fstat".to_string(), "mmap".to_string(), "mprotect".to_string(), "munmap".to_string(), "brk".to_string(), "exit".to_string(), "exit_group".to_string(), ]) .action(LinuxSeccompAction::ScmpActAllow) .build() .unwrap(), // Allow socket with restrictions LinuxSyscallBuilder::default() .names(vec!["socket".to_string()]) .action(LinuxSeccompAction::ScmpActAllow) .args(vec![ LinuxSeccompArgBuilder::default() .index(0usize) .value(2u64) // AF_INET .op(LinuxSeccompOperator::ScmpCmpEq) .build() .unwrap(), ]) .build() .unwrap(), ]) .build() .expect("build seccomp"); ``` -------------------------------- ### Bump Version Script for oci-spec-rs Release Source: https://github.com/youki-dev/oci-spec-rs/blob/main/release.md This script is used to propose a new release by bumping the version number of the oci-spec-rs crate. It takes the desired version as an argument. ```console ./hack/release x.y.z ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.