为新类型扩展 kustomize 图像转换器

Cra*_*ger 5 kubernetes kustomize

有没有办法扩展 kustomize 图像转换器以将更多键识别为图像说明符?就像nameReference变压器namePrefixnameSuffix变压器所做的那样。

Kustomizeimages:转换器对于 k8s 清单中的图像替换和注册表重命名非常有用。

它只支持嵌入的类型PodTemplate,也许还有一些硬编码的类型。不使用的 CRDPodTemplate不会被处理,尽管它们常见。示例包括kube-prometheus PrometheusAlertManager资源以及opentelemetry-operator OpenTelemetryCollector资源。

因此,您必须维护一堆混乱的战略合并或 json 补丁,以使用受信任的注册表等为此类图像添加前缀。


这是目前问题的一个例子。假设我必须部署以mytrusted.registry变压器images:列表为前缀的所有内容。为了简洁起见,我将使用一个虚拟图像来替换所有匹配的图像MATCHED,因此我不必将它们全部列出:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - "https://github.com/prometheus-operator/kube-prometheus"
images:
  - name: "(.*)"
    newName: "MATCHED"
    newTag: "fake"
Run Code Online (Sandbox Code Playgroud)

您可能期望结果中唯一的图像是“MATCHED:fake”,但实际上:

$ kustomize build  | grep 'image: .*' | sort | uniq -c
     12         image: MATCHED:fake
      1   image: quay.io/prometheus/alertmanager:v0.24.0
      1   image: quay.io/prometheus/prometheus:v2.34.0
Run Code Online (Sandbox Code Playgroud)

kind: Prometheus和资源中的图像kind: AlertManager不会匹配,因为它们不是PodTemplate.

您必须为这些内容编写一个自定义补丁,这会造成如下kustomization.yaml内容的混乱:

patches:
  - path: prometheus_image.yaml
    target:
      kind: Prometheus
  - path: alertmanager_image.yaml
    target:
      kind: Alertmanager
Run Code Online (Sandbox Code Playgroud)

prometheus_image.yaml

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
      name: ignored
spec:
      image: "MATCHED:fake"
Run Code Online (Sandbox Code Playgroud)

alertmanager_image.yaml

apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
      name: ignored
spec:
      image: "MATCHED:fake"
Run Code Online (Sandbox Code Playgroud)

在我看来,这是可怕的。

想要做的是告诉Kustomize的图像转换器它,就像它可以使用自定义配置映射生成器等进行扩展,例如以下基于现有转换器建模的不受支持和虚构的伪代码nameReference

imageReference:
  - kind: Prometheus
    fieldSpecs:
      - spec/image
Run Code Online (Sandbox Code Playgroud)

Cra*_*ger 5

写完这篇文章后,我终于偶然发现了答案:Kustomize 确实支持图像转换器配置

表达上述内容的正确方法是image_transformer_config.yaml包含以下内容的文件:

images:
  - path: spec/image
    kind: Prometheus
  - path: spec/image
    kind: Alertmanager
Run Code Online (Sandbox Code Playgroud)

以及kustomization.yaml引用它的条目,例如

configurations:
  - image_transformer_config.yaml
Run Code Online (Sandbox Code Playgroud)

当作为 a 导入时,这似乎工作正常Component

变压器文档甚至指出了这一点,所以我将责怪这个人是盲目的。