From 9c029afe33f493a9571f7c2013ce8de8738ed458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Va=C5=A1ko?= Date: Wed, 18 Dec 2024 09:24:16 +0100 Subject: [PATCH] feat: Add new error check for empty configuration --- internal/pkg/template/manifest/file.go | 13 +++++++++++-- internal/pkg/template/manifest/manifest.go | 8 +++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/pkg/template/manifest/file.go b/internal/pkg/template/manifest/file.go index b412a6fbb7..e50604e109 100644 --- a/internal/pkg/template/manifest/file.go +++ b/internal/pkg/template/manifest/file.go @@ -39,6 +39,7 @@ func evaluateFile(ctx context.Context, file *filesystem.RawFile, jsonnetCtx *jso } content := newFile() + // Decode string and set Configs object if err := json.DecodeString(jsonContent, content); err != nil { return nil, err } @@ -86,9 +87,13 @@ func (f *file) validate(ctx context.Context) error { return nil } -func (f *file) records() []model.ObjectManifest { +func (f *file) records() (oms []model.ObjectManifest, err error) { out := make([]model.ObjectManifest, 0, len(f.Configs)) for _, config := range f.Configs { + if config == nil { + continue + } + out = append(out, &config.ConfigManifest) for _, row := range config.Rows { row.ComponentID = config.ComponentID @@ -96,7 +101,11 @@ func (f *file) records() []model.ObjectManifest { out = append(out, row) } } - return out + if len(out) == 0 { + return nil, errors.New("unable to create template using invalid manifest configuration") + } + + return out, nil } func (f *file) setRecords(records []model.ObjectManifest) { diff --git a/internal/pkg/template/manifest/manifest.go b/internal/pkg/template/manifest/manifest.go index 7578aa1c80..0b25657918 100644 --- a/internal/pkg/template/manifest/manifest.go +++ b/internal/pkg/template/manifest/manifest.go @@ -58,8 +58,14 @@ func (f *File) Evaluate(ctx context.Context, jsonnetCtx *jsonnet.Context) (*Mani // Create manifest m := New() + // Get records + records, err := content.records() + if err != nil { + return nil, errors.Errorf(`cannot load configurations from manifest "%s": %w`, f.file.Path(), err) + } + // Set records - if err := m.records.SetRecords(content.records()); err != nil { + if err := m.records.SetRecords(records); err != nil { return nil, errors.Errorf(`cannot load manifest: %w`, err) }