Skip to content

Commit 3e9c77f

Browse files
authored
[release-v1.16] Pick Python scaffolding (#1236)
* Pick knative#2685 Signed-off-by: Matej Vašek <[email protected]> * Fix Python S2I On-Cluster build Signed-off-by: Matej Vašek <[email protected]> --------- Signed-off-by: Matej Vašek <[email protected]> Signed-off-by: Matej Vašek <[email protected]>
1 parent 9c14c0f commit 3e9c77f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+9473
-8218
lines changed

.github/workflows/test-e2e-runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
continue-on-error: true
1313
strategy:
1414
matrix:
15-
runtime: ["node", "go", "python", "quarkus", "springboot", "typescript", "rust"]
15+
runtime: ["node", "go", "quarkus", "springboot", "typescript", "rust"]
1616
runs-on: ubuntu-latest
1717
steps:
1818
- name: Set Environment Variables

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
/func
33
/func_*
44
/cmd/func.yaml
5-
/templates/typescript/cloudevents/build
6-
/templates/typescript/http/build
75
/coverage.out
86
/coverage.txt
97
/.coverage
@@ -16,11 +14,15 @@
1614

1715
/pkg/functions/testdata/migrations/*/.gitignore
1816

19-
# Nodejs
17+
# JS
2018
node_modules
19+
/templates/typescript/cloudevents/build
20+
/templates/typescript/http/build
2121

2222
# Python
2323
__pycache__
24+
/templates/python/cloudevents/.venv
25+
/templates/python/http/.venv
2426

2527
# VSCode
2628
.vscode

Makefile

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,27 @@ generate/zz_filesystem_generated.go: clean_templates templates/certs/ca-certific
8585
.PHONY: clean_templates
8686
clean_templates:
8787
# Removing temporary template files
88+
@rm -rf templates/**/.DS_Store
8889
@rm -rf templates/node/cloudevents/node_modules
8990
@rm -rf templates/node/http/node_modules
90-
@rm -rf templates/python/cloudevents/__pycache__
91-
@rm -rf templates/python/http/__pycache__
92-
@rm -rf templates/typescript/cloudevents/node_modules
93-
@rm -rf templates/typescript/http/node_modules
94-
@rm -rf templates/typescript/cloudevents/build
95-
@rm -rf templates/typescript/http/build
96-
@rm -rf templates/rust/cloudevents/target
97-
@rm -rf templates/rust/http/target
91+
@rm -rf templates/python/cloudevents/.venv
92+
@rm -rf templates/python/cloudevents/.pytest_cache
93+
@rm -rf templates/python/cloudevents/function/__pycache__
94+
@rm -rf templates/python/cloudevents/tests/__pycache__
95+
@rm -rf templates/python/http/.venv
96+
@rm -rf templates/python/http/.pytest_cache
97+
@rm -rf templates/python/http/function/__pycache__
98+
@rm -rf templates/python/http/tests/__pycache__
9899
@rm -rf templates/quarkus/cloudevents/target
99100
@rm -rf templates/quarkus/http/target
101+
@rm -rf templates/rust/cloudevents/target
102+
@rm -rf templates/rust/http/target
100103
@rm -rf templates/springboot/cloudevents/target
101104
@rm -rf templates/springboot/http/target
102-
@rm -f templates/**/.DS_Store
105+
@rm -rf templates/typescript/cloudevents/build
106+
@rm -rf templates/typescript/cloudevents/node_modules
107+
@rm -rf templates/typescript/http/build
108+
@rm -rf templates/typescript/http/node_modules
103109

104110
.PHONY: clean
105111
clean: clean_templates ## Remove generated artifacts such as binaries and schemas
@@ -151,9 +157,8 @@ test-node: ## Test Node templates
151157
cd templates/node/cloudevents && npm ci && npm test && rm -rf node_modules
152158
cd templates/node/http && npm ci && npm test && rm -rf node_modules
153159

154-
test-python: ## Test Python templates
155-
cd templates/python/cloudevents && pip3 install -r requirements.txt && python3 test_func.py && rm -rf __pycache__
156-
cd templates/python/http && python3 test_func.py && rm -rf __pycache__
160+
test-python: ## Test Python templates and Scaffolding
161+
test/test_python.sh
157162

158163
test-quarkus: ## Test Quarkus templates
159164
cd templates/quarkus/cloudevents && ./mvnw -q test && ./mvnw clean && rm .mvn/wrapper/maven-wrapper.jar
@@ -176,14 +181,15 @@ test-typescript: ## Test Typescript templates
176181
###############
177182

178183
# Pulls runtimes then rebuilds the embedded filesystem
179-
update-runtimes: pull-runtimes generate/zz_filesystem_generated.go ## Update Scaffolding Runtimes
184+
update-runtimes: update-runtime-go generate/zz_filesystem_generated.go ## Update Scaffolding Runtimes
180185

181-
pull-runtimes:
186+
update-runtime-go:
182187
cd templates/go/scaffolding/instanced-http && go get -u knative.dev/func-go/http
183188
cd templates/go/scaffolding/static-http && go get -u knative.dev/func-go/http
184189
cd templates/go/scaffolding/instanced-cloudevents && go get -u knative.dev/func-go/cloudevents
185190
cd templates/go/scaffolding/static-cloudevents && go get -u knative.dev/func-go/cloudevents
186191

192+
187193
.PHONY: cert
188194
certs: templates/certs/ca-certificates.crt ## Update root certificates
189195

cmd/build.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ EXAMPLES
130130

131131
// Temporarily Hidden Basic Auth Flags
132132
// Username, Password and Token flags, which plumb through basic auth, are
133-
// currently only available on the experimental "host" builder, which is
134-
// itself behind a feature flag FUNC_ENABLE_HOST_BUILDER. So set these
135-
// flags to hidden until it's out of preview and they are plumbed through
136-
// the docker pusher as well.
133+
// currently only available on the "host" builder.
137134
_ = cmd.Flags().MarkHidden("username")
138135
_ = cmd.Flags().MarkHidden("password")
139136
_ = cmd.Flags().MarkHidden("token")
@@ -165,13 +162,13 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro
165162
if err = cfg.Validate(); err != nil { // Perform any pre-validation
166163
return
167164
}
168-
if f, err = fn.NewFunction(cfg.Path); err != nil {
165+
if f, err = fn.NewFunction(cfg.Path); err != nil { // Read in the Function
169166
return
170167
}
171168
if !f.Initialized() {
172169
return fn.NewErrNotInitialized(f.Root)
173170
}
174-
f = cfg.Configure(f) // Updates f at path to include build request values
171+
f = cfg.Configure(f) // Returns an f updated with values from the config (flags, envs, etc)
175172

176173
cmd.SetContext(cfg.WithValues(cmd.Context())) // Some optional settings are passed via context
177174

@@ -184,7 +181,7 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro
184181
defer done()
185182

186183
// Build
187-
buildOptions, err := cfg.buildOptions()
184+
buildOptions, err := cfg.buildOptions() // build-specific options from the finalized cfg
188185
if err != nil {
189186
return
190187
}

cmd/deploy.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"os"
87
"strconv"
98
"strings"
109

@@ -205,10 +204,7 @@ EXAMPLES
205204

206205
// Temporarily Hidden Basic Auth Flags
207206
// Username, Password and Token flags, which plumb through basic auth, are
208-
// currently only available on the experimental "host" builder, which is
209-
// itself behind a feature flag FUNC_ENABLE_HOST_BUILDER. So set these
210-
// flags to hidden until it's out of preview and they are plumbed through
211-
// the docker pusher as well.
207+
// currently only available on "host" builder.
212208
_ = cmd.Flags().MarkHidden("username")
213209
_ = cmd.Flags().MarkHidden("password")
214210
_ = cmd.Flags().MarkHidden("token")
@@ -431,18 +427,7 @@ func KnownBuilders() builders.Known {
431427
// However, future third-party integrations may support less than, or more
432428
// builders, and certain environmental considerations may alter this list.
433429

434-
// Also a good place to stick feature-flags; to wit:
435-
enable_host, _ := strconv.ParseBool(os.Getenv("FUNC_ENABLE_HOST_BUILDER"))
436-
if !enable_host {
437-
bb := []string{}
438-
for _, b := range builders.All() {
439-
if b != builders.Host {
440-
bb = append(bb, b)
441-
}
442-
}
443-
return bb
444-
}
445-
430+
// Also a good place to stick feature-flags.
446431
return builders.All()
447432
}
448433

cmd/deploy_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,6 @@ func testAuthentication(cmdFn commandConstructor, t *testing.T) {
15311531
}
15321532

15331533
cmd := cmdFn(NewTestClient(fn.WithPusher(pusher)))
1534-
t.Setenv("FUNC_ENABLE_HOST_BUILDER", "true") // host builder is currently behind this feature flag
15351534
cmd.SetArgs([]string{"--builder", "host", "--username", testUser, "--password", testPass})
15361535
if err := cmd.Execute(); err != nil {
15371536
t.Fatal(err)

cmd/func-util/main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func scaffold(ctx context.Context) error {
7878
return fmt.Errorf("cannot load func project: %w", err)
7979
}
8080

81-
if f.Runtime != "go" {
81+
if f.Runtime != "go" && f.Runtime != "python" {
8282
// Scaffolding is for now supported/needed only for Go.
8383
return nil
8484
}
@@ -100,7 +100,17 @@ func scaffold(ctx context.Context) error {
100100
return fmt.Errorf("unable to create .s2i bin dir. %w", err)
101101
}
102102

103-
if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(s2i.GoAssembler), 0755); err != nil {
103+
var asm string
104+
switch f.Runtime {
105+
case "go":
106+
asm = s2i.GoAssembler
107+
case "python":
108+
asm = s2i.PythonAssembler
109+
default:
110+
panic("unreachable")
111+
}
112+
113+
if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(asm), 0755); err != nil {
104114
return fmt.Errorf("unable to write go assembler. %w", err)
105115
}
106116

cmd/run.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"knative.dev/func/pkg/config"
1515
"knative.dev/func/pkg/docker"
1616
fn "knative.dev/func/pkg/functions"
17+
"knative.dev/func/pkg/oci"
1718
)
1819

1920
func NewRunCmd(newClient ClientFactory) *cobra.Command {
@@ -38,9 +39,14 @@ DESCRIPTION
3839
The --container flag indicates that the function's container should be
3940
run rather than running the source code directly. This may require that
4041
the function's container first be rebuilt. Building the container on or
41-
off can be altered using the --build flag. The default value --build=auto
42-
indicates the system should automatically build the container only if
43-
necessary.
42+
off can be altered using the --build flag. The value --build=auto
43+
can be used to indicate the function should be run in a container, with
44+
the container automatically built if necessary.
45+
46+
The --container flag defaults to true if the builder defined for the
47+
function is a containerized builder such as Pack or S2I, and in the case
48+
where the function's runtime requires containerized builds (is not yet
49+
supported by the Host builder.
4450
4551
Process Scaffolding
4652
This is an Experimental Feature currently available only to Go projects.
@@ -103,6 +109,8 @@ EXAMPLES
103109
"You may provide this flag multiple times for setting multiple environment variables. "+
104110
"To unset, specify the environment variable name followed by a \"-\" (e.g., NAME-).")
105111
cmd.Flags().Duration("start-timeout", f.Run.StartTimeout, fmt.Sprintf("time this function needs in order to start. If not provided, the client default %v will be in effect. ($FUNC_START_TIMEOUT)", fn.DefaultStartTimeout))
112+
cmd.Flags().BoolP("container", "t", runContainerizedByDefault(f),
113+
"Run the function in a container. ($FUNC_CONTAINER)")
106114

107115
// TODO: Without the "Host" builder enabled, this code-path is unreachable,
108116
// so remove hidden flag when either the Host builder path is available,
@@ -116,8 +124,6 @@ EXAMPLES
116124
cmd.Flags().String("build", "auto",
117125
"Build the function. [auto|true|false]. ($FUNC_BUILD)")
118126
cmd.Flags().Lookup("build").NoOptDefVal = "true" // register `--build` as equivalient to `--build=true`
119-
cmd.Flags().BoolP("container", "t", true,
120-
"Run the function in a container. ($FUNC_CONTAINER)")
121127

122128
// Oft-shared flags:
123129
addConfirmFlag(cmd, cfg.Confirm)
@@ -136,14 +142,17 @@ EXAMPLES
136142
return cmd
137143
}
138144

145+
func runContainerizedByDefault(f fn.Function) bool {
146+
return f.Build.Builder == "pack" || f.Build.Builder == "s2i" || !oci.IsSupported(f.Runtime)
147+
}
148+
139149
func runRun(cmd *cobra.Command, newClient ClientFactory) (err error) {
140150
var (
141151
cfg runConfig
142152
f fn.Function
143153
)
144-
if cfg, err = newRunConfig(cmd).Prompt(); err != nil {
145-
return
146-
}
154+
cfg = newRunConfig(cmd) // Will add Prompt on upcoming UX refactor
155+
147156
if f, err = fn.NewFunction(cfg.Path); err != nil {
148157
return
149158
}
@@ -340,15 +349,8 @@ func (c runConfig) Validate(cmd *cobra.Command, f fn.Function) (err error) {
340349
}
341350
}
342351

343-
// There is currently no local host runner implemented, so specifying
344-
// --container=false should return an informative error for runtimes other
345-
// than Go that is more helpful than the cryptic, though correct, error
346-
// from the Client that it was instantated without a runner.
347-
// TODO: modify this check when the local host runner is available to
348-
// only generate this error when --container==false && the --language is
349-
// not yet implemented.
350-
if !c.Container && f.Runtime != "go" {
351-
return errors.New("the ability to run functions outside of a container via 'func run' is coming soon.")
352+
if !c.Container && !oci.IsSupported(f.Runtime) {
353+
return fmt.Errorf("The %q runtime currently requires being run in a container", f.Runtime)
352354
}
353355

354356
// When the docker runner respects the StartTimeout, this validation check

cmd/templates_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ go http
2727
node cloudevents
2828
node http
2929
python cloudevents
30-
python flask
3130
python http
32-
python wsgi
3331
quarkus cloudevents
3432
quarkus http
3533
rust cloudevents
@@ -67,9 +65,7 @@ func TestTemplates_JSON(t *testing.T) {
6765
],
6866
"python": [
6967
"cloudevents",
70-
"flask",
71-
"http",
72-
"wsgi"
68+
"http"
7369
],
7470
"quarkus": [
7571
"cloudevents",

docs/reference/func_build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func build
5858

5959
```
6060
--build-timestamp Use the actual time as the created time for the docker image. This is only useful for buildpacks builder.
61-
-b, --builder string Builder to use when creating the function's container. Currently supported builders are "pack" and "s2i". ($FUNC_BUILDER) (default "s2i")
61+
-b, --builder string Builder to use when creating the function's container. Currently supported builders are "host", "pack" and "s2i". ($FUNC_BUILDER) (default "s2i")
6262
--builder-image string Specify a custom builder image for use by the builder other than its default. ($FUNC_BUILDER_IMAGE)
6363
-c, --confirm Prompt to confirm options interactively ($FUNC_CONFIRM)
6464
-h, --help help for build

0 commit comments

Comments
 (0)