diff --git a/pkg/api/config/config.go b/pkg/api/config/config.go index 699a4fa..f1f0534 100644 --- a/pkg/api/config/config.go +++ b/pkg/api/config/config.go @@ -84,7 +84,10 @@ func New(ctx context.Context, cfg *config.Config) (*api.Configuration, error) { if err := core.DecodeController( buf, func(tc core.TemplateController) error { - c.Controllers[tc.Metadata.Name] = template.New() + c.Controllers[tc.Metadata.Name] = template.New( + template.WithListTemplate(tc.Spec.Spec.ListTemplate), + template.WithResourceTemplate(tc.Spec.Spec.ResourceTemplate), + ) return nil }, func(w core.WASMController) error { diff --git a/pkg/api/core/controller.go b/pkg/api/core/controller.go index 31387a3..96b752f 100644 --- a/pkg/api/core/controller.go +++ b/pkg/api/core/controller.go @@ -74,8 +74,8 @@ type ControllerSpec[T any] struct { } type TemplateControllerSpec struct { - DirectoryTemplate string `json:"directory_template"` - PathTemplate string `json:"path_template"` + ListTemplate string `json:"list_template"` + ResourceTemplate string `json:"resource_template"` } type WASMControllerSpec struct { diff --git a/pkg/controllers/template/template.go b/pkg/controllers/template/template.go index 9218b0e..c73d9f1 100644 --- a/pkg/controllers/template/template.go +++ b/pkg/controllers/template/template.go @@ -18,8 +18,8 @@ import ( ) const ( - defaultNamespaceTmpl = `{{ .Namespace }}/*.json` - defaultResourceTmpl = `{{ .Namespace }}/{{ .Group }}-{{ .Version }}-{{ .Kind }}-{{ .Name }}.json` + defaultListTmpl = `{{ .Namespace }}/{{ .Group }}-{{ .Version }}-{{ .Kind }}-*.json` + defaultResourceTmpl = `{{ .Namespace }}/{{ .Group }}-{{ .Version }}-{{ .Kind }}-{{ .Name }}.json` ) var funcs = template.FuncMap{ @@ -38,7 +38,7 @@ type ResourceEncoding interface { // encoding them using the provided marshaller. type Controller struct { encoding ResourceEncoding - nsTmpl *template.Template + listTmpl *template.Template resourceTmpl *template.Template } @@ -50,9 +50,9 @@ func New(opts ...containers.Option[Controller]) *Controller { Prefix: "", Indent: " ", }, - nsTmpl: template.Must(template.New("ns"). + listTmpl: template.Must(template.New("list"). Funcs(funcs). - Parse(defaultNamespaceTmpl), + Parse(defaultListTmpl), ), resourceTmpl: template.Must(template.New("resource"). Funcs(funcs). @@ -72,6 +72,37 @@ func WithResourceEncoding(e ResourceEncoding) containers.Option[Controller] { } } +// WithListTemplate lets you override the default namespace template +// for identifying which files should be respected and returned for a given +// resource type and namespace +func WithListTemplate(tmpl string) containers.Option[Controller] { + if tmpl == "" { + return func(c *Controller) {} + } + + return func(c *Controller) { + c.listTmpl = template.Must(template.New("list"). + Funcs(funcs). + Parse(tmpl), + ) + } +} + +// WithResourceTemplate lets you override the default resource template +// for identifying which file relates to the requested resource +func WithResourceTemplate(tmpl string) containers.Option[Controller] { + if tmpl == "" { + return func(c *Controller) {} + } + + return func(c *Controller) { + c.resourceTmpl = template.Must(template.New("resource"). + Funcs(funcs). + Parse(tmpl), + ) + } +} + func (c *Controller) Get(_ context.Context, req *controllers.GetRequest) (_ *core.Resource, err error) { defer func() { if err != nil { @@ -103,7 +134,7 @@ func (c *Controller) List(_ context.Context, req *controllers.ListRequest) (reso }() buf := &bytes.Buffer{} - if err := c.nsTmpl.Execute(buf, req); err != nil { + if err := c.listTmpl.Execute(buf, req); err != nil { return nil, err }