Skip to content

Commit

Permalink
Merge pull request #67 from flipt-io/gm/fix-default-directory-template
Browse files Browse the repository at this point in the history
fix(controllers/template): ensure list glob facets by gvk and ns
  • Loading branch information
GeorgeMac authored Sep 14, 2023
2 parents 1297c0e + 25e4855 commit 541fba4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
5 changes: 4 additions & 1 deletion pkg/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/core/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 37 additions & 6 deletions pkg/controllers/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}

Expand All @@ -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).
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 541fba4

Please sign in to comment.