From a7bba6d808bbac7df7bb352c970e2053591aa8ad Mon Sep 17 00:00:00 2001 From: Yannick Kirschen Date: Sat, 1 Jun 2024 13:31:33 +0200 Subject: [PATCH] feat: error field on manifest --- example/example.go | 5 ++++- manifesto.go | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/example/example.go b/example/example.go index 3751424..fb267f3 100755 --- a/example/example.go +++ b/example/example.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "github.com/yannickkirschen/manifesto" ) @@ -12,6 +13,7 @@ type MySpec struct { func MyListener(action manifesto.Action, manifest *manifesto.Manifest) error { if manifest.ApiVersion != "example.com/v1alpha1" || manifest.Kind != "MyManifest" { + log.Printf("Unknown API Version and kind: %s/%s", manifest.ApiVersion, manifest.Kind) return nil } @@ -36,6 +38,7 @@ func main() { pool.Listen(MyListener) pool.Apply(m) - m3 := pool.GetByKey(m.CreateKey()) + m3, _ := pool.GetByKey(m.CreateKey()) + m3.Error("Houston, we have a problem!") pool.Delete(m3.CreateKey()) } diff --git a/manifesto.go b/manifesto.go index 7540841..d2c28ae 100755 --- a/manifesto.go +++ b/manifesto.go @@ -23,6 +23,7 @@ type manifest struct { Metadata Metadata `yaml:"metadata" json:"metadata"` Spec yaml.Node `yaml:"spec" json:"spec"` Status yaml.Node `yaml:"status" json:"status"` + Errors []string `yaml:"errors,omitempty" json:"errors,omitempty"` } // Manifest is the root entity. @@ -45,6 +46,9 @@ type Manifest struct { // Status holds status information. Developers must provide their own struct // to be used as Status. Status any `yaml:"status" json:"status"` + + // Errors contains errors that occurred while handling the manifest. + Errors []string `yaml:"errors,omitempty" json:"errors,omitempty"` } // CreateKey created a new ManifestKey based on the ApiVersion and Kind. @@ -52,6 +56,11 @@ func (manifest *Manifest) CreateKey() ManifestKey { return ManifestKey{manifest.ApiVersion, manifest.Kind, manifest.Metadata.Name} } +// Error adds an error message to the list of errors. +func (manifest *Manifest) Error(message string) { + manifest.Errors = append(manifest.Errors, message) +} + // ManifestKey is a primary key for manifests. type ManifestKey struct { ApiVersion string @@ -59,6 +68,11 @@ type ManifestKey struct { Name string } +// NewManifestKey creates a new key based on the parameters. +func NewManifestKey(apiVersion string, kind string, name string) *ManifestKey { + return &ManifestKey{apiVersion, kind, name} +} + // Metadata contains all additional information on a manifest. type Metadata struct { // Name is the name of the manifest. Within a Kind, the name must be unique. @@ -124,8 +138,9 @@ func (pool *Pool) Delete(key ManifestKey) { } // GetByKey searches for a manifest and returns it. -func (pool *Pool) GetByKey(key ManifestKey) *Manifest { - return pool.manifests[key] +func (pool *Pool) GetByKey(key ManifestKey) (*Manifest, bool) { + manifest, ok := pool.manifests[key] + return manifest, ok } // ParseFile reads a JSON/YAML file and returns the parsed Manifest. @@ -184,5 +199,6 @@ func parseManifest(manifest *manifest, spec any, status any) *Manifest { Metadata: manifest.Metadata, Spec: spec, Status: status, + Errors: manifest.Errors, } }