Skip to content

Commit

Permalink
added func to calculate image layers sha in e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: kumari tanushree <ktanushree@vmware.com>
  • Loading branch information
kumari tanushree committed Jan 9, 2024
1 parent 8eee075 commit 5121899
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 102 deletions.
4 changes: 2 additions & 2 deletions pkg/imgpkg/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (p bundleTextPrinter) printerRec(description v1.Description, originalLogger
indentLogger.Logf(" Origin: %s\n", b.Origin)
indentLogger.Logf(" Layers:\n")
for _, d := range b.Layers {
indentLogger.Logf(" - Digest: %s\n", d)
indentLogger.Logf(" - Digest: %s\n", d.Digest)
}
annotations := b.Annotations

Expand All @@ -166,7 +166,7 @@ func (p bundleTextPrinter) printerRec(description v1.Description, originalLogger
}
indentLogger.Logf(" Layers:\n")
for _, d := range image.Layers {
indentLogger.Logf(" - Digest: %s\n", d)
indentLogger.Logf(" - Digest: %s\n", d.Digest)
}
annotations := image.Annotations
p.printAnnotations(annotations, util.NewIndentedLogger(indentLogger))
Expand Down
107 changes: 52 additions & 55 deletions pkg/imgpkg/v1/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ type Metadata struct {
Websites []Website `json:"websites,omitempty"`
}

// Layers image layers info
type Layers struct {
Digest string `json:"digest,omitempty"`
}

// ImageInfo URLs where the image can be found as well as annotations provided in the Images Lock
type ImageInfo struct {
Image string `json:"image,omitempty"`
Origin string `json:"origin,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
ImageType bundle.ImageType `json:"imageType"`
Error string `json:"error,omitempty"`
Layers []string `json:"layers,omitempty"`
Layers []Layers `json:"layers,omitempty"`
}

// Content Contents present in a Bundle
Expand All @@ -59,7 +64,7 @@ type Description struct {
Annotations map[string]string `json:"annotations,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Content Content `json:"content"`
Layers []string `json:"layers,omitempty"`
Layers []Layers `json:"layers,omitempty"`
}

// DescribeOpts Options used when calling the Describe function
Expand Down Expand Up @@ -111,48 +116,30 @@ func DescribeWithRegistryAndSignatureFetcher(bundleImage string, opts DescribeOp
topBundle := refWithDescription{
imgRef: bundle.NewBundleImageRef(lockconfig.ImageRef{Image: newBundle.DigestRef()}),
}
return topBundle.DescribeBundle(allBundles), nil
return topBundle.DescribeBundle(allBundles)
}

type refWithDescription struct {
imgRef bundle.ImageRef
bundle Description
}

func (r *refWithDescription) DescribeBundle(bundles []*bundle.Bundle) Description {
func (r *refWithDescription) DescribeBundle(bundles []*bundle.Bundle) (Description, error) {
var visitedImgs map[string]refWithDescription
return r.describeBundleRec(visitedImgs, r.imgRef, bundles)
}

func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDescription, currentBundle bundle.ImageRef, bundles []*bundle.Bundle) Description {
func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDescription, currentBundle bundle.ImageRef, bundles []*bundle.Bundle) (Description, error) {
desc, wasVisited := visitedImgs[currentBundle.Image]
if wasVisited {
return desc.bundle
}

layers := []string{}
parsedImgRef, err := regname.ParseReference(currentBundle.Image, regname.WeakValidation)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", currentBundle.Image, err.Error()))
}

v1Img, err := remote.Image(parsedImgRef, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", currentBundle.Image, err.Error()))
return desc.bundle, nil
}

imgLayers, err := v1Img.Layers()
layers, err := getImageLayersInfo(currentBundle.Image)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", currentBundle.Image, err.Error()))
return desc.bundle, err
}

for _, imgLayer := range imgLayers {
digHash, err := imgLayer.Digest()
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", currentBundle.Image, err.Error()))
}
layers = append(layers, digHash.String())
}
desc = refWithDescription{
imgRef: currentBundle,
bundle: Description{
Expand All @@ -175,57 +162,40 @@ func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDes
}
}
if newBundle == nil {
panic(fmt.Sprintf("Internal consistency: bundle with ref '%s' could not be found in list of bundles", currentBundle.PrimaryLocation()))
return desc.bundle, fmt.Errorf("Internal consistency: bundle with ref '%s' could not be found in list of bundles", currentBundle.PrimaryLocation())
}

imagesRefs := newBundle.ImagesRefsWithErrors()

sort.Slice(imagesRefs, func(i, j int) bool {
return imagesRefs[i].Image < imagesRefs[j].Image
})

for _, ref := range imagesRefs {
if ref.IsBundle == nil {
panic("Internal consistency: IsBundle after processing must always have a value")
return desc.bundle, fmt.Errorf("Internal consistency: IsBundle after processing must always have a value")
}

if *ref.IsBundle {
bundleDesc := r.describeBundleRec(visitedImgs, ref, bundles)
bundleDesc, err := r.describeBundleRec(visitedImgs, ref, bundles)
if err != nil {
return desc.bundle, err
}

digest, err := name.NewDigest(bundleDesc.Image)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved", bundleDesc.Image))
return desc.bundle, fmt.Errorf("Internal inconsistency: image %s should be fully resolved", bundleDesc.Image)
}
desc.bundle.Content.Bundles[digest.DigestStr()] = bundleDesc
} else {
if ref.Error == "" {
digest, err := name.NewDigest(ref.Image)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved", ref.Image))
}
layers = []string{}
parsedImgRef, err = regname.ParseReference(ref.Image, regname.WeakValidation)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", ref.Image, err.Error()))
}

v1Img, err = remote.Image(parsedImgRef, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", ref.Image, err.Error()))
return desc.bundle, fmt.Errorf("Internal inconsistency: image %s should be fully resolved", ref.Image)
}

imgLayers, err = v1Img.Layers()
layers, err = getImageLayersInfo(ref.Image)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", ref.Image, err.Error()))
}

for _, imgLayer := range imgLayers {
digHash, err := imgLayer.Digest()
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", ref.Image, err.Error()))
}
layers = append(layers, digHash.String())
return desc.bundle, err
}

desc.bundle.Content.Images[digest.DigestStr()] = ImageInfo{
Image: ref.PrimaryLocation(),
Origin: ref.Image,
Expand All @@ -242,5 +212,32 @@ func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDes
}
}

return desc.bundle
return desc.bundle, nil
}

func getImageLayersInfo(image string) ([]Layers, error) {
layers := []Layers{}
parsedImgRef, err := regname.ParseReference(image, regname.WeakValidation)
if err != nil {
return nil, fmt.Errorf("Error: %s in parsing image %s", err.Error(), image)
}

v1Img, err := remote.Image(parsedImgRef, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return nil, fmt.Errorf("Error: %s in getting remote access of image %s", err.Error(), image)
}

imgLayers, err := v1Img.Layers()
if err != nil {
return nil, fmt.Errorf("Error: %s in getting layers of image %s", err.Error(), image)
}

for _, imgLayer := range imgLayers {
digHash, err := imgLayer.Digest()
if err != nil {
return nil, fmt.Errorf("Error: %s in getting digest of layer's of image %s", err.Error(), image)
}
layers = append(layers, Layers{Digest: digHash.String()})
}
return layers, nil
}
Loading

0 comments on commit 5121899

Please sign in to comment.