Skip to content

Commit

Permalink
feat: error field on manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickkirschen committed Jun 1, 2024
1 parent c8ccd3d commit a7bba6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"

"github.com/yannickkirschen/manifesto"
)
Expand All @@ -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
}

Expand All @@ -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())
}
20 changes: 18 additions & 2 deletions manifesto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -45,20 +46,33 @@ 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.
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
Kind string
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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -184,5 +199,6 @@ func parseManifest(manifest *manifest, spec any, status any) *Manifest {
Metadata: manifest.Metadata,
Spec: spec,
Status: status,
Errors: manifest.Errors,
}
}

0 comments on commit a7bba6d

Please sign in to comment.