Skip to content

Commit ad360c7

Browse files
committed
Fix: escape {{}} in CRDs to prevent Helm templating errors
1 parent 61603cb commit ad360c7

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pkg/processor/crd/crd.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,21 @@ func (c crd) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
111111
return true, nil, fmt.Errorf("%w: unable to cast to crd spec", err)
112112
}
113113

114+
replacements := make(map[string]string)
114115
if spec.Conversion != nil {
115116
conv := spec.Conversion
116117
if conv.Strategy == v1.WebhookConverter {
117118
wh := conv.Webhook
118119
if wh != nil && wh.ClientConfig != nil && wh.ClientConfig.Service != nil {
119-
wh.ClientConfig.Service.Name = appMeta.TemplatedName(wh.ClientConfig.Service.Name)
120-
wh.ClientConfig.Service.Namespace = strings.ReplaceAll(wh.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
120+
// Use placeholders starting with '*' to force quoting in YAML
121+
namePlaceholder := "*__HELMIFY_NAME_PLACEHOLDER__"
122+
nsPlaceholder := "*__HELMIFY_NS_PLACEHOLDER__"
123+
124+
replacements[namePlaceholder] = appMeta.TemplatedName(wh.ClientConfig.Service.Name)
125+
wh.ClientConfig.Service.Name = namePlaceholder
126+
127+
replacements[nsPlaceholder] = strings.ReplaceAll(wh.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
128+
wh.ClientConfig.Service.Namespace = nsPlaceholder
121129
}
122130
}
123131
}
@@ -126,7 +134,17 @@ func (c crd) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
126134
specYaml = yamlformat.Indent(specYaml, 2)
127135
specYaml = bytes.TrimRight(specYaml, "\n ")
128136

129-
res := fmt.Sprintf(crdTeml, obj.GetName(), appMeta.ChartName(), annotations, labels, string(specYaml))
137+
specStr := string(specYaml)
138+
// Escape {{ and }} to {{ "{{" }} and {{ "}}" }} to prevent Helm from interpreting them
139+
replacer := strings.NewReplacer("{{", `{{ "{{" }}`, "}}", `{{ "}}" }}`)
140+
specStr = replacer.Replace(specStr)
141+
142+
// Restore placeholders with actual Helm templates
143+
for k, v := range replacements {
144+
specStr = strings.ReplaceAll(specStr, k, v)
145+
}
146+
147+
res := fmt.Sprintf(crdTeml, obj.GetName(), appMeta.ChartName(), annotations, labels, specStr)
130148
res = strings.ReplaceAll(res, "\n\n", "\n")
131149

132150
return true, &result{

0 commit comments

Comments
 (0)