You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Helm Chart中点(.)的定义、用途及与Shell中用法的差异

Understanding Helm's Dot (.) Usage & Comparing to Bash

Hey there! Let's break down what the dot (.) does in Helm templates, how it compares to bash's dot, and fix up those examples you shared.

Core Definition of . in Helm

In Helm (which uses Go templates under the hood), the dot . is the entry point to the current context. Think of it as a pointer to whatever data and template functions are available right where you're writing code. The context shifts depending on your location in the template:

  • At the top level, . refers to the root context, which includes built-in objects like Release, Values, Files, and Capabilities.
  • Inside a range loop, . switches to represent the current item being iterated over.
  • When you pass . to an included template, you're handing over your current context so that template can access the same data and functions.

How Does This Compare to Bash's Dot?

The bash dot has a few distinct uses, and the only loose similarity is the idea of "current":

  • In bash, . can mean the current directory (e.g., ./script.sh).
  • It's also used to source scripts (e.g., . ./my-script.sh), which loads the script's variables/functions into the current shell session.
  • Unlike Helm's ., bash's dot isn't a context pointer—it's either a path shortcut or a command. There's no direct functional overlap, so it's best to treat them as entirely separate syntax elements.

Let's Tackle Your Examples

1. Will This Code Print the Range's Files?

Yes, this code will work exactly as you expect! Let's break it down:

{{- $files := .Files }}
{{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
{{ . }}: |-
{{ $files.Get . }}
{{- end }}
  • $files := .Files captures the root context's Files object into a variable ($files—variables starting with $ retain their value across context changes).
  • Inside the range loop, . switches to represent each string in the tuple (e.g., "config1.toml" on the first iteration).
  • $files.Get . uses the captured Files object to fetch the content of the file named by the current . value. The output will be each filename followed by its content in a YAML block scalar format.

2. What Does . Do in the Include Statement?

{{ include "mychart.app" . | indent 4 }}

Here, . is passing your current context to the mychart.app template. Without passing ., the included template would have no access to any data (like Values or Release). This feels vaguely similar to referencing the current directory in bash, but functionally it's about sharing template data rather than file paths. The indent 4 just formats the output to be indented 4 spaces for proper YAML structure.

3. Fixing the Broken read.select-annot Template

The issue here is almost certainly that the template is being called without passing a valid map/object as context. Let's look at the original code:

{{- define "read.select-annot" -}}
{{- range $key, $value := . }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}

This template expects . to be a key-value map (like Kubernetes annotations). If you call it with something that's not a map (or nothing at all), the range will fail with an error.

Fix Steps:

  1. Ensure you pass a valid map when calling the template:
    For example, if your annotations are stored in .Values.annotations, call it like this:
    {{ include "read.select-annot" .Values.annotations }}
    
  2. Add a safety check to handle invalid inputs:
    To prevent crashes when the context isn't a map, add a check using kindIs:
    {{- define "read.select-annot" -}}
    {{- if kindIs "map" . }}
      {{- range $key, $value := . }}
      {{ $key }}: {{ $value }}
      {{- end }}
    {{- else }}
      # No valid annotation map provided
    {{- end }}
    {{- end }}
    

This way, even if you pass a non-map value, the template won't crash—it'll just output a friendly message instead.


内容的提问来源于stack exchange,提问作者Juan.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:52:30