fix python injector bug

This commit is contained in:
David Fridrich 2025-08-18 14:13:08 +02:00
parent c13214d21f
commit 4a4fc73f83
2 changed files with 11 additions and 6 deletions

View File

@ -217,7 +217,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
if f.Runtime == "python" { if f.Runtime == "python" {
if fi, _ := os.Lstat(filepath.Join(f.Root, "Procfile")); fi == nil { if fi, _ := os.Lstat(filepath.Join(f.Root, "Procfile")); fi == nil {
cli = pyScaffoldInjector{cli} cli = pyScaffoldInjector{cli, f.Invoke}
} }
} }

View File

@ -4,6 +4,7 @@ import (
"archive/tar" "archive/tar"
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"runtime" "runtime"
"strings" "strings"
@ -17,6 +18,7 @@ import (
// It basically moves content of /workspace to /workspace/fn and then setup scaffolding code directly in /workspace. // It basically moves content of /workspace to /workspace/fn and then setup scaffolding code directly in /workspace.
type pyScaffoldInjector struct { type pyScaffoldInjector struct {
client.CommonAPIClient client.CommonAPIClient
invoke string
} }
func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string, r io.Reader, opts container.CopyToContainerOptions) error { func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string, r io.Reader, opts container.CopyToContainerOptions) error {
@ -61,7 +63,7 @@ func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string,
return return
} }
} }
err = writePythonScaffolding(tw) err = writePythonScaffolding(tw, s.invoke)
if err != nil { if err != nil {
return return
} }
@ -71,7 +73,7 @@ func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string,
return s.CommonAPIClient.CopyToContainer(ctx, ctr, p, pr, opts) return s.CommonAPIClient.CopyToContainer(ctx, ctr, p, pr, opts)
} }
func writePythonScaffolding(tw *tar.Writer) error { func writePythonScaffolding(tw *tar.Writer, invoke string) error {
for _, f := range []struct { for _, f := range []struct {
path string path string
content string content string
@ -82,7 +84,7 @@ func writePythonScaffolding(tw *tar.Writer) error {
}, },
{ {
path: "service/main.py", path: "service/main.py",
content: serviceMain, content: serviceMain(invoke),
}, },
{ {
path: "service/__init__.py", path: "service/__init__.py",
@ -133,14 +135,15 @@ python = ">=3.9,<4.0"
script = "service.main:main" script = "service.main:main"
` `
const serviceMain = `""" func serviceMain(invoke string) string {
template := `"""
This code is glue between a user's Function and the middleware which will This code is glue between a user's Function and the middleware which will
expose it as a network service. This code is written on-demand when a expose it as a network service. This code is written on-demand when a
Function is being built, deployed or run. This will be included in the Function is being built, deployed or run. This will be included in the
final container. final container.
""" """
import logging import logging
from func_python.cloudevent import serve from func_python.%s import serve
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -161,3 +164,5 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() main()
` `
return fmt.Sprintf(template, invoke)
}