From 2e084160736b8b55b471f2b9dd5b182649ac41d4 Mon Sep 17 00:00:00 2001 From: George MacRorie Date: Thu, 14 Sep 2023 11:13:50 +0100 Subject: [PATCH 1/2] fix(controllers/template): ensure list glob facets by gvk and ns --- pkg/controllers/template/template.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controllers/template/template.go b/pkg/controllers/template/template.go index 9218b0e..fee1f33 100644 --- a/pkg/controllers/template/template.go +++ b/pkg/controllers/template/template.go @@ -18,7 +18,7 @@ import ( ) const ( - defaultNamespaceTmpl = `{{ .Namespace }}/*.json` + defaultNamespaceTmpl = `{{ .Namespace }}/{{ .Group }}-{{ .Version }}-{{ .Kind }}-*.json` defaultResourceTmpl = `{{ .Namespace }}/{{ .Group }}-{{ .Version }}-{{ .Kind }}-{{ .Name }}.json` ) From 25e485591659500b41481a4880515a2a84c5e4ad Mon Sep 17 00:00:00 2001 From: George MacRorie Date: Thu, 14 Sep 2023 11:25:50 +0100 Subject: [PATCH 2/2] feat(controller/template): make resouce and list templates configurable --- pkg/api/config/config.go | 5 +++- pkg/api/core/controller.go | 4 +-- pkg/controllers/template/template.go | 43 ++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) 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 fee1f33..c73d9f1 100644 --- a/pkg/controllers/template/template.go +++ b/pkg/controllers/template/template.go @@ -18,8 +18,8 @@ import ( ) const ( - defaultNamespaceTmpl = `{{ .Namespace }}/{{ .Group }}-{{ .Version }}-{{ .Kind }}-*.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 }