Skip to content

Commit

Permalink
Introduction of the copy API
Browse files Browse the repository at this point in the history
Signed-off-by: Joao Pereira <joaod@vmware.com>
  • Loading branch information
joaopapereira committed Jun 3, 2024
1 parent d080918 commit 99ba904
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 348 deletions.
54 changes: 36 additions & 18 deletions pkg/imgpkg/cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import (
"carvel.dev/imgpkg/pkg/imgpkg/plainimage"
"carvel.dev/imgpkg/pkg/imgpkg/registry"
"carvel.dev/imgpkg/pkg/imgpkg/signature"
v1 "carvel.dev/imgpkg/pkg/imgpkg/v1"
"github.com/cppforlife/go-cli-ui/ui"
"github.com/spf13/cobra"
)

const rootBundleLabelKey string = "dev.carvel.imgpkg.copy.root-bundle"

type CopyOptions struct {
ui ui.UI

Expand Down Expand Up @@ -116,26 +115,21 @@ func (c *CopyOptions) Run() error {
imageSet := ctlimgset.NewImageSet(c.Concurrency, prefixedLogger, tagGen)
tarImageSet := ctlimgset.NewTarImageSet(imageSet, c.Concurrency, prefixedLogger)

var signatureRetriever SignatureRetriever
var signatureRetriever v1.SignatureFetcher
if c.SignatureFlags.CopyCosignSignatures {
signatureRetriever = signature.NewSignatures(signature.NewCosign(reg), c.Concurrency)
} else {
signatureRetriever = signature.NewNoop()
}

repoSrc := CopyRepoSrc{
ImageFlags: c.ImageFlags,
BundleFlags: c.BundleFlags,
LockInputFlags: c.LockInputFlags,
TarFlags: c.TarFlags,
IncludeNonDistributable: c.IncludeNonDistributable,
opts := v1.CopyOpts{
Logger: levelLogger,
ImageSet: imageSet,
TarImageSet: tarImageSet,
Concurrency: c.Concurrency,

logger: levelLogger,
registry: registry.NewRegistryWithProgress(reg, imagesUploaderLogger),
imageSet: imageSet,
tarImageSet: tarImageSet,
signatureRetriever: signatureRetriever,
SignatureRetriever: signatureRetriever,
IncludeNonDistributable: c.IncludeNonDistributable,
Resume: c.TarFlags.Resume,
}

switch {
Expand All @@ -146,17 +140,41 @@ func (c *CopyOptions) Run() error {
if c.LockOutputFlags.LockFilePath != "" {
return fmt.Errorf("Cannot output lock file with tar destination")
}
return repoSrc.CopyToTar(c.TarFlags.TarDst, c.TarFlags.Resume)

origin := v1.CopyOrigin{
ImageRef: c.ImageFlags.Image,
BundleRef: c.BundleFlags.Bundle,
LockfilePath: c.LockInputFlags.LockFilePath,
}
ids, err := v1.CopyToTar(origin, c.TarFlags.TarDst, opts, registry.NewRegistryWithProgress(reg, imagesUploaderLogger))
if err != nil {
return err
}
informUserToUseTheNonDistributableFlagWithDescriptors(
levelLogger, c.IncludeNonDistributable, getNonDistributableLayersFromImageDescriptors(ids))

return nil

case c.isRepoDst():
if c.TarFlags.Resume {
return fmt.Errorf("Flag --resume can only be used when copying to tar")
}

processedImages, err := repoSrc.CopyToRepo(c.RepoDst)
origin := v1.CopyOrigin{
ImageRef: c.ImageFlags.Image,
BundleRef: c.BundleFlags.Bundle,
TarPath: c.TarFlags.TarSrc,
LockfilePath: c.LockInputFlags.LockFilePath,
}

processedImages, err := v1.CopyToRepository(origin, c.RepoDst, opts, reg)
if err != nil {
return err
}

informUserToUseTheNonDistributableFlagWithDescriptors(
levelLogger, c.IncludeNonDistributable, processedImagesNonDistLayer(processedImages))

return c.writeLockOutput(processedImages, reg)

default:
Expand Down Expand Up @@ -205,7 +223,7 @@ func (c *CopyOptions) findProcessedImageRootBundle(processedImages *ctlimgset.Pr
var bundleProcessedImage *ctlimgset.ProcessedImage

for _, processedImage := range processedImages.All() {
if _, ok := processedImage.Labels[rootBundleLabelKey]; ok {
if v1.IsRootBundle(processedImage) {
if bundleProcessedImage != nil {
panic("Internal inconsistency: expected only 1 root bundle")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/imgpkg/imageset/unprocessed_image_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type UnprocessedImageRef struct {
OrigRef string
}

// LabelValue returns the value of the provided label and a bool to identify if the label was present or not
func (u UnprocessedImageRef) LabelValue(label string) (string, bool) {
value, ok := u.Labels[label]
return value, ok
}

// Key that uniquely identify a ImageRef
func (u UnprocessedImageRef) Key() string {
// With this definition of key if one image is shared by 2 bundles
Expand Down
Loading

0 comments on commit 99ba904

Please sign in to comment.