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

Proposal: use Golang templating to write func.yaml #1853

Closed
Tracked by #2026
zroubalik opened this issue Jul 7, 2023 · 1 comment
Closed
Tracked by #2026

Proposal: use Golang templating to write func.yaml #1853

zroubalik opened this issue Jul 7, 2023 · 1 comment
Labels
kind/feature-request lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@zroubalik
Copy link
Contributor

zroubalik commented Jul 7, 2023

Currently yaml.Marshal() & os.WriteFile() is used to write a Function configuration to disk. It is a convenient way to handle Golang structures, but it has one major drawback - we are unable to write comments to the file. Comments are useful as we can explain the fields, add examples, defaults etc. We can even write the whole configuration to the file, just comment the fields that are not used and users can easily uncomment them if they want to use them (for example the whole options part would benefit from this).

// Write Function struct (metadata) to Disk at f.Root
func (f Function) Write() (err error) {
// Skip writing (and dirtying the work tree) if there were no modifications.
f1, _ := NewFunction(f.Root)
if reflect.DeepEqual(f, f1) {
return
}
// Do not write invalid functions
if err = f.Validate(); err != nil {
return
}
// Write
var bb []byte
if bb, err = yaml.Marshal(&f); err != nil {
return
}
// TODO: open existing file for writing, such that existing permissions
// are preserved?
return os.WriteFile(filepath.Join(f.Root, FunctionFile), bb, 0644)
}

I have been thinking how to solve this for some time and there's option that could be potentially used - Golang built in templating from text/template package. Something like this:

type funcTemplateData struct {
	FunctionName   string
	FunctionRuntime  string
	FunctionRegistry  string
	FunctionImage  string
	FunctionType  string
        ...
}

func (f Function) Write() (err error) { 

funcYamlTemplate :=`
specVersion: 0.35.0
name: {{.FunctionName}}
runtime: {{.FunctionRuntime}}
registry: {{.FunctionRegistry}}
image: {{.FunctionImage}}

# my comment
invoke: {{.FunctionType}}

# another Comment
...
`

var funcData funcTemplateData

// this would set all the values that are set in the Function struct and fill defaults for the rest
funcData = f.GetValuesOrDefaults()

tmpl, err := template.New("func.yaml").Parse(funcYamlTemplate)
file, err := os.Create(projectRoot+"func.yaml")

return tmpl.Execute(file, funcData)
}

The same approach is used for Tekton Pipeline templates:
https://github.com/knative/func/blob/main/pkg/pipelines/tekton/templates_pack.go
https://github.com/knative/func/blob/main/pkg/pipelines/tekton/templates.go#L165

To read the function config, we can use the same apporach we have now, we just need to change the write function. This solution is obviously much harder to implement (dealing with yamls this way is a pain). But it could give us the benefit of adding some comments&examples on the file. Obviously this apporach still doesn't allow users to write their own comments, but at least something. Or maybe it is now worth the work 🤷‍♂️

Copy link
Contributor

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Reopen the issue with /reopen. Mark the issue as
fresh by adding the comment /remove-lifecycle stale.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature-request lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
Status: ✅ Done
Development

No branches or pull requests

2 participants