From afc2669d5d354980010c19f1f48bd2d814766e09 Mon Sep 17 00:00:00 2001 From: Husni Faiz Date: Tue, 9 May 2023 21:16:03 +0530 Subject: [PATCH] manifest: use pack home directory to store local manifests Signed-off-by: Husni Faiz --- internal/commands/manifest_add.go | 11 +++++++++++ internal/commands/manifest_annotate.go | 12 ++++++++++++ internal/commands/manifest_create.go | 9 +++++++++ internal/commands/manifest_push.go | 13 ++++++++++++- pkg/client/add_manifest.go | 12 +++++++++++- pkg/client/annotate_manifest.go | 14 ++++++++++++-- pkg/client/client.go | 2 +- pkg/client/create_manifest.go | 9 ++++++--- pkg/client/push_manifest.go | 24 +++++++++++++++++++++++- 9 files changed, 97 insertions(+), 9 deletions(-) diff --git a/internal/commands/manifest_add.go b/internal/commands/manifest_add.go index 4c8d33ed3..155ab7f60 100644 --- a/internal/commands/manifest_add.go +++ b/internal/commands/manifest_add.go @@ -1,6 +1,9 @@ package commands import ( + "path/filepath" + + "github.com/buildpacks/pack/internal/config" "github.com/buildpacks/pack/internal/style" "github.com/buildpacks/pack/pkg/client" "github.com/buildpacks/pack/pkg/logging" @@ -36,8 +39,16 @@ func ManifestAdd(logger logging.Logger, pack PackClient) *cobra.Command { all = flags.All } + packHome, err := config.PackHome() + if err != nil { + return err + } + + manifestDir := filepath.Join(packHome, "manifests") + if err := pack.AddManifest(cmd.Context(), client.AddManifestOptions{ Index: indexName, + Path: manifestDir, Manifest: manifest, All: all, }); err != nil { diff --git a/internal/commands/manifest_annotate.go b/internal/commands/manifest_annotate.go index c7bc5b78d..fc25ccbac 100644 --- a/internal/commands/manifest_annotate.go +++ b/internal/commands/manifest_annotate.go @@ -1,6 +1,9 @@ package commands import ( + "path/filepath" + + "github.com/buildpacks/pack/internal/config" "github.com/buildpacks/pack/internal/style" "github.com/buildpacks/pack/pkg/client" "github.com/buildpacks/pack/pkg/logging" @@ -28,8 +31,17 @@ func ManifestAnnotate(logger logging.Logger, pack PackClient) *cobra.Command { indexName := args[0] manifest := args[1] + + packHome, err := config.PackHome() + if err != nil { + return err + } + + manifestDir := filepath.Join(packHome, "manifests") + if err := pack.AnnotateManifest(cmd.Context(), client.AnnotateManifestOptions{ Index: indexName, + Path: manifestDir, Manifest: manifest, Architecture: flags.Architecture, OS: flags.OS, diff --git a/internal/commands/manifest_create.go b/internal/commands/manifest_create.go index 6e47fee4f..be010e3e4 100644 --- a/internal/commands/manifest_create.go +++ b/internal/commands/manifest_create.go @@ -4,6 +4,7 @@ import ( "path/filepath" "github.com/buildpacks/imgutil" + "github.com/buildpacks/pack/internal/config" "github.com/buildpacks/pack/internal/style" "github.com/buildpacks/pack/pkg/client" "github.com/buildpacks/pack/pkg/logging" @@ -55,6 +56,13 @@ func ManifestCreate(logger logging.Logger, pack PackClient) *cobra.Command { return errors.Wrap(err, "getting absolute layout path") } + packHome, err := config.PackHome() + if err != nil { + return err + } + + manifestDir := filepath.Join(packHome, "manifests") + indexName := args[0] manifests := args[1:] if err := pack.CreateManifest(cmd.Context(), client.CreateManifestOptions{ @@ -64,6 +72,7 @@ func ManifestCreate(logger logging.Logger, pack PackClient) *cobra.Command { Publish: flags.Publish, Registry: flags.Registry, LayoutDir: layoutDir, + ManifestDir: manifestDir, }); err != nil { return err } diff --git a/internal/commands/manifest_push.go b/internal/commands/manifest_push.go index 149f85cd6..efc952349 100644 --- a/internal/commands/manifest_push.go +++ b/internal/commands/manifest_push.go @@ -1,6 +1,9 @@ package commands import ( + "path/filepath" + + "github.com/buildpacks/pack/internal/config" "github.com/buildpacks/pack/internal/style" "github.com/buildpacks/pack/pkg/client" "github.com/buildpacks/pack/pkg/logging" @@ -23,7 +26,7 @@ func ManifestPush(logger logging.Logger, pack PackClient) *cobra.Command { cmd := &cobra.Command{ Use: "push [OPTIONS] ", Short: "Push a manifest list to a repository", - Args: cobra.MatchAll(cobra.ExactArgs(2)), + Args: cobra.MatchAll(cobra.ExactArgs(1)), Example: `pack manifest push cnbs/sample-package:hello-multiarch-universe`, Long: "manifest push pushes a manifest list (Image index) to a registry.", RunE: logError(logger, func(cmd *cobra.Command, args []string) error { @@ -32,8 +35,16 @@ func ManifestPush(logger logging.Logger, pack PackClient) *cobra.Command { } indexName := args[0] + packHome, err := config.PackHome() + if err != nil { + return err + } + + manifestDir := filepath.Join(packHome, "manifests") + if err := pack.PushManifest(cmd.Context(), client.PushManifestOptions{ Index: indexName, + Path: manifestDir, }); err != nil { return err } diff --git a/pkg/client/add_manifest.go b/pkg/client/add_manifest.go index 61768aa13..4c1047757 100644 --- a/pkg/client/add_manifest.go +++ b/pkg/client/add_manifest.go @@ -8,13 +8,23 @@ import ( type AddManifestOptions struct { Index string + Path string Manifest string All bool } func (c *Client) AddManifest(ctx context.Context, opts AddManifestOptions) error { + indexManifest, err := local.GetIndexManifest(opts.Index, opts.Path) + if err != nil { + panic(err) + } + + idx, err := local.NewIndex(opts.Index, opts.Path, local.WithManifest(indexManifest)) + if err != nil { + panic(err) + } - err := local.AppendManifest(opts.Index, opts.Manifest) + err = idx.AppendManifest(opts.Manifest) if err != nil { return err } diff --git a/pkg/client/annotate_manifest.go b/pkg/client/annotate_manifest.go index abc0d4ddc..2ec2e4503 100644 --- a/pkg/client/annotate_manifest.go +++ b/pkg/client/annotate_manifest.go @@ -8,6 +8,7 @@ import ( type AnnotateManifestOptions struct { Index string + Path string Manifest string Architecture string OS string @@ -15,8 +16,17 @@ type AnnotateManifestOptions struct { } func (c *Client) AnnotateManifest(ctx context.Context, opts AnnotateManifestOptions) error { - err := local.AnnotateManifest( - opts.Index, + indexManifest, err := local.GetIndexManifest(opts.Index, opts.Path) + if err != nil { + panic(err) + } + + idx, err := local.NewIndex(opts.Index, opts.Path, local.WithManifest(indexManifest)) + if err != nil { + panic(err) + } + + err = idx.AnnotateManifest( opts.Manifest, local.AnnotateFields{ Architecture: opts.Architecture, diff --git a/pkg/client/client.go b/pkg/client/client.go index 6d15d7899..4b7781ad2 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -301,6 +301,6 @@ func (f *indexFactory) NewIndex(opts CreateManifestOptions) (imgutil.ImageIndex, return local.NewIndex( opts.ManifestName, - f.dockerClient, + opts.ManifestDir, local.WithIndexMediaTypes(opts.MediaType)) } diff --git a/pkg/client/create_manifest.go b/pkg/client/create_manifest.go index 118119a17..c982b9090 100644 --- a/pkg/client/create_manifest.go +++ b/pkg/client/create_manifest.go @@ -25,6 +25,9 @@ type CreateManifestOptions struct { // Directory to store OCI layout LayoutDir string + + // Directory to store OCI layout + ManifestDir string } func (c *Client) CreateManifest(ctx context.Context, opts CreateManifestOptions) error { @@ -32,21 +35,21 @@ func (c *Client) CreateManifest(ctx context.Context, opts CreateManifestOptions) indexCreator := c.indexFactory idx, err := indexCreator.NewIndex(opts) if err != nil { - panic(err) + return err } // Add every manifest to image index for _, j := range opts.Manifests { err := idx.Add(j) if err != nil { - panic(err) + return err } } // Store index err = idx.Save() if err != nil { - panic(err) + return err } return nil diff --git a/pkg/client/push_manifest.go b/pkg/client/push_manifest.go index 5b99c90f7..bb5d52724 100644 --- a/pkg/client/push_manifest.go +++ b/pkg/client/push_manifest.go @@ -1,11 +1,33 @@ package client -import "context" +import ( + "context" + + "github.com/buildpacks/imgutil/local" + "github.com/buildpacks/imgutil/remote" +) type PushManifestOptions struct { Index string + Path string } func (c *Client) PushManifest(ctx context.Context, opts PushManifestOptions) error { + indexManifest, err := local.GetIndexManifest(opts.Index, opts.Path) + if err != nil { + panic(err) + } + + idx, err := remote.NewIndex(opts.Index, c.keychain, remote.WithManifest(indexManifest)) + if err != nil { + panic(err) + } + + // Store index + err = idx.Save() + if err != nil { + panic(err) + } + return nil }