Skip to content

Commit

Permalink
manifest: use pack home directory to store local manifests
Browse files Browse the repository at this point in the history
Signed-off-by: Husni Faiz <[email protected]>
  • Loading branch information
husni-faiz committed May 9, 2023
1 parent e1d7964 commit afc2669
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 9 deletions.
11 changes: 11 additions & 0 deletions internal/commands/manifest_add.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions internal/commands/manifest_annotate.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions internal/commands/manifest_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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
}
Expand Down
13 changes: 12 additions & 1 deletion internal/commands/manifest_push.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,7 +26,7 @@ func ManifestPush(logger logging.Logger, pack PackClient) *cobra.Command {
cmd := &cobra.Command{
Use: "push [OPTIONS] <manifest-list>",
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 {
Expand All @@ -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
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/client/add_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/client/annotate_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ import (

type AnnotateManifestOptions struct {
Index string
Path string
Manifest string
Architecture string
OS string
Variant string
}

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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
9 changes: 6 additions & 3 deletions pkg/client/create_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@ 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 {

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
Expand Down
24 changes: 23 additions & 1 deletion pkg/client/push_manifest.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit afc2669

Please sign in to comment.