forked from opendatahub-io/opendatahub-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(templates): process Feature templates in memory (opendatahub-io#797
) Co-authored-by: Bartosz Majsak <bartosz.majsak@gmail.com> Co-authored-by: Wen Zhou <wenzhou@redhat.com>
- Loading branch information
1 parent
515328c
commit 0e9c4ed
Showing
12 changed files
with
579 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,104 @@ | ||
package feature | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"html/template" | ||
"os" | ||
"io" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const BaseOutputDir = "/tmp/odh-operator" | ||
//go:embed templates | ||
var embeddedFiles embed.FS | ||
|
||
type manifest struct { | ||
name, | ||
path string | ||
path, | ||
processedContent string | ||
template, | ||
patch, | ||
processed bool | ||
patch bool | ||
fsys fs.FS | ||
} | ||
|
||
func loadManifestsFrom(path string) ([]manifest, error) { | ||
func loadManifestsFrom(fsys fs.FS, path string) ([]manifest, error) { | ||
var manifests []manifest | ||
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | ||
|
||
err := fs.WalkDir(fsys, path, func(path string, dirEntry fs.DirEntry, walkErr error) error { | ||
_, err := dirEntry.Info() | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() { | ||
|
||
if dirEntry.IsDir() { | ||
return nil | ||
} | ||
basePath := filepath.Base(path) | ||
manifests = append(manifests, manifest{ | ||
name: basePath, | ||
path: path, | ||
patch: strings.Contains(basePath, ".patch"), | ||
template: filepath.Ext(path) == ".tmpl", | ||
}) | ||
m := createManifestFrom(fsys, path) | ||
manifests = append(manifests, m) | ||
|
||
return nil | ||
}); err != nil { | ||
return nil, errors.WithStack(err) | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return manifests, nil | ||
} | ||
|
||
func createManifestFrom(fsys fs.FS, path string) manifest { | ||
basePath := filepath.Base(path) | ||
m := manifest{ | ||
name: basePath, | ||
path: path, | ||
patch: strings.Contains(basePath, ".patch"), | ||
template: filepath.Ext(path) == ".tmpl", | ||
fsys: fsys, | ||
} | ||
|
||
return m | ||
} | ||
|
||
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 { | ||
if !m.template { | ||
return nil | ||
func (m *manifest) process(data interface{}) error { | ||
manifestFile, err := m.open() | ||
if err != nil { | ||
return err | ||
} | ||
path := m.targetPath() | ||
defer manifestFile.Close() | ||
|
||
f, err := os.Create(path) | ||
content, err := io.ReadAll(manifestFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to create file: %w", err) | ||
} | ||
|
||
tmpl := template.New(m.name).Funcs(template.FuncMap{"ReplaceChar": ReplaceChar}) | ||
if !m.template { | ||
// If, by convention, the file is not suffixed with `.tmpl` we do not need to trigger template processing. | ||
// It's safe to return at this point. | ||
m.processedContent = string(content) | ||
return nil | ||
} | ||
|
||
tmpl, err = tmpl.ParseFiles(m.path) | ||
tmpl, err := template.New(m.name).Funcs(template.FuncMap{"ReplaceChar": ReplaceChar}).Parse(string(content)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create file: %w", err) | ||
} | ||
|
||
var buffer bytes.Buffer | ||
if err := tmpl.Execute(&buffer, data); err != nil { | ||
return err | ||
} | ||
|
||
err = tmpl.Execute(f, data) | ||
m.processed = err == nil | ||
m.processedContent = buffer.String() | ||
|
||
return nil | ||
} | ||
|
||
return err | ||
func (m *manifest) open() (fs.File, error) { | ||
return m.fsys.Open(m.path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.