外部化/重用 Helm _helpers
Externalize / reuse Helm _helpers
我有几个不同形状和用途的应用程序,每个应用程序都有自己的 templates/
文件夹,由 helm create chart
生成。每个应用程序中的模板都非常不同,足以证明将它们作为一次性使用是合理的。但是,_helpers.tpl
对所有这些都是相同的。我想要 externalize/reuse 这个 _helpers.tpl
模板,这样我就不需要在每个应用程序中都有它的副本了。
我目前看到的是这样的:
App1
|--app (random source code crap, irrelevant)
|--chart
|---templates
|------_helpers.tpl
|------ deployment.yaml
|------ other unique templates
App2
|--app (random source code crap, irrelevant)
|--chart
|---templates
|------_helpers.tpl
|------ deployment.yaml
|------ other unique templates
我想把这个_helpers.tpl
集中起来,这样我就不需要维护它的N个版本了。我正在想象这样的事情,但我对任何事情都持开放态度:
Common
|--chart
|----templates
|------ _helpers.tpl (I live here now and was removed from the 2 Apps below)
App1
|-- app (random source code crap, irrelevant)
|-- chart
|--- templates
|------ deployment.yaml
|------ other unique templates
App2
|-- app (random source code crap, irrelevant)
|-- chart
|--- templates
|------ deployment.yaml
|------ other unique templates
我已经尝试使用指向 AppN/chart/templates/_helper.tpl
到 Common/chart/templates/_helper.tpl
的符号链接来执行此操作,但这显然很糟糕,我想有一种内置的方法可以做到这一点,但我只是找不到。
即使 AppN/chart/templates/_helpers.tpl
只需要存在以读取 ../../_helpers.tpl
,这就足够了,但我不确定如何处理 YAML-y/Go-y 语法。
我通过将我的文件结构反转为类似以下内容来解决这个问题:
Parent App
|--Chart.yaml // new
|--values.yaml // new
|--templates // new
|----_helpers.tpl // automagically gets referenced in charts/*/templates
|--apps (source code stuff, irrelevant)
|--charts
|----App1
|------Chart.yaml
|------values.yaml
|------templates
|--------deployment.yaml (and others)
|----App2
|------Chart.yaml
|------values.yaml
|------templates
|--------deployment.yaml (and others)
更紧密地遵循此处概述的“子图”模式:https://helm.sh/docs/chart_template_guide/subcharts_and_globals/
尽管这些是独立的图表,但并不像这种结构那样依赖于某些父图表,这对我来说已经足够了。
如果一个 Helm 图表包含另一个作为子图表,则任何父图表或子图表中任何位置的任何 {{ define }}
模板对所有图表都是可见的 {{ include }}
d 或 {{ template }}
d。这意味着您可以创建一个仅包含助手的 library chart,其结构基本上与上面显示的结构完全相同
common
+-- Chart.yaml
\-- templates/
+-- _a_helper.tpl
\-- _another.tpl
这看起来像一个普通的 Helm chart,因为它有一个 Chart.yaml
文件和一个 templates
子目录,但在 templates
里面只有 _*.tpl
个文件而不是 *.yaml
个文件,这些文件只包含 {{ define }}
个顶级指令。
在您的应用程序图表中,您可以像使用任何其他图表依赖项一样使用库图表。如果这些都签入同一个存储库,您可以使用相对 file:
URL 指向公共图表。
# app1/Chart.yaml
dependencies:
- name: common
repository: file://../common/
在部署应用程序图表之前,您确实需要记住 运行 helm dep up
;这会在 charts
子目录中创建一个副本。
(如果您使用的是现代 Helm 3,则可以在 Chart.yaml
文件中使用 type: library
标记库图表。如果您使用的是过时的 Helm 2,则需要删除依赖项在单独的 requirements.yaml
文件中。您无法使用值配置库图表;如果应用程序图表调用 {{ include "common.some.template" . }}
,它将传递父图表的 .Values
视图作为模板参数。)
我有几个不同形状和用途的应用程序,每个应用程序都有自己的 templates/
文件夹,由 helm create chart
生成。每个应用程序中的模板都非常不同,足以证明将它们作为一次性使用是合理的。但是,_helpers.tpl
对所有这些都是相同的。我想要 externalize/reuse 这个 _helpers.tpl
模板,这样我就不需要在每个应用程序中都有它的副本了。
我目前看到的是这样的:
App1
|--app (random source code crap, irrelevant)
|--chart
|---templates
|------_helpers.tpl
|------ deployment.yaml
|------ other unique templates
App2
|--app (random source code crap, irrelevant)
|--chart
|---templates
|------_helpers.tpl
|------ deployment.yaml
|------ other unique templates
我想把这个_helpers.tpl
集中起来,这样我就不需要维护它的N个版本了。我正在想象这样的事情,但我对任何事情都持开放态度:
Common
|--chart
|----templates
|------ _helpers.tpl (I live here now and was removed from the 2 Apps below)
App1
|-- app (random source code crap, irrelevant)
|-- chart
|--- templates
|------ deployment.yaml
|------ other unique templates
App2
|-- app (random source code crap, irrelevant)
|-- chart
|--- templates
|------ deployment.yaml
|------ other unique templates
我已经尝试使用指向 AppN/chart/templates/_helper.tpl
到 Common/chart/templates/_helper.tpl
的符号链接来执行此操作,但这显然很糟糕,我想有一种内置的方法可以做到这一点,但我只是找不到。
即使 AppN/chart/templates/_helpers.tpl
只需要存在以读取 ../../_helpers.tpl
,这就足够了,但我不确定如何处理 YAML-y/Go-y 语法。
我通过将我的文件结构反转为类似以下内容来解决这个问题:
Parent App
|--Chart.yaml // new
|--values.yaml // new
|--templates // new
|----_helpers.tpl // automagically gets referenced in charts/*/templates
|--apps (source code stuff, irrelevant)
|--charts
|----App1
|------Chart.yaml
|------values.yaml
|------templates
|--------deployment.yaml (and others)
|----App2
|------Chart.yaml
|------values.yaml
|------templates
|--------deployment.yaml (and others)
更紧密地遵循此处概述的“子图”模式:https://helm.sh/docs/chart_template_guide/subcharts_and_globals/
尽管这些是独立的图表,但并不像这种结构那样依赖于某些父图表,这对我来说已经足够了。
如果一个 Helm 图表包含另一个作为子图表,则任何父图表或子图表中任何位置的任何 {{ define }}
模板对所有图表都是可见的 {{ include }}
d 或 {{ template }}
d。这意味着您可以创建一个仅包含助手的 library chart,其结构基本上与上面显示的结构完全相同
common
+-- Chart.yaml
\-- templates/
+-- _a_helper.tpl
\-- _another.tpl
这看起来像一个普通的 Helm chart,因为它有一个 Chart.yaml
文件和一个 templates
子目录,但在 templates
里面只有 _*.tpl
个文件而不是 *.yaml
个文件,这些文件只包含 {{ define }}
个顶级指令。
在您的应用程序图表中,您可以像使用任何其他图表依赖项一样使用库图表。如果这些都签入同一个存储库,您可以使用相对 file:
URL 指向公共图表。
# app1/Chart.yaml
dependencies:
- name: common
repository: file://../common/
在部署应用程序图表之前,您确实需要记住 运行 helm dep up
;这会在 charts
子目录中创建一个副本。
(如果您使用的是现代 Helm 3,则可以在 Chart.yaml
文件中使用 type: library
标记库图表。如果您使用的是过时的 Helm 2,则需要删除依赖项在单独的 requirements.yaml
文件中。您无法使用值配置库图表;如果应用程序图表调用 {{ include "common.some.template" . }}
,它将传递父图表的 .Values
视图作为模板参数。)