extract docker images from helm chart

Viewed 8018

Is there any way to extract docker images from the helm chart via python?

I would like to avoid any :

helm install --dry-run helm-chart > log

bash scripts to extract this information.

If the helm chart contains two images (for example), I would like to extract these images

Example:

image: repo/image1:1.0     image1.yaml file
image: repo/image2:1.0     image2.yaml file
3 Answers

mike has provided a fantastic answer in a blog post here, all credit goes to him.

To list all unique images in a given Helm chart:

helm template . \
    | yq '..|.image? | select(.)' \
    | sort -u

helm template .

This assumes you are in a directory containining a Helm chart. The template command prints out all of the Kubernetes objects that would normally be created via helm install. Its output is formatted in yaml.

yq '..|.image? | select(.)'

yq is a variant of the popular jq command line tool. Whereas jq expects to deal with JSON streams, yq allows you to do the same with yaml.

The magic is in the filters that yq provides. Filters act on some input and performs some operation to produce an output. In this case, we’re using the recursive descent operator to find all fields named image. The ? in .image? indicates that yq should produce either the image value or null if it doesn’t exist. Piping that to select(.) will ignore all null values, finally returning a new-line delimited string of the images we’re after.

helm template will render the contents of a Helm chart to YAML files and print it to stdout. A quick-and-dirty answer would be

helm template helm-chart | grep 'image:'

but you could also use tools like yq to do queries based on the YAML structure.

You do have to use some sort of Helm command to render the template, since it's common enough to specify things like

image: 'some/image:{{ .Values.tag }}'

that depend on the input file. The flip side of this is that you can use the usual Helm --set or -f options to specify values, so if you would use different images in different environments (or different builds, or would only include some deployments based on settings) you'll get an accurate reflection of what would be deployed.

I had a similar use-case and built a tool to extract the image, tag and push them to different registries.

https://github.com/shashankv02/helm-image-mirror

  1. Create a yaml file containing all the charts
# repos contains helm repositories to be added
repos:
  # (optional) global username to be used for all repos
  username:
  # (optional) global password to be used for all repos
  password:
  add:
  - name: my_helm_repo
    remote: https://example.org/my_helm_repo
    # (optional) override global username for this repo
    username:
    # (optional) override global password for this repo
    password:


# charts from which the images must be parsed
charts:
  - repo: stable
    # name of the chart
    name: redis
    # (optional) overrides global fetch setting
    # from remote repository
    fetch: true
    values:
      # (optional) values to be passed to `--set` flag
      set:
      # (optional) values to be passed to `--set-string` flag
      set_str:
    versions:
      - version: 3.0.0
        # (optional) override fetch setting for version
        fetch: true
        # (optional) local_dir specifies the local directory in which the
        # chart tgz exists if fetch is set to false
        local_dir:
        # (optional) override values for version
        values:
          # (optional) values to be passed to `--set` flag
          set:
          # (optional) values to be passed to `--set-string` flag
          set_str:


# registries specifies the docker registries to which the images must be pushed
registries:
  - name: hub.docker.com
    # (optional) the local tagged images will be retained if true else deleted after
    # pushing the image to the registty
    retain: false
    # (optional) images will not be pushed to the registry if set to false
    push: true


# init_scripts specifies any initilization scripts that must be run before starting the
# program. This can be used to enhance the functionality that is not available natively.
init_scripts:
  - init.sh


# Global settings

# (optional) fetch specifies the global fetch policy for all charts.
# defauls to true if not specified. If fetch is set to false, local_dir
# setting must be set to specify the local directory in which the charts exists
fetch: true

# (optional) push specifies the global push policy for all regitries.
# defaults to true if not specified. If push is set to false, images
# will not be pushed to specified registries
push: true

# (optional) retain specifies the global retain policy for all images
# defaults to false if not specified. if retain is set to false, images
# that are downloaded and retagged are deleted after pushing them
# to the specified registries
retain: false
  1. Run the tool by passing the config file - helm_image_mirror -c config.yaml
Related