Skip to content

Commit

Permalink
Fix use of template functions declared in the same file
Browse files Browse the repository at this point in the history
When using template functions that are declared in the same file as
 the templates, an error would be returned specifying that the function
 is not found. This commit fixes this behavior and also adds a test case 
that ensures that the bug has been fixed. 

Signed-off-by: Alina Sudakov <asudakov@redhat.com>
  • Loading branch information
AlinaSecret committed Jun 25, 2024
1 parent 8817fc8 commit 8b1a041
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ error code:2`),
outputFormat: Json,
checks: defaultChecks,
},
{
name: "Ref Contains Templates With Function Templates In Same File",
mode: []Mode{DefaultMode},
checks: defaultChecks,
},
}
tf := cmdtesting.NewTestFactory()
testFlags := flag.NewFlagSet("test", flag.ContinueOnError)
Expand Down
2 changes: 1 addition & 1 deletion pkg/compare/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func parseTemplates(templatePaths, functionTemplates []string, fsys fs.FS) ([]*t
continue
}
// recreate template with new name that includes path from reference root:
parsedTemp, _ = template.New(temp).Funcs(FuncMap()).AddParseTree(temp, parsedTemp.Tree)
parsedTemp, _ = parsedTemp.New(temp).AddParseTree(temp, parsedTemp.Lookup(path.Base(temp)).Tree)
if len(functionTemplates) > 0 {
parsedTemp, err = parsedTemp.ParseFS(fsys, functionTemplates...)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

error code:1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

**********************************

Cluster CR: v1_ConfigMap_kubernetes-dashboard_kubernetes-dashboard-settings
Reference File: cm.yaml
Diff Output: diff -u -N TEMP/v1_configmap_kubernetes-dashboard_kubernetes-dashboard-settings TEMP/v1_configmap_kubernetes-dashboard_kubernetes-dashboard-settings
--- TEMP/v1_configmap_kubernetes-dashboard_kubernetes-dashboard-settings DATE
+++ TEMP/v1_configmap_kubernetes-dashboard_kubernetes-dashboard-settings DATE
@@ -2,6 +2,6 @@
kind: ConfigMap
metadata:
labels:
- k8s-app: kubernetes-dashboardfunction was called successfully from same file
+ k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-settings
namespace: kubernetes-dashboard

**********************************

Summary
CRs with diffs: 1
No CRs are missing from the cluster
No CRs are unmatched to reference CRs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{- define "addBlob" -}}
function was called successfully from same file
{{- end -}}

kind: ConfigMap
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard {{- template "addBlob" }}
name: kubernetes-dashboard-settings
namespace: kubernetes-dashboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Parts:
- name: ExamplePart
Components:
- name: DemonSets
type: Required
requiredTemplates:
- cm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: ConfigMap
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-settings
namespace: kubernetes-dashboard

0 comments on commit 8b1a041

Please sign in to comment.