buildkit/platform.go
buildkit/platform.goBrowse 1970 files
423 tokens
1,833 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package buildkit2 3import (4 "github.com/containerd/platforms"5 specs "github.com/opencontainers/image-spec/specs-go/v1"6)7 8// ParsePlatformWithDefaults parses a platform string and returns the corresponding specs.Platform.9// If the input is empty, it defaults to a Linux platform that matches the host architecture.10//11// This function handles the common case where we need to map host platforms to container platforms.12// We cannot use platforms.DefaultSpec() directly because it returns the host platform13// (e.g., darwin/arm64/v8 on macOS), but container base images only support Linux platforms.14// Instead, we map the host architecture to the corresponding Linux container platform15// to provide optimal performance while ensuring compatibility with container runtimes.16//17// Examples:18// - "" -> linux/amd64 (on Intel hosts) or linux/arm64/v8 (on ARM hosts)19// - "linux/amd64" -> linux/amd6420// - "linux/arm64" -> linux/arm6421// - "linux/arm64/v8" -> linux/arm64/v822func ParsePlatformWithDefaults(platformStr string) (specs.Platform, error) {23 if platformStr == "" {24 // Default to Linux platform for container builds25 // We cannot use platforms.DefaultSpec() directly because it returns the host platform26 // (e.g., darwin/arm64/v8 on macOS), but container base images only support Linux platforms.27 // Instead, we map the host architecture to the corresponding Linux container platform28 // to provide optimal performance while ensuring compatibility with container runtimes.29 hostPlatform := platforms.DefaultSpec()30 if hostPlatform.Architecture == "arm64" {31 return specs.Platform{OS: "linux", Architecture: "arm64", Variant: "v8"}, nil32 } else {33 return specs.Platform{OS: "linux", Architecture: "amd64"}, nil34 }35 }36 37 // Parse the user-specified platform string38 return platforms.Parse(platformStr)39}40