From e75c4150c0c53e047bb175f37bc47f33351a2f5e Mon Sep 17 00:00:00 2001 From: kumari tanushree Date: Mon, 26 Feb 2024 21:46:01 +0530 Subject: [PATCH] Added flag --layers to enable/disable funtionality of showing each layer of image Signed-off-by: kumari tanushree --- pkg/imgpkg/cmd/describe.go | 19 +++++++++++++------ pkg/imgpkg/v1/describe.go | 31 ++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/pkg/imgpkg/cmd/describe.go b/pkg/imgpkg/cmd/describe.go index a7298e7e6..cd2c2206f 100644 --- a/pkg/imgpkg/cmd/describe.go +++ b/pkg/imgpkg/cmd/describe.go @@ -30,6 +30,7 @@ type DescribeOptions struct { Concurrency int OutputType string + Layers bool IncludeCosignArtifacts bool } @@ -53,6 +54,7 @@ func NewDescribeCmd(o *DescribeOptions) *cobra.Command { o.RegistryFlags.Set(cmd) cmd.Flags().IntVar(&o.Concurrency, "concurrency", 5, "Concurrency") cmd.Flags().StringVarP(&o.OutputType, "output-type", "o", "text", "Type of output possible values: [text, yaml]") + cmd.Flags().BoolVarP(&o.Layers, "layers", "", true, "Retrieve image layers info (Default: false)") cmd.Flags().BoolVar(&o.IncludeCosignArtifacts, "cosign-artifacts", true, "Retrieve cosign artifact information (Default: true)") return cmd } @@ -72,6 +74,7 @@ func (d *DescribeOptions) Run() error { Logger: levelLogger, Concurrency: d.Concurrency, IncludeCosignArtifacts: d.IncludeCosignArtifacts, + Layers: d.Layers, }, d.RegistryFlags.AsRegistryOpts()) if err != nil { @@ -138,9 +141,11 @@ func (p bundleTextPrinter) printerRec(description v1.Description, originalLogger indentLogger.Logf("- Image: %s\n", b.Image) indentLogger.Logf(" Type: Bundle\n") indentLogger.Logf(" Origin: %s\n", b.Origin) - indentLogger.Logf(" Layers:\n") - for _, d := range b.Layers { - indentLogger.Logf(" - Digest: %s\n", d.Digest) + if len(b.Layers) > 0 { + indentLogger.Logf(" Layers:\n") + for _, d := range b.Layers { + indentLogger.Logf(" - Digest: %s\n", d.Digest) + } } annotations := b.Annotations @@ -164,9 +169,11 @@ func (p bundleTextPrinter) printerRec(description v1.Description, originalLogger if image.ImageType == bundle.ContentImage { indentLogger.Logf(" Origin: %s\n", image.Origin) } - indentLogger.Logf(" Layers:\n") - for _, d := range image.Layers { - indentLogger.Logf(" - Digest: %s\n", d.Digest) + if len(image.Layers) > 0 { + indentLogger.Logf(" Layers:\n") + for _, d := range image.Layers { + indentLogger.Logf(" - Digest: %s\n", d.Digest) + } } annotations := image.Annotations p.printAnnotations(annotations, util.NewIndentedLogger(indentLogger)) diff --git a/pkg/imgpkg/v1/describe.go b/pkg/imgpkg/v1/describe.go index ac0359caa..c35ceba32 100644 --- a/pkg/imgpkg/v1/describe.go +++ b/pkg/imgpkg/v1/describe.go @@ -72,6 +72,7 @@ type DescribeOpts struct { Logger bundle.Logger Concurrency int IncludeCosignArtifacts bool + Layers bool } // SignatureFetcher Interface to retrieve signatures associated with Images @@ -116,7 +117,7 @@ func DescribeWithRegistryAndSignatureFetcher(bundleImage string, opts DescribeOp topBundle := refWithDescription{ imgRef: bundle.NewBundleImageRef(lockconfig.ImageRef{Image: newBundle.DigestRef()}), } - return topBundle.DescribeBundle(allBundles) + return topBundle.DescribeBundle(allBundles, opts.Layers) } type refWithDescription struct { @@ -124,20 +125,26 @@ type refWithDescription struct { bundle Description } -func (r *refWithDescription) DescribeBundle(bundles []*bundle.Bundle) (Description, error) { +func (r *refWithDescription) DescribeBundle(bundles []*bundle.Bundle, layers bool) (Description, error) { var visitedImgs map[string]refWithDescription - return r.describeBundleRec(visitedImgs, r.imgRef, bundles) + return r.describeBundleRec(visitedImgs, r.imgRef, bundles, layers) } -func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDescription, currentBundle bundle.ImageRef, bundles []*bundle.Bundle) (Description, error) { +func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDescription, currentBundle bundle.ImageRef, bundles []*bundle.Bundle, showLayers bool) (Description, error) { desc, wasVisited := visitedImgs[currentBundle.Image] + var ( + layers []Layers + err error + ) if wasVisited { return desc.bundle, nil } - layers, err := getImageLayersInfo(currentBundle.Image) - if err != nil { - return desc.bundle, err + if showLayers { + layers, err = getImageLayersInfo(currentBundle.Image) + if err != nil { + return desc.bundle, err + } } desc = refWithDescription{ @@ -176,7 +183,7 @@ func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDes } if *ref.IsBundle { - bundleDesc, err := r.describeBundleRec(visitedImgs, ref, bundles) + bundleDesc, err := r.describeBundleRec(visitedImgs, ref, bundles, showLayers) if err != nil { return desc.bundle, err } @@ -192,9 +199,11 @@ func (r *refWithDescription) describeBundleRec(visitedImgs map[string]refWithDes if err != nil { return desc.bundle, fmt.Errorf("Internal inconsistency: image %s should be fully resolved", ref.Image) } - layers, err = getImageLayersInfo(ref.Image) - if err != nil { - return desc.bundle, err + if showLayers { + layers, err = getImageLayersInfo(ref.Image) + if err != nil { + return desc.bundle, err + } } desc.bundle.Content.Images[digest.DigestStr()] = ImageInfo{ Image: ref.PrimaryLocation(),