简体   繁体   English

从 helm chart 中提取 docker 图像

[英]extract docker images from helm chart

Is there any way to extract docker images from the helm chart via python?有没有办法通过python从舵图中提取docker图像?

I would like to avoid any :我想避免任何:

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

bash scripts to extract this information. bash 脚本来提取此信息。

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

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 图表中的所有唯一图像:

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

helm template .

This assumes you are in a directory containining a Helm chart.这假设您位于包含 Helm 图表的目录中。 The template command prints out all of the Kubernetes objects that would normally be created via helm install. template 命令打印出通常通过 helm install 创建的所有 Kubernetes 对象。 Its output is formatted in yaml.其输出格式为 yaml。

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

yq is a variant of the popular jq command line tool. yq 是流行的 jq 命令行工具的变体。 Whereas jq expects to deal with JSON streams, yq allows you to do the same with yaml. jq 希望处理 JSON 流,而 yq 允许您对 yaml 执行相同的操作。

The magic is in the filters that yq provides.神奇之处在于 yq 提供的过滤器。 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.在这种情况下,我们使用递归下降运算符来查找所有名为 image 的字段。 The ?这 ? in .image?在.image? indicates that yq should produce either the image value or null if it doesn't exist.表示 yq 应该生成图像值或 null 如果它不存在。 Piping that to select(.) will ignore all null values, finally returning a new-line delimited string of the images we're after.将其传递给 select(.) 将忽略所有空值,最终返回我们所追求的图像的换行分隔字符串。

helm template will render the contents of a Helm chart to YAML files and print it to stdout. helm template会将 Helm 图表的内容呈现为 YAML 文件并将其打印到标准输出。 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.但您也可以使用yq 之类的工具根据 YAML 结构进行查询。

You do have to use some sort of Helm command to render the template, since it's common enough to specify things like您确实必须使用某种 Helm 命令来呈现模板,因为它很常见,可以指定诸如

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.另一方面是您可以使用通常的 Helm --set-f选项来指定值,因此如果您要在不同环境(或不同构建,或仅包含基于设置的一些部署)中使用不同的图像将准确反映将要部署的内容。

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 https://github.com/shashankv02/helm-image-mirror

  1. Create a yaml file containing all the charts创建一个包含所有图表的 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. Run the tool by passing the config file - helm_image_mirror -c config.yaml通过传递配置文件运行该工具 - helm_image_mirror -c config.yaml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM