kubernetes/kops · error
error executing template %q: %w
Error message
error executing template %q: %w
What it means
After successfully parsing, RenderTemplate executes the template against cluster.spec. This error wraps any runtime execution failure — nil map index issues aside (missingkey=zero), these are typically data/type errors such as calling a template function with wrong arguments or ranging over a non-iterable value.
Source
Thrown at upup/pkg/fi/cloudup/template_functions.go:133
tf := r.newTemplateFunctions(tasks)
funcMap := template.FuncMap{}
if err := tf.AddTo(funcMap, r.secretStore); err != nil {
return nil, err
}
if tasks == nil {
funcMap["Task"] = func(typeName, name string) (fi.CloudupTask, error) { return nil, nil }
funcMap["HasTask"] = func(typeName, name string) bool { return false }
funcMap["TasksByType"] = func(typeName string) ([]fi.CloudupTask, error) { return nil, nil }
}
t := template.New(name).Funcs(funcMap).Option("missingkey=zero")
if _, err := t.Parse(string(source)); err != nil {
return nil, fmt.Errorf("error parsing template %q: %w", name, err)
}
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, name, r.modelContext.Cluster.Spec); err != nil {
return nil, fmt.Errorf("error executing template %q: %w", name, err)
}
return buf.Bytes(), nil
}
// CloudControllerConfigArgv returns the cloud controller argv without binding any task graph.
func (r *addonTemplateRenderer) CloudControllerConfigArgv() ([]string, error) {
return r.newTemplateFunctions(nil).CloudControllerConfigArgv()
}
// AddTo defines the available functions we can use in our YAML models.
// If we are trying to get a new function implemented it MUST
// be defined here.
func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretStore) (err error) {
cluster := tf.Cluster
dest["ToJSON"] = tf.ToJSON
dest["ToYAML"] = tf.ToYAML
dest["KubeObjectToApplyYAML"] = kubemanifest.KubeObjectToApplyYAMLView on GitHub (pinned to 4c8573c808)
Solutions
- Read the wrapped inner error to find the failing template line and fix the expression or function call
- Check the template functions available in your kOps version (template_functions.go) and update calls to match
- Test-render with the same cluster spec via `kops toolbox template` if available to iterate quickly
Example fix
// before
region: {{ Region }}
// after
region: {{ Region . }} Defensive patterns
Strategy: try-catch
Validate before calling
// render against real ClusterSpec in a unit test before deploy
tmpl := template.New(name).Funcs(funcMap).Option("missingkey=zero")
if err := tmpl.Execute(io.Discard, cluster.Spec); err != nil {
t.Fatalf("template %s fails at runtime: %v", name, err)
} Try / catch
out, err := r.RenderTemplate(name, source)
if err != nil && strings.Contains(err.Error(), "error executing template") {
// inspect wrapped error for the failing line/function
return fmt.Errorf("rendering %s: %w", name, err)
} Prevention
- Add a unit test executing every custom template against a real ClusterSpec
- Only call template functions that exist in your kOps version's funcMap
- Use missingkey=zero semantics consciously and guard optional fields
When it happens
Trigger: Template parses but fails at ExecuteTemplate: calling a template function with wrong argument types (e.g. AddInt on a non-integer), indexing a field that doesn't exist on ClusterSpec in this kOps version, or a custom func returning an error.
Common situations: Templates written for a different kOps version referencing removed ClusterSpec fields or renamed template functions; custom addons calling helpers with wrong signatures.
Related errors
- error parsing template %q: %w
- template tasks are not available during this render phase
- listing SSH key tasks: %w
- error creating kops config template: %w
- error creating gcp machine template: %w
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/85324e6a01e17522.
Report an issue: GitHub.