Skip to content

Commit

Permalink
Merge pull request #351 from vmware-tanzu/images-location-issue
Browse files Browse the repository at this point in the history
Images Location not correctly created
  • Loading branch information
joaopapereira authored Mar 16, 2022
2 parents a9b3335 + 43f6475 commit acb7d5d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
11 changes: 9 additions & 2 deletions pkg/imgpkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ func NewBundleWithReader(ref string, imagesMetadata ImagesMetadata, imagesLockRe

// DigestRef Bundle full location including registry, repository and digest
func (o *Bundle) DigestRef() string { return o.plainImg.DigestRef() }
func (o *Bundle) Repo() string { return o.plainImg.Repo() }
func (o *Bundle) Tag() string { return o.plainImg.Tag() }

// Digest Bundle Digest
func (o *Bundle) Digest() string { return o.plainImg.Digest() }

// Repo Bundle registry and Repository
func (o *Bundle) Repo() string { return o.plainImg.Repo() }

// Tag Bundle Tag
func (o *Bundle) Tag() string { return o.plainImg.Tag() }

func (o *Bundle) updateCachedImageRefWithoutAnnotations(ref ImageRef) {
imgRef, found := o.cachedImageRefs.ImageRef(ref.Image)
Expand Down
3 changes: 2 additions & 1 deletion pkg/imgpkg/bundle/bundle_images_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func (o *Bundle) UpdateImageRefs(bundles []*Bundle) error {
for _, image := range imageRefsToProcess.ImageRefs() {
isBundle := false
for _, bundle := range bundles {
if bundle.DigestRef() == image.ImageRef.Image {
if bundle.Digest() == image.Digest() {
isBundle = true
image.ImageType = BundleImage
break
}
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/imgpkg/bundle/images_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func NewImageRefWithType(imgRef lockconfig.ImageRef, imageType ImageType) ImageR
return ImageRef{ImageRef: imgRef, IsBundle: &isBundle, Copiable: copiable, ImageType: imageType}
}

// Digest Image Digest
func (i ImageRef) Digest() string {
digest, err := name.NewDigest(i.Image)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency, ImageRef.Image '%s' should contain a digest", i.Image))
}

return digest.DigestStr()
}

func (i ImageRef) DeepCopy() ImageRef {
var isBundle *bool
if i.IsBundle != nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/imgpkg/plainimage/plain_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ func (i *PlainImage) DigestRef() string {
return i.parsedRef.Context().Name() + "@" + i.parsedDigest
}

// Digest Image Digest
func (i *PlainImage) Digest() string {
if i.parsedRef == nil {
panic("Unexpected usage of Digest(); call Fetch before")
}
if len(i.parsedDigest) == 0 {
panic("Unexpected usage of Digest(); call Fetch before")
}
return i.parsedDigest
}

func (i *PlainImage) Tag() string {
if i.parsedRef == nil {
panic("Unexpected usage of Tag(); call Fetch before")
Expand Down
79 changes: 79 additions & 0 deletions test/e2e/copy_from_bundle_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,85 @@ images:
require.NoError(t, env.Assert.ValidateImagesPresenceInRegistry(imagesToCheck))
})

t.Run("With Bundle with Nested Bundles, When Push, Copy to Repo, Copy to Tar, Copy to Repo1 and Copy to tar, should copy all Images", func(t *testing.T) {
env := helpers.BuildEnv(t)
imgpkg := helpers.Imgpkg{T: t, ImgpkgPath: env.ImgpkgPath}
defer env.Cleanup()
fakeRegBuilder := helpers.NewFakeRegistry(t, env.Logger)
testDir := env.Assets.CreateTempFolder("nested-bundles-tar-test")
tarFilePath := filepath.Join(testDir, "bundle.tar")
secondTarFilePath := filepath.Join(testDir, "second-bundle.tar")

imgRef, err := regname.ParseReference(env.Image)
require.NoError(t, err)
imgRefFakeReg, err := regname.ParseReference(fakeRegBuilder.ReferenceOnTestServer(imgRef.Identifier()))
require.NoError(t, err)

var img1DigestRef, img2DigestRef, img1Digest, img2Digest string
logger.Section("create 2 simple images", func() {
img1DigestRef = imgRefFakeReg.Context().Name() + "-img1"
img1Digest = env.ImageFactory.PushSimpleAppImageWithRandomFile(imgpkg, img1DigestRef)
img1DigestRef = img1DigestRef + img1Digest

img2DigestRef = imgRefFakeReg.Context().Name() + "-img2"
img2Digest = env.ImageFactory.PushSimpleAppImageWithRandomFile(imgpkg, img2DigestRef)
img2DigestRef = img2DigestRef + img2Digest
})

nestedBundle := imgRefFakeReg.Context().Name() + "-bundle-nested"
nestedBundleDigest := ""
logger.Section("create nested bundle", func() {
imageLockYAML := fmt.Sprintf(`---
apiVersion: imgpkg.carvel.dev/v1alpha1
kind: ImagesLock
images:
- image: %s
- image: %s
`, img1DigestRef, img2DigestRef)

bundleDir := env.BundleFactory.CreateBundleDir(helpers.BundleYAML, imageLockYAML)
out := imgpkg.Run([]string{"push", "--tty", "-b", nestedBundle, "-f", bundleDir})
nestedBundleDigest = fmt.Sprintf("@%s", helpers.ExtractDigest(t, out))
})

outerBundle := imgRef.Context().Name() + "-bundle-outer"
outerBundleDigest := ""
logger.Section("create outer bundle", func() {
imageLockYAML := fmt.Sprintf(`---
apiVersion: imgpkg.carvel.dev/v1alpha1
kind: ImagesLock
images:
- image: %s
- image: %s
`, nestedBundle+nestedBundleDigest, img1DigestRef)

bundleDir := env.BundleFactory.CreateBundleDir(helpers.BundleYAML, imageLockYAML)
out := imgpkg.Run([]string{"push", "--tty", "-b", outerBundle, "-f", bundleDir})
outerBundleDigest = fmt.Sprintf("@%s", helpers.ExtractDigest(t, out))
})
relocatedOuterBundle := outerBundle + "-relocated"
logger.Section("relocate OuterBundle", func() {
imgpkg.Run([]string{"copy", "-b", outerBundle + outerBundleDigest, "--to-repo", relocatedOuterBundle})
})

var outputTarExport string
logger.Section("export full bundle to tar", func() {
outputTarExport = imgpkg.Run([]string{"copy", "-b", relocatedOuterBundle + outerBundleDigest, "--to-tar", tarFilePath})
})

logger.Section("import bundle to new repository", func() {
imgpkg.Run([]string{"copy", "--tar", tarFilePath, "--to-repo", env.RelocationRepo})
})

var secondOutputTarExport string
logger.Section("export again to a tar", func() {
secondOutputTarExport = imgpkg.Run([]string{"copy", "-b", env.RelocationRepo + outerBundleDigest, "--to-tar", secondTarFilePath})
})

require.Contains(t, outputTarExport, "exporting 4 images...")
require.Contains(t, secondOutputTarExport, "exporting 4 images...")
})

t.Run("when bundle contains only images it copies all images", func(t *testing.T) {
env := helpers.BuildEnv(t)
imgpkg := helpers.Imgpkg{T: t, ImgpkgPath: env.ImgpkgPath}
Expand Down

0 comments on commit acb7d5d

Please sign in to comment.