Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controllers/template): ensure list glob facets by gvk and ns #67

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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