From 435f1d92f852e0ea8d3ef3b5ebfca207c620a3c7 Mon Sep 17 00:00:00 2001 From: nocturnalastro Date: Fri, 9 Aug 2024 10:06:01 +0100 Subject: [PATCH] Stop failing metadata collection from causing a panic --- pkg/compare/parsing.go | 9 ++++++--- .../localerr.golden | 2 +- pkg/compare/testdata/YAMLOutput/localout.golden | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/compare/parsing.go b/pkg/compare/parsing.go index 2e94b411..8d122ae4 100644 --- a/pkg/compare/parsing.go +++ b/pkg/compare/parsing.go @@ -132,7 +132,10 @@ func (rf ReferenceTemplate) Exec(params map[string]any) (*unstructured.Unstructu content := buf.Bytes() err = yaml.Unmarshal(bytes.ReplaceAll(content, []byte(noValue), []byte("")), &data) if err != nil { - return nil, fmt.Errorf("template: %s isn't a yaml file after injection. yaml unmarshal error: %w. The Template After Execution: %s", rf.Name(), err, string(content)) + return nil, fmt.Errorf( + "template: %s isn't a yaml file after injection. yaml unmarshal error: %w. The Template After Execution: %s", + rf.Name(), err, string(content), + ) } return &unstructured.Unstructured{Object: data}, nil } @@ -272,13 +275,13 @@ func parseTemplates(templateReference []*ReferenceTemplate, functionTemplates [] temp.Template = parsedTemp temp.metadata, err = temp.Exec(map[string]any{}) // Extract Metadata if err != nil { - errs = append(errs, err) + errs = append(errs, fmt.Errorf("failed to parse template %s with empty data: %w", temp.Path, err)) } err = temp.ValidateFieldsToOmit(ref.FieldsToOmit) if err != nil { errs = append(errs, err) } - if temp.metadata.GetKind() == "" { + if temp.metadata != nil && temp.metadata.GetKind() == "" { errs = append(errs, fmt.Errorf("template missing kind: %s", temp.Path)) } } diff --git a/pkg/compare/testdata/TemplateIsntYAMLAfterExecutionWithEmptyMap/localerr.golden b/pkg/compare/testdata/TemplateIsntYAMLAfterExecutionWithEmptyMap/localerr.golden index 753f7df7..4a22b31d 100644 --- a/pkg/compare/testdata/TemplateIsntYAMLAfterExecutionWithEmptyMap/localerr.golden +++ b/pkg/compare/testdata/TemplateIsntYAMLAfterExecutionWithEmptyMap/localerr.golden @@ -1,2 +1,2 @@ -error: failed to constuct template: template: apps.v1.DaemonSet.kube-system.kindnet.yaml:5:61: executing "apps.v1.DaemonSet.kube-system.kindnet.yaml" at : error calling len: reflect: call of reflect.Value.Type on zero Value +error: failed to parse template apps.v1.DaemonSet.kube-system.kindnet.yaml with empty data: failed to constuct template: template: apps.v1.DaemonSet.kube-system.kindnet.yaml:5:61: executing "apps.v1.DaemonSet.kube-system.kindnet.yaml" at : error calling len: reflect: call of reflect.Value.Type on zero Value error code:2 diff --git a/pkg/compare/testdata/YAMLOutput/localout.golden b/pkg/compare/testdata/YAMLOutput/localout.golden index bd293828..7a4e02e9 100644 --- a/pkg/compare/testdata/YAMLOutput/localout.golden +++ b/pkg/compare/testdata/YAMLOutput/localout.golden @@ -3,9 +3,9 @@ Diffs: CorrelatedTemplate: deploymentDashboard.yaml DiffOutput: "diff -u -N TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\n--- - TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n@@ -14,7 +14,7 @@\n template:\n metadata:\n labels:\n- k8s-app: - kubernetes-dashboard\n+ k8s-app: kubernetes-dashboard-diff\n spec:\n - \ containers:\n - args:\n" + TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n@@ -14,7 +14,7 @@\n template:\n metadata:\n labels:\n- + \ k8s-app: kubernetes-dashboard\n+ k8s-app: kubernetes-dashboard-diff\n + \ spec:\n containers:\n - args:\n" Summary: NumDiffCRs: 1 NumMissing: 1