首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >外部化/重用Helm _helpers

外部化/重用Helm _helpers
EN

Stack Overflow用户
提问于 2021-05-03 16:52:44
回答 2查看 715关注 0票数 0

我有几个不同形状和用途的应用程序,每个应用程序都有自己的templates/文件夹,由helm create chart生成。每个应用程序中的模板都有足够的不同,可以证明它们是一次性的。然而,_helpers.tpl对于所有这些都是相同的。我想外化/重用这个_helpers.tpl模板,这样我就不需要在每个应用程序中都有它的副本了。

我目前的情况如下:

代码语言:javascript
复制
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个版本了。我在想象这样的事情,但我对任何事情都敞开心扉:

代码语言:javascript
复制
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语法,我不知道如何处理这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-03 19:43:45

最后,我将我的文件结构转换为如下内容,从而解决了这个问题:

代码语言:javascript
复制
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)

它更接近于下面列出的“子图表”模式:全球/

尽管这些是独立的图表,而不是依赖于像这种结构这样的父图表,但这对我来说已经足够好了。

票数 1
EN

Stack Overflow用户

发布于 2021-05-04 23:07:00

如果一个Helm图表包含另一个作为子图表,则任何父图表或子图表中的任何{{ define }}模板对所有图表都是可见的,即{{ include }}d或{{ template }}d。

代码语言:javascript
复制
common
+-- Chart.yaml
\-- templates/
  +-- _a_helper.tpl
  \-- _another.tpl

这看起来像一个普通的Helm图表,因为它有一个Chart.yaml文件和一个templates子目录,但是templates内部只有_*.tpl文件,而不是*.yaml文件,这些文件只包含{{ define }}顶级指令。

在应用程序图表中,可以像使用任何其他图表依赖项一样使用库图表。如果所有这些都被签入同一个存储库中,则可以使用相对file: URL指向公共图表。

代码语言:javascript
复制
# app1/Chart.yaml
dependencies:
  - name: common
    repository: file://../common/

在部署应用程序图表之前,您确实需要记住运行helm dep up;这将在charts子目录中生成一个副本。

(如果使用现代Helm 3,则可以在type: library文件中使用Chart.yaml标记库图表。如果您使用的是过时的Helm 2,则依赖项需要放在单独的requirements.yaml文件中。您无法使用值配置库图表;如果应用程序图表调用{{ include "common.some.template" . }},它将传递父图表的.Values视图作为模板参数的一部分。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67372801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档