从 helm 图表中提取 docker 个图像

extract docker images from helm chart

是否有任何方法可以通过 python 从 helm chart 中提取 docker 图像?

我想避免任何 :

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

bash 个提取此信息的脚本。

如果舵图包含两个图像(例如),我想提取这些图像

示例:

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

helm template 会将 Helm 图表的内容呈现为 YAML 文件并将其打印到标准输出。一个快速而肮脏的答案是

helm template helm-chart | grep 'image:'

但您也可以使用 yq 等工具基于 YAML 结构进行查询。

您必须使用某种 Helm 命令来呈现模板,因为它很常见,可以指定

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

这取决于输入文件。另一方面,您可以使用常用的 Helm --set-f 选项来指定值,因此如果您要在不同的环境(或不同的构建,或者只包括一些部署)中使用不同的图像基于设置)您将准确反映将部署的内容。

mike has provided a fantastic answer in a blog post here,所有功劳都归于他。

要列出给定 Helm 图表中的所有唯一映像:

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

helm template .

这假定您位于包含 Helm 图表的目录中。 template 命令打印出通常通过 helm install 创建的所有 Kubernetes 对象。其输出格式为 yaml。

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

yq 是流行的 jq 命令行工具的变体。 jq 期望处理 JSON 流,而 yq 允许您对 yaml 执行相同的操作。

神奇之处在于 yq 提供的过滤器。过滤器作用于某些输入并执行某些操作以产生输出。在这种情况下,我们使用递归下降运算符来查找所有名为 image 的字段。这 ?在.image?指示 yq 应生成图像值或 null(如果不存在)。将其传送到 select(.) 将忽略所有空值,最终返回一个 new-line 分隔的图像字符串。

我有一个类似的用例并构建了一个工具来提取图像、标记并将它们推送到不同的注册表。

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

  1. 创建一个包含所有图表的 yaml 文件
# 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. 运行 通过传递配置文件的工具 - helm_image_mirror -c config.yaml