如何在HelmRelease上使用postRenderers的patchesStrategicMerge

69次阅读
没有评论

问题描述

在使用Flux配置时,成功部署了一个HelmRelease。现在他想对其进行补丁。但他不确定如何使用这种补丁格式。以下是他当前的配置(没有postRenderers时,他不会出现错误):

apiVersion: kind: HelmReleasemetadata:  name: ${elasticsearch_flx_app_name}  namespace: ${elasticsearch_flx_app_name}spec:  interval: 1m  install:    disableWait: true  upgrade:    disableWait: true  chart:    spec:      chart: search-guard-flx      version: "2.5.0"      sourceRef:        kind: HelmRepository        name: my-oci        namespace: flux-system  postRenderers:    - kustomize:        patchesStrategicMerge:          - apiVersion: apps/v1            kind: StatefulSet            metadata:              namespace: ${elasticsearch_flx_app_name}              labels:                app: ${elasticsearch_flx_app_name}-${cluster_env}                test_label: foo  values:    # ... values omitted for brevity

他得到的错误是:

preparing upgrade for elasticsearch-flx                                                                                                                                                                 resetting values to the chart's original version                                                                                                                                                               Warning  error  55s (x4 over 29m)  helm-controller  reconciliation failed: Helm upgrade failed: error while running post render on files: trouble configuring builtin PatchStrategicMergeTransformer with config: `                                                                                                                                                                                                     paths:                                                                                                                                                                                                       - '{"apiVersion":"apps/v1","kind":"StatefulSet","metadata":{"labels":{"app":"elasticsearch-flx-dev","test_label":"foo"},"namespace":"elasticsearch-flx"}}'                                         `: '/{"apiVersion":"apps/v1","kind":"StatefulSet","metadata":{"labels":{"app":"elasticsearch-flx-dev","test_label":"foo"},"namespace":"elasticsearch-flx"}}' doesn't exist

用户解释说他的补丁意味着“在这个命名空间和这个应用标签中找到一个StatefulSet,并添加test_label: foo”。但在内联补丁中混合选择器和修改感觉有点奇怪。他可能误解了吗?

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

根据您提供的配置,您的补丁似乎有一些问题。在patchesStrategicMerge中,您应该使用metadatalabels字段来指定要修改的StatefulSet的标签。以下是修复后的配置示例:

apiVersion: kind: HelmReleasemetadata:  name: ${elasticsearch_flx_app_name}  namespace: ${elasticsearch_flx_app_name}spec:  interval: 1m  install:    disableWait: true  upgrade:    disableWait: true  chart:    spec:      chart: search-guard-flx      version: "2.5.0"      sourceRef:        kind: HelmRepository        name: my-oci        namespace: flux-system  postRenderers:    - kustomize:        patchesStrategicMerge:          - apiVersion: apps/v1            kind: StatefulSet            metadata:              labels:                app: ${elasticsearch_flx_app_name}-${cluster_env}            spec:              template:                metadata:                  labels:                    test_label: foo  values:    # ... values omitted for brevity

在上面的示例中,我们将metadatalabels字段移动到了正确的位置,并在spec下的template中添加了labels字段。这样,补丁将正确地修改StatefulSet的标签。

方案2

如果您觉得在内联补丁中混合选择器和修改有点奇怪,您可以尝试使用单独的补丁文件来修改StatefulSet。以下是一个示例补丁文件的配置:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: ${elasticsearch_flx_app_name}
  labels:
    app: ${elasticsearch_flx_app_name}-${cluster_env}
spec:
  template:
    metadata:
      labels:
        test_label: foo

您可以将上述内容保存为一个单独的文件(例如statefulset-patch.yaml),然后在patchesStrategicMerge中引用该文件:

apiVersion: kind: HelmReleasemetadata:  name: ${elasticsearch_flx_app_name}  namespace: ${elasticsearch_flx_app_name}spec:  interval: 1m  install:    disableWait: true  upgrade:    disableWait: true  chart:    spec:      chart: search-guard-flx      version: "2.5.0"      sourceRef:        kind: HelmRepository        name: my-oci        namespace: flux-system  postRenderers:    - kustomize:        patchesStrategicMerge:          - statefulset-patch.yaml  values:    # ... values omitted for brevity

这样,您可以将选择器和修改分开,并更清晰地管理补丁文件。

方案3

如果您仍然遇到问题,可能是由于版本差异或其他原因导致的。在这种情况下,您可以尝试查看相关文档或寻求社区支持以获取更多帮助。

正文完