From bd757676f39071c19ad188acb1671c72bc7447b0 Mon Sep 17 00:00:00 2001 From: Bartosz Majsak Date: Tue, 9 Jul 2024 13:33:22 +0200 Subject: [PATCH] chore(feature): adds context to manifest errors (#1104) During processing manifest files the names of the files were not reported as part of the errors such as missing keys referred in the templates. This commit adds this context to returned errors so the problems can be located faster. (cherry picked from commit 9f028997c3b2bdfa713ad7caa4ba0623ac95102f) --- pkg/feature/manifest.go | 48 +++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/pkg/feature/manifest.go b/pkg/feature/manifest.go index e83afa83770..2cf994e3a5a 100644 --- a/pkg/feature/manifest.go +++ b/pkg/feature/manifest.go @@ -33,19 +33,24 @@ type rawManifest struct { var _ Manifest = (*rawManifest)(nil) func (b *rawManifest) Process(_ any) ([]*unstructured.Unstructured, error) { - manifestFile, err := b.fsys.Open(b.path) - if err != nil { - return nil, err + manifestFile, openErr := b.fsys.Open(b.path) + if openErr != nil { + return nil, fmt.Errorf("failed opening file %s: %w", b.path, openErr) } defer manifestFile.Close() - content, err := io.ReadAll(manifestFile) - if err != nil { - return nil, fmt.Errorf("failed to read file: %w", err) + content, readErr := io.ReadAll(manifestFile) + if readErr != nil { + return nil, fmt.Errorf("failed to read file %s: %w", b.path, readErr) } resources := string(content) - return convertToUnstructuredSlice(resources) + unstructuredObjs, convertErr := convertToUnstructuredSlice(resources) + if convertErr != nil { + return nil, fmt.Errorf("failed to convert resources defined in %s to unstructured objects: %w", b.path, convertErr) + } + + return unstructuredObjs, nil } func (b *rawManifest) MarkAsManaged(objects []*unstructured.Unstructured) { @@ -64,33 +69,38 @@ type templateManifest struct { } func (t *templateManifest) Process(data any) ([]*unstructured.Unstructured, error) { - manifestFile, err := t.fsys.Open(t.path) - if err != nil { - return nil, err + manifestFile, openErr := t.fsys.Open(t.path) + if openErr != nil { + return nil, fmt.Errorf("failed opening file %s: %w", t.path, openErr) } defer manifestFile.Close() - content, err := io.ReadAll(manifestFile) - if err != nil { - return nil, fmt.Errorf("failed to create file: %w", err) + content, readErr := io.ReadAll(manifestFile) + if readErr != nil { + return nil, fmt.Errorf("failed to read file %s: %w", t.path, readErr) } - tmpl, err := template.New(t.name). + tmpl, parseErr := template.New(t.name). Option("missingkey=error"). Funcs(template.FuncMap{"ReplaceChar": ReplaceChar}). Parse(string(content)) - if err != nil { - return nil, fmt.Errorf("failed to parse template: %w", err) + if parseErr != nil { + return nil, fmt.Errorf("failed to parse template %s: %w", t.path, parseErr) } var buffer bytes.Buffer - if err := tmpl.Execute(&buffer, data); err != nil { - return nil, fmt.Errorf("failed to execute template: %w", err) + if executeTmplErr := tmpl.Execute(&buffer, data); executeTmplErr != nil { + return nil, fmt.Errorf("failed to execute template %s: %w", t.path, executeTmplErr) } resources := buffer.String() - return convertToUnstructuredSlice(resources) + unstructuredObjs, convertErr := convertToUnstructuredSlice(resources) + if convertErr != nil { + return nil, fmt.Errorf("failed to convert resources defined in %s to unstructured objects: %w", t.path, convertErr) + } + + return unstructuredObjs, nil } func (t *templateManifest) MarkAsManaged(objects []*unstructured.Unstructured) {