Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add platform flag to store save #329

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions cmd/hauler/cli/store/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SaveCmd(ctx context.Context, o *flags.SaveOpts, outputFile string) error {
return err
}

if err := writeExportsManifest(ctx, "."); err != nil {
if err := writeExportsManifest(ctx, ".", o.Platform); err != nil {
return err
}

Expand All @@ -64,9 +64,15 @@ type exports struct {
records map[string]tarball.Descriptor
}

func writeExportsManifest(ctx context.Context, dir string) error {
func writeExportsManifest(ctx context.Context, dir string, platformStr string) error {
l := log.FromContext(ctx)

// validate platform format
platform, err := libv1.ParsePlatform(platformStr)
if err != nil {
return err
}

oci, err := layout.FromPath(dir)
if err != nil {
return err
Expand Down Expand Up @@ -105,6 +111,12 @@ func writeExportsManifest(ctx context.Context, dir string) error {
}
case consts.KindAnnotationIndex:
l.Debugf("index [%s]: digest=%s, type=%s, size=%d", refName, desc.Digest.String(), desc.MediaType, desc.Size)

// when no platform is provided, warn the user of potential mismatch on import
if platform.String() == "" {
l.Warnf("index [%s]: provide an export platform to prevent potential platform mismatch on import", refName)
}

iix, err := idx.ImageIndex(desc.Digest)
if err != nil {
return err
Expand All @@ -115,6 +127,20 @@ func writeExportsManifest(ctx context.Context, dir string) error {
}
for _, ixd := range ixm.Manifests {
if ixd.MediaType.IsImage() {
// check if platform is provided, if so, skip anything that doesn't match
if platform.String() != "" {
if ixd.Platform.Architecture != platform.Architecture || ixd.Platform.OS != platform.OS {
l.Warnf("index [%s]: digest=%s, platform=%s/%s: does not match the supplied platform, skipping", refName, desc.Digest.String(), ixd.Platform.OS, ixd.Platform.Architecture)
continue
}
}

// skip 'unknown' platforms... docker hates
if ixd.Platform.Architecture == "unknown" && ixd.Platform.OS == "unknown" {
l.Warnf("index [%s]: digest=%s, platform=%s/%s: skipping 'unknown/unknown' platform", refName, desc.Digest.String(), ixd.Platform.OS, ixd.Platform.Architecture)
continue
}

if err := x.record(ctx, iix, ixd, refName); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/flags/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import "github.com/spf13/cobra"
type SaveOpts struct {
*StoreRootOpts
FileName string
Platform string
}

func (o *SaveOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.StringVarP(&o.FileName, "filename", "f", "haul.tar.zst", "(Optional) Specify the name of outputted archive")
f.StringVarP(&o.Platform, "platform", "p", "", "(Optional) Specifiy the platform of the images for the outputted archive... i.e. linux/amd64 (defaults to all)")
}