Skip to content

Commit

Permalink
Merge pull request #2 from vmware-labs/info-command
Browse files Browse the repository at this point in the history
Added info command
  • Loading branch information
mpermar committed Aug 18, 2023
2 parents e3b6271 + e174992 commit d2d9b47
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,82 @@ helm dt images push examples/mariadb

INFO[0033] All images pushed successfully
```
### Getting information about a wrapped chart

It is sometimes useful to obtain information about a wrapped chart before unwrapping it. For this purpose, you can use the info command:

```sh
helm dt info wordpress-16.1.24.wrap.tgz
» Wrap Information
Chart: wordpress
Version: 16.1.24
» Metadata
- generatedBy: Distribution Tooling for Helm
- generatedAt: 2023-08-18T12:52:55.824345304Z
» Images
docker.io/bitnami/apache-exporter:0.13.4-debian-11-r12 (linux/amd64, linux/arm64)
docker.io/bitnami/bitnami-shell:11-debian-11-r132 (linux/amd64, linux/arm64)
docker.io/bitnami/wordpress:6.2.2-debian-11-r26 (linux/amd64, linux/arm64)
docker.io/bitnami/bitnami-shell:11-debian-11-r123 (linux/amd64, linux/arm64)
docker.io/bitnami/mariadb:10.11.4-debian-11-r0 (linux/amd64, linux/arm64)
docker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r125 (linux/amd64, linux/arm64)
docker.io/bitnami/bitnami-shell:11-debian-11-r130 (linux/amd64, linux/arm64)
docker.io/bitnami/memcached:1.6.21-debian-11-r4 (linux/amd64, linux/arm64)
docker.io/bitnami/memcached-exporter:0.13.0-debian-11-r8 (linux/amd64, linux/arm64)
```

If you are interested in getting the image digests, you can use the `--detailed` flag:

```sh
helm dt info --detailed wordpress-16.1.24.wrap.tgz
» Wrap Information
Chart: wordpress
Version: 16.1.24
» Metadata
- generatedBy: Distribution Tooling for Helm
- generatedAt: 2023-08-18T12:52:55.824345304Z
» Images
» wordpress/apache-exporter
Image: docker.io/bitnami/apache-exporter:0.13.4-debian-11-r12
Digests
- Arch: linux/amd64
Digest: sha256:0b4373c3571d5640320b68f8d296c0a4eaf7704947214640b77528bb4d79d23c
- Arch: linux/arm64
Digest: sha256:895ba569e4db3188798e445fe3be2e4da89fd85cb8ae0c5ef0bd2a67cfe4305c
...
» mariadb/bitnami-shell
Image: docker.io/bitnami/bitnami-shell:11-debian-11-r123
Digests
- Arch: linux/amd64
Digest: sha256:13d8883d4f40612e8a231c5d9fa8c4efa74d2a62f0a1991f20fc32c5debdd2b1
- Arch: linux/arm64
Digest: sha256:74579dc63b3ae7d8ec21a6ffcd47d16781582fef8dd5a28e77844fcbcb1072c1
...
```

It is also possible to get a YAML dump if the `Images.lock` in case you need to feed it to another process:

```sh
helm dt info --yaml wordpress-16.1.24.wrap.tgz
apiversion: v0
kind: ImagesLock
metadata:
generatedAt: "2023-08-18T12:52:55.824345304Z"
generatedBy: Distribution Tooling for Helm
chart:
name: wordpress
version: 16.1.24
images:
- name: apache-exporter
image: docker.io/bitnami/apache-exporter:0.13.4-debian-11-r12
chart: wordpress
digests:
- digest: sha256:0b4373c3571d5640320b68f8d296c0a4eaf7704947214640b77528bb4d79d23c
arch: linux/amd64
- digest: sha256:895ba569e4db3188798e445fe3be2e4da89fd85cb8ae0c5ef0bd2a67cfe4305c
arch: linux/arm64
...
```

### Annotating a chart (EXPERIMENTAL)

Expand Down
119 changes: 119 additions & 0 deletions cmd/dt/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"archive/tar"
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/vmware-labs/distribution-tooling-for-helm/imagelock"
"github.com/vmware-labs/distribution-tooling-for-helm/internal/log"
"github.com/vmware-labs/distribution-tooling-for-helm/utils"
)

var infoCmd = newInfoCmd()

func readLockFromWrap(chartPath string) (*imagelock.ImagesLock, error) {
var lock *imagelock.ImagesLock
var err error
if isTar, _ := utils.IsTarFile(chartPath); isTar {
if err := utils.FindFileInTar(context.Background(), chartPath, "Images.lock", func(tr *tar.Reader) error {
lock, err = imagelock.FromYAML(tr)
return err
}, utils.TarConfig{StripComponents: 1}); err != nil {
return nil, err
}
if lock == nil {
return nil, fmt.Errorf("failed to find Images.lock file inside wrap")
}
return lock, nil
}

f, err := getImageLockFilePath(chartPath)
if err != nil {
return nil, fmt.Errorf("failed to find Images.lock file for Helm chart %q: %v", chartPath, err)
}
return imagelock.FromYAMLFile(f)
}

func newInfoCmd() *cobra.Command {
var yamlFormat bool
var showDetails bool

cmd := &cobra.Command{
Use: "info FILE",
Short: "shows info of a wrapped chart",
Long: `Shows information of a wrapped Helm chart, including the bundled images and chart metadata`,
Example: ` # Show information of a wrapped Helm chart
$ dt info mariadb-12.2.8.wrap.tgz`,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
chartPath := args[0]
l := getLogger()
_, _ = chartPath, l
if !utils.FileExists(chartPath) {
return fmt.Errorf("wrap file %q does not exist", chartPath)
}
lock, err := readLockFromWrap(chartPath)
if err != nil {
return err
}
if yamlFormat {
if err := lock.ToYAML(os.Stdout); err != nil {
return fmt.Errorf("failed to write Images.lock yaml representation: %v", err)
}
} else {

_ = l.Section("Wrap Information", func(l log.SectionLogger) error {
l.Printf("Chart: %s", lock.Chart.Name)
l.Printf("Version: %s", lock.Chart.Version)
_ = l.Section("Metadata", func(l log.SectionLogger) error {
for k, v := range lock.Metadata {
l.Printf("- %s: %s", k, v)

}
return nil
})
_ = l.Section("Images", func(l log.SectionLogger) error {
for _, img := range lock.Images {
if showDetails {
_ = l.Section(fmt.Sprintf("%s/%s", img.Chart, img.Name), func(l log.SectionLogger) error {
l.Printf("Image: %s", img.Image)
if showDetails {
l.Printf("Digests")
for _, digest := range img.Digests {
l.Printf("- Arch: %s", digest.Arch)
l.Printf(" Digest: %s", digest.Digest)
}
}
return nil
})
} else {
platforms := make([]string, 0)
for _, digest := range img.Digests {
platforms = append(platforms, digest.Arch)
}
l.Printf("%s (%s)", img.Image, strings.Join(platforms, ", "))
}
}
return nil
})
return nil
})
}
return nil
},
}
cmd.PersistentFlags().BoolVar(&yamlFormat, "yaml", yamlFormat, "Show report in YAML format")
cmd.PersistentFlags().BoolVar(&showDetails, "detailed", showDetails, "When using the printable report, add more details about the bundled images")

return cmd
}

func init() {
rootCmd.AddCommand(infoCmd)
}

0 comments on commit d2d9b47

Please sign in to comment.