Skip to content

Commit c48dbc1

Browse files
authored
fix python injector bug (knative#2992)
1 parent 88c0dd6 commit c48dbc1

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

pkg/builders/buildpacks/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
217217

218218
if f.Runtime == "python" {
219219
if fi, _ := os.Lstat(filepath.Join(f.Root, "Procfile")); fi == nil {
220-
cli = pyScaffoldInjector{cli}
220+
cli = pyScaffoldInjector{cli, f.Invoke}
221221
}
222222
}
223223

pkg/builders/buildpacks/scaffolding_injector.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"context"
66
"errors"
7+
"fmt"
78
"io"
89
"runtime"
910
"strings"
@@ -17,6 +18,7 @@ import (
1718
// It basically moves content of /workspace to /workspace/fn and then setup scaffolding code directly in /workspace.
1819
type pyScaffoldInjector struct {
1920
client.CommonAPIClient
21+
invoke string
2022
}
2123

2224
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,
6163
return
6264
}
6365
}
64-
err = writePythonScaffolding(tw)
66+
err = writePythonScaffolding(tw, s.invoke)
6567
if err != nil {
6668
return
6769
}
@@ -71,7 +73,7 @@ func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string,
7173
return s.CommonAPIClient.CopyToContainer(ctx, ctr, p, pr, opts)
7274
}
7375

74-
func writePythonScaffolding(tw *tar.Writer) error {
76+
func writePythonScaffolding(tw *tar.Writer, invoke string) error {
7577
for _, f := range []struct {
7678
path string
7779
content string
@@ -82,7 +84,7 @@ func writePythonScaffolding(tw *tar.Writer) error {
8284
},
8385
{
8486
path: "service/main.py",
85-
content: serviceMain,
87+
content: serviceMain(invoke),
8688
},
8789
{
8890
path: "service/__init__.py",
@@ -133,14 +135,15 @@ python = ">=3.9,<4.0"
133135
script = "service.main:main"
134136
`
135137

136-
const serviceMain = `"""
138+
func serviceMain(invoke string) string {
139+
template := `"""
137140
This code is glue between a user's Function and the middleware which will
138141
expose it as a network service. This code is written on-demand when a
139142
Function is being built, deployed or run. This will be included in the
140143
final container.
141144
"""
142145
import logging
143-
from func_python.cloudevent import serve
146+
from func_python.%s import serve
144147
145148
logging.basicConfig(level=logging.INFO)
146149
@@ -161,3 +164,5 @@ def main():
161164
if __name__ == "__main__":
162165
main()
163166
`
167+
return fmt.Sprintf(template, invoke)
168+
}

0 commit comments

Comments
 (0)