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

Process SM related templates in memory #15

Merged

Conversation

cam-garrison
Copy link

This PR changes our approach with templates to now process/store the templates in memory, rather than copying .tmpl files to /tmp/.... and then creating the .yaml files there as well.

This means that when creating a feature.Manifests(path), you no longer will include the "rootdir" or similar variable in the path. See PR changes for examples of this, since you point directly to the path in /templates.

Used image quay.io/cgarriso/opendatahub-operator:template-in-memory for testing, created DSCI with SM enabled as well as DSC with dashboard to ensure changes with Manifest(path) there still functioned as expected.

Copy link

@bartoszmajsak bartoszmajsak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this improvement! I left a few suggestions about naming and small change on the way how we can structure builder so we allow other filesystems to be used easily.

When reviewing the code I found a bug in the original implementation too :)

There's also dead code I cannot comment directly on, which we could remove https://github.com/maistra/opendatahub-operator/pull/15/files#diff-897801bd6ac96d166eb19cd078cd2b46eb522fce98d8e3573f08473ca2661e70R119-R121

}

func loadManifestsFrom(path string) ([]manifest, error) {
func loadManifestsFrom(embeddedFS embed.FS, rootPath string) ([]manifest, error) {
Copy link

@bartoszmajsak bartoszmajsak Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could align it with walk fs.WalkDir expects. We do not need anything specific from embeddedFS to make this whole thing work, and this is a limitation we don't need.

Suggested change
func loadManifestsFrom(embeddedFS embed.FS, rootPath string) ([]manifest, error) {
func loadManifestsFrom(fsys fs.FS, rootPath string) ([]manifest, error) {

note: I can't apply suggestion on the whole func so you will need to make changes in the editor, not here.

@@ -62,28 +75,30 @@ func (m *manifest) targetPath() string {
return fmt.Sprintf("%s%s", m.path[:len(m.path)-len(filepath.Ext(m.path))], ".yaml")
}

func (m *manifest) processTemplate(data interface{}) error {
func (m *manifest) processTemplate(fs embed.FS, data interface{}) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reasons as above

Suggested change
func (m *manifest) processTemplate(fs embed.FS, data interface{}) error {
func (m *manifest) processTemplate(fsys fs.FS, data interface{}) error {

Comment on lines 100 to 101
m.processedContent = buffer.String()
m.processed = true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we don't need m.processed anymore. Can't we assume that if m.processedContent == "" that means it was not a template?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8a2c375 agreed

pkg/feature/raw_resources.go Outdated Show resolved Hide resolved
@@ -106,7 +97,7 @@ func (f *Feature) patchResourceFromFile(filename string) error {
}

// Convert the patch from YAML to JSON
patchAsJSON, err := yaml.YAMLToJSON(data)
patchAsJSON, err := yaml.YAMLToJSON([]byte(data))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a bug in here. We split the whole thing into chunks based on --- separate, but instead of patching resources one by one we apply the entire data (so all the resources all at once). This means that currently we only have single-resource files in use, that's why this bug never showed up.

We should either give up on the assumption there can be more than one resource per file OR fix it to apply one by one. I would vouch for fixing it. Can you take care of it in this PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -79,7 +79,7 @@ func (fb *featureBuilder) Manifests(paths ...string) *featureBuilder {
var manifests []manifest

for _, path := range paths {
manifests, err = loadManifestsFrom(path)
manifests, err = loadManifestsFrom(embeddedFiles, path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite sure about this one from the pure API perspective. By hardcoding it to the global variable (//go:embed) we are locking it for any other sources like we made possible before.

What I would think about instead is to make fsys fs.FS a field on the featureBuilder struct, add extra method such as func (fb *featureBuilder) ManifestSource(fsys fs.FS) *featureBuilder which will allow us to set it (with godoc mentioning it's a root fs from which manifest paths are loaded). By default, in the Load() func, where we instantiate a Feature, we would set it to embeddedFiles and if ManifestSource is not called in the builder chain that would stay like that. Not a big change, but gives us flexibility. WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e072121 done here

return errors.WithStack(err)
}

log.Info("converted template to manifest", "feature", f.Name, "path", m.targetPath())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as a follow-up PR should change log levels from info to debug in most places. It's too much meaningless output in the logs now just to see that stuff is happening

pkg/feature/raw_resources.go Outdated Show resolved Hide resolved
Copy link

@bartoszmajsak bartoszmajsak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my other comments, but I think I wasn't exactly clear on the suggestions. Here's what I had in mind:

func (fb *featureBuilder) Load() (*Feature, error) {
	feature := &Feature{
		Name:    fb.name,
		Enabled: true,
        fsys: embeddedFiles,
	}

+ suggestions below.

Comment on lines 80 to 84
fsys := fb.fsys
if fsys == nil {
fsys = embeddedFiles
}

Copy link

@bartoszmajsak bartoszmajsak Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean it like this, sorry for misleading you with my original comment. I thought about using featureBuilder to populate this field in Feature instance eventually... let's blame it on the fact that I'm sick now.

Have a look at the follow-up suggestions.

Suggested change
fsys := fb.fsys
if fsys == nil {
fsys = embeddedFiles
}

pkg/feature/builder.go Outdated Show resolved Hide resolved
pkg/feature/feature.go Outdated Show resolved Hide resolved
@@ -159,34 +159,34 @@ func (f *Feature) addCleanup(cleanupFuncs ...Action) {

func (f *Feature) ApplyManifest(filename string) error {
m := loadManifestFrom(filename)
if err := m.processTemplate(f.Spec); err != nil {
if err := m.processTemplate(embeddedFiles, f.Spec); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err := m.processTemplate(embeddedFiles, f.Spec); err != nil {
if err := m.processTemplate(f.fsys, f.Spec); err != nil {

pkg/feature/builder.go Outdated Show resolved Hide resolved
pkg/feature/feature.go Outdated Show resolved Hide resolved
Copy link

@bartoszmajsak bartoszmajsak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few minor suggestions + bunch of commits I couldn't make as suggestions, but I hope you agree with them :)

Great job!

Co-authored-by: Bartosz Majsak <bartosz.majsak@gmail.com>
@cam-garrison cam-garrison merged commit edf0ebc into maistra:service-mesh-integration Dec 19, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants