Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit 616399a

Browse files
imikushinberndtj
authored andcommitted
CLI: create function: support single-file functions (#474)
* CLI: create function: single-file functions Make `--handler` unnecessary when creating single-file functions. The base-image will try to guess the handler at function creation. This is language dependent. Function creation will fail for base-images not supporting this feature. Also, update docs and base-image versions * Bump base-image versions * Error out when source is a dir and no handler is set
1 parent a5fd9fc commit 616399a

Some content is hidden

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

41 files changed

+167
-133
lines changed

docs/_guides/quickstart.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ $ dispatch create --file seed.yaml --work-dir examples/
9090
$ dispatch get images
9191
NAME | URL | BASEIMAGE | STATUS | CREATED DATE
9292
------------------------------------------------------------------------------------------------------------------------
93-
nodejs6 | dispatchframework/nodejs-base:0.0.3 | nodejs6-base | READY | Wed Dec 6 14:28:30 PST 2017
94-
python3 | dispatchframework/python3-base:0.0.3 | python3-base | INITIALIZED | Wed Dec 6 14:28:30 PST 2017
93+
nodejs | dispatchframework/nodejs-base:0.0.7 | nodejs-base | READY | Wed Dec 6 14:28:30 PST 2017
94+
python3 | dispatchframework/python3-base:0.0.7 | python3-base | INITIALIZED | Wed Dec 6 14:28:30 PST 2017
9595

9696
$ dispatch get functions
9797
NAME | IMAGE | STATUS | CREATED DATE
9898
------------------------------------------------------------
99-
hello-js | nodejs6 | READY | Wed Dec 6 14:29:05 PST 2017
99+
hello-js | nodejs | READY | Wed Dec 6 14:29:05 PST 2017
100100
hello-py | python3 | READY | Wed Dec 6 14:28:52 PST 2017
101101
```
102102

docs/_guides/setup-service-acccount-authentication.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ use this private key to sign the generated JWT token.
4949

5050
```bash
5151
$ dispatch create -f seed.yaml --service-account example-svc-account --jwt-private-key ../example-user.key
52-
Created BaseImage: nodejs6-base
52+
Created BaseImage: nodejs-base
5353
Created BaseImage: python3-base
5454
Created BaseImage: powershell-base
55-
Created Image: nodejs6
55+
Created Image: nodejs
5656
Created Image: python3
5757
Created Image: powershell
5858
Created Function: hello-py
@@ -64,9 +64,9 @@ Created Secret: open-sesame
6464
$ dispatch get base-image --service-account example-svc-account --jwt-private-key ../example-user.key
6565
NAME | URL | STATUS | CREATED DATE
6666
------------------------------------------------------------------------------------------------
67-
python3-base | vmware/dispatch-python3-base:0.0.2-dev1 | READY | Sat Jan 1 14:40:18 PST 0000
68-
nodejs6-base | vmware/dispatch-nodejs6-base:0.0.2-dev1 | READY | Sat Jan 1 14:40:18 PST 0000
69-
powershell-base | vmware/dispatch-powershell-base:0.0.3 | READY | Sat Jan 1 14:40:18 PST 0000
67+
python3-base | dispatchframework/python3-base:0.0.7 | READY | Sat Jan 1 14:40:18 PST 0000
68+
nodejs-base | dispatchframework/nodejs-base:0.0.6 | READY | Sat Jan 1 14:40:18 PST 0000
69+
powershell-base | dispatchframework/powershell-base:0.0.8 | READY | Sat Jan 1 14:40:18 PST 0000
7070
```
7171

7272
If you are directly calling the API instead of using the CLI, you need to create a JWT payload as follows:

docs/_specs/language-support/language-support.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ This directory contains the Dockerfile and any supporting files (but typically,
4848

4949
The Dockerfile for the function image accepts build args (using `ARG` instruction):
5050

51-
- **`IMAGE`** — the *image* the be used as the base for the function image:
52-
ARG IMAGE
53-
FROM ${IMAGE}
54-
- **`FUNCTION_SRC`**, defaults to `function.txt` — the file with the function source code, put into the function image build container AS IS by Dispatch function builder.
51+
- **`IMAGE`** — the *image* the be used as the base for the *function image*
52+
- **`HANDLER`** — language specific handler function name (module path, fully qualified name, etc.)
5553

5654
Function builder expects the Dockerfile to have all the necessary instructions to build and install the function, so that when a container is run from the function image, the *Function Runtime API* (see below) is exposed on the port specified by `PORT` env var, which is 8080 by default.
5755

@@ -60,7 +58,7 @@ Function builder expects the Dockerfile to have all the necessary instructions t
6058

6159
It’s a request/reply RPC-style function invocation API implemented with a simple HTTP server written in the language provided by the *base image*. All function invocations go through this API. How the invocations reach this API is FaaS specific and outside of the function runtime responsibilities.
6260

63-
When a container is normally run from a *function* image (without specifying commands), the main (and usually, the only) process in that container listens on the specified `PORT` and serves this API via HTTP.
61+
When a container is normally run from a *function* image (without specifying commands), the main (and usually, the only) process in that container listens on the specified `PORT` and serves this API via HTTP, using the specified `HANDLER` function to handle incoming invocations.
6462

6563
It is the API server process’s responsibility to adequately react to OS signals (such as `SIGTERM`) and perform graceful shutdown, i.e. to stop accepting new requests and finish processing already accepted invocation(s) before exiting.
6664

@@ -123,18 +121,18 @@ Payloads must be JSON encodable.
123121

124122
A **user** registers an existing docker image as a *base image* in Dispatch:
125123

126-
$ dispatch create base-image js dispatchframework/nodejs-base:0.0.2
124+
$ dispatch create base-image js-base dispatchframework/nodejs-base:0.0.7
127125

128-
Here, `dispatchframework/nodejs-base:0.0.2` is registered as the *base image* named `js`.
126+
Here, `dispatchframework/nodejs-base:0.0.7` is registered as the *base image* named `js-base`.
129127

130128

131129
## Create an *Image*
132130

133131
A **user** creates an *image* to be used for their functions, including any needed system packages and library dependencies:
134132

135-
$ dispatch create image js-deps js --runtime-deps ./package.json
133+
$ dispatch create image js-deps js-base --runtime-deps ./package.json
136134

137-
Here, the *image* named `js-deps` is created from the *base image* `js` adding dependencies from the manifest file `./package.json`.
135+
Here, the *image* named `js-deps` is created from the *base image* `js-base` adding dependencies from the manifest file `./package.json`.
138136

139137
In order to create the *image*, **Dispatch image manager** does the following:
140138

@@ -151,13 +149,13 @@ A **user** creates a *function* from a source file using the specified *image*:
151149

152150
$ dispatch create function --image=js-deps hello1 . --handler=./hello.js
153151

154-
Here, the *function* named `hello1` is created from the source file `./hello.js` using the image `js-deps` containing library packages that can be used by the *function*.
152+
Here, the specified directory (`.`) is used as the root for function source files, *function* named `hello1` is created from `./hello.js`, using image `js-deps` containing library packages.
155153

156154
In order to create the function, **Dispatch function manager** does the following:
157155

158156
1. creates (doesn’t run) a temporary container from the specified *image*
159157
2. copies the directory specified by the metadata label `io.dispatchframework.functionTemplate` (in our case, `/function-template`) from the container into a temporary directory
160-
3. copies the function source file into the same directory
158+
3. copies the function sources into the same directory
161159
4. builds the docker image from the temporary directory, using the *image* docker image as the value of `IMAGE` build argument
162160
5. registers the docker image as the *function* image.
163161

@@ -176,10 +174,10 @@ In order to create the function, **Dispatch function manager** does the followin
176174

177175
There is a selection of base-images implementing this spec in [dispatchframework](https://github.com/dispatchframework) organization on GitHub:
178176

179-
- https://github.com/dispatchframework/nodejs-base-image
180-
- https://github.com/dispatchframework/python3-base-image
181-
- https://github.com/dispatchframework/powershell-base-image
182-
- https://github.com/dispatchframework/java-base-image
183-
- https://github.com/dispatchframework/clojure-base-image
177+
- [nodejs-base-image](https://github.com/dispatchframework/nodejs-base-image)
178+
- [python3-base-image](https://github.com/dispatchframework/python3-base-image)
179+
- [powershell-base-image](https://github.com/dispatchframework/powershell-base-image)
180+
- [java-base-image](https://github.com/dispatchframework/java-base-image)
181+
- [clojure-base-image](https://github.com/dispatchframework/clojure-base-image)
184182

185183
Any one of these images can be used as an example of how to add a new language support to Dispatch.

e2e/tests/api_update.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ kind: API
22
name: api-test-update
33
authentication: public
44
enabled: true
5-
function: func-nodejs6
5+
function: func-nodejs
66
methods:
77
- GET
88
protocols:

e2e/tests/apis.bats

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ load variables
1010

1111
@test "Create Images for test" {
1212

13-
run dispatch create base-image base-nodejs6 $DOCKER_REGISTRY/$BASE_IMAGE_NODEJS6 --language nodejs6
13+
run dispatch create base-image base-nodejs $DOCKER_REGISTRY/$BASE_IMAGE_NODEJS6 --language nodejs
1414
assert_success
15-
run_with_retry "dispatch get base-image base-nodejs6 --json | jq -r .status" "READY" 4 5
15+
run_with_retry "dispatch get base-image base-nodejs --json | jq -r .status" "READY" 4 5
1616

17-
run dispatch create image nodejs6 base-nodejs6
17+
run dispatch create image nodejs base-nodejs
1818
assert_success
19-
run_with_retry "dispatch get image nodejs6 --json | jq -r .status" "READY" 8 5
19+
run_with_retry "dispatch get image nodejs --json | jq -r .status" "READY" 8 5
2020
}
2121

2222
@test "Create Functions for test" {
23-
run dispatch create function --image=nodejs6 func-nodejs6 ${DISPATCH_ROOT}/examples/nodejs6 --handler=./hello.js
23+
run dispatch create function --image=nodejs func-nodejs ${DISPATCH_ROOT}/examples/nodejs --handler=./hello.js
2424
echo_to_log
2525
assert_success
2626

27-
run_with_retry "dispatch get function func-nodejs6 --json | jq -r .status" "READY" 10 5
27+
run_with_retry "dispatch get function func-nodejs --json | jq -r .status" "READY" 10 5
2828

29-
run dispatch create function --image=nodejs6 node-echo-back ${DISPATCH_ROOT}/examples/nodejs6 --handler=./debug.js
29+
run dispatch create function --image=nodejs node-echo-back ${DISPATCH_ROOT}/examples/nodejs --handler=./debug.js
3030
echo_to_log
3131
assert_success
3232

3333
run_with_retry "dispatch get function node-echo-back --json | jq -r .status" "READY" 10 5
3434
}
3535

3636
@test "Test APIs with HTTP(S)" {
37-
run dispatch create api api-test-http func-nodejs6 -m POST -p /http --auth public
37+
run dispatch create api api-test-http func-nodejs -m POST -p /http --auth public
3838
echo_to_log
3939
assert_success
4040

@@ -54,7 +54,7 @@ load variables
5454
}
5555

5656
@test "Test APIs with HTTPS ONLY" {
57-
run dispatch create api api-test-https-only func-nodejs6 -m POST --https-only -p /https-only --auth public
57+
run dispatch create api api-test-https-only func-nodejs -m POST --https-only -p /https-only --auth public
5858
echo_to_log
5959
assert_success
6060
run_with_retry "dispatch get api api-test-https-only --json | jq -r .status" "READY" 6 5
@@ -72,7 +72,7 @@ load variables
7272

7373
@test "Test APIs with Kong Plugins" {
7474

75-
run dispatch create api api-test func-nodejs6 -m GET -m DELETE -m POST -m PUT -p /hello --auth public
75+
run dispatch create api api-test func-nodejs -m GET -m DELETE -m POST -m PUT -p /hello --auth public
7676
echo_to_log
7777
assert_success
7878
run_with_retry "dispatch get api api-test --json | jq -r .status" "READY" 6 5
@@ -120,7 +120,7 @@ load variables
120120
}
121121

122122
@test "Test APIs with CORS" {
123-
run dispatch create api api-test-cors func-nodejs6 -m POST -m PUT -p /cors --auth public --cors
123+
run dispatch create api api-test-cors func-nodejs -m POST -m PUT -p /cors --auth public --cors
124124
echo_to_log
125125
assert_success
126126
run_with_retry "dispatch get api api-test-cors --json | jq -r .status" "READY" 10 5
@@ -133,7 +133,7 @@ load variables
133133
}
134134

135135
@test "Test API Updates" {
136-
run dispatch create api api-test-update func-nodejs6 -m GET -p /hello --auth public
136+
run dispatch create api api-test-update func-nodejs -m GET -p /hello --auth public
137137
assert_success
138138
run_with_retry "dispatch get api api-test-update --json | jq -r .status" "READY" 6 5
139139

e2e/tests/application.bats

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ load variables
2525
run dispatch get base-image
2626
assert_success
2727

28-
# Create base image "base-nodejs6"
29-
run dispatch create base-image base-nodejs6 $DOCKER_REGISTRY/$BASE_IMAGE_NODEJS6 --language nodejs6
28+
# Create base image "base-nodejs"
29+
run dispatch create base-image base-nodejs $DOCKER_REGISTRY/$BASE_IMAGE_NODEJS6 --language nodejs
3030
assert_success
3131

32-
run_with_retry "dispatch get base-image base-nodejs6 --json | jq -r .status" "READY" 4 5
32+
run_with_retry "dispatch get base-image base-nodejs --json | jq -r .status" "READY" 4 5
3333
}
3434

3535
@test "Image creation" {
3636

3737
run_with_retry "dispatch get image --json | jq -r length" 0 1 1
3838
assert_success
3939

40-
run dispatch create image foo-app-image base-nodejs6 --application foo-app
40+
run dispatch create image foo-app-image base-nodejs --application foo-app
4141
assert_success
42-
run dispatch create image bar-app-image base-nodejs6 --application bar-app
42+
run dispatch create image bar-app-image base-nodejs --application bar-app
4343
assert_success
4444

4545
# list image of foo app, only one returns
@@ -60,11 +60,11 @@ load variables
6060

6161
@test "Secret creation" {
6262

63-
run dispatch create secret open-sesame -a foo-app ${DISPATCH_ROOT}/examples/nodejs6/secret.json
63+
run dispatch create secret open-sesame -a foo-app ${DISPATCH_ROOT}/examples/nodejs/secret.json
6464
echo_to_log
6565
assert_success
6666

67-
run dispatch create secret open-sesame-bar -a bar-app ${DISPATCH_ROOT}/examples/nodejs6/secret.json
67+
run dispatch create secret open-sesame-bar -a bar-app ${DISPATCH_ROOT}/examples/nodejs/secret.json
6868
echo_to_log
6969
assert_success
7070

@@ -74,7 +74,7 @@ load variables
7474

7575
@test "Function creation" {
7676

77-
run dispatch create function --image=foo-app-image foo-app-func -a foo-app ${DISPATCH_ROOT}/examples/nodejs6 --handler=./i-have-a-secret.js --secret open-sesame
77+
run dispatch create function --image=foo-app-image foo-app-func -a foo-app ${DISPATCH_ROOT}/examples/nodejs --handler=./i-have-a-secret.js --secret open-sesame
7878
echo_to_log
7979
assert_success
8080

e2e/tests/events.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ load variables
1313
func_name=node-echo-back-${RANDOM}
1414
sub_name=testsub-${RANDOM}
1515
event_name=test.event.${RANDOM}
16-
run dispatch create function --image=nodejs6 ${func_name} ${DISPATCH_ROOT}/examples/nodejs6 --handler=./debug.js
16+
run dispatch create function --image=nodejs ${func_name} ${DISPATCH_ROOT}/examples/nodejs --handler=./debug.js
1717
echo_to_log
1818
assert_success
1919

@@ -82,7 +82,7 @@ load variables
8282
sub_name=testsub-${RANDOM}
8383
driver_name=testdriver-${RANDOM}
8484

85-
run dispatch create function --image=nodejs6 ${func_name} ${DISPATCH_ROOT}/examples/nodejs6 --handler=./debug.js
85+
run dispatch create function --image=nodejs ${func_name} ${DISPATCH_ROOT}/examples/nodejs --handler=./debug.js
8686
echo_to_log
8787
assert_success
8888

e2e/tests/functions.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ load variables
1111

1212
@test "Create node function no schema" {
1313

14-
run dispatch create function --image=nodejs6 node-hello-no-schema ${DISPATCH_ROOT}/examples/nodejs6 --handler=./hello.js
14+
run dispatch create function --image=nodejs node-hello-no-schema ${DISPATCH_ROOT}/examples/nodejs --handler=./hello.js
1515
echo_to_log
1616
assert_success
1717

1818
run_with_retry "dispatch get function node-hello-no-schema --json | jq -r .status" "READY" 8 5
1919
}
2020

2121
@test "Create a function with duplicate name" {
22-
run dispatch create function --image=nodejs6 node-hello-dup ${DISPATCH_ROOT}/examples/nodejs6 --handler=./hello.js
22+
run dispatch create function --image=nodejs node-hello-dup ${DISPATCH_ROOT}/examples/nodejs --handler=./hello.js
2323
echo_to_log
2424
assert_success
2525

26-
run dispatch create function --image=nodejs6 node-hello-dup ${DISPATCH_ROOT}/examples/nodejs6 --handler=./hello.js
26+
run dispatch create function --image=nodejs node-hello-dup ${DISPATCH_ROOT}/examples/nodejs --handler=./hello.js
2727
assert_failure
2828
}
2929

@@ -137,7 +137,7 @@ load variables
137137
}
138138

139139
@test "Create node function with schema" {
140-
run dispatch create function --image=nodejs6 node-hello-with-schema ${DISPATCH_ROOT}/examples/nodejs6 --handler=./hello.js --schema-in ${DISPATCH_ROOT}/examples/nodejs6/hello.schema.in.json --schema-out ${DISPATCH_ROOT}/examples/nodejs6/hello.schema.out.json
140+
run dispatch create function --image=nodejs node-hello-with-schema ${DISPATCH_ROOT}/examples/nodejs --handler=./hello.js --schema-in ${DISPATCH_ROOT}/examples/nodejs/hello.schema.in.json --schema-out ${DISPATCH_ROOT}/examples/nodejs/hello.schema.out.json
141141
echo_to_log
142142
assert_success
143143

e2e/tests/image_not_found_update.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Test when a base image does not exist
2-
dockerUrl: dispatchframework/python3-base:0.0.3
2+
dockerUrl: dispatchframework/python3-base:0.0.7
33
groups:
44
kind: BaseImage
55
language: python3

e2e/tests/images.bats

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ load variables
1010
run dispatch get base-image
1111
assert_success
1212

13-
# Create base image "base-nodejs6"
14-
run dispatch create base-image base-nodejs6 $DOCKER_REGISTRY/$BASE_IMAGE_NODEJS6 --language nodejs6
13+
# Create base image "base-nodejs"
14+
run dispatch create base-image base-nodejs $DOCKER_REGISTRY/$BASE_IMAGE_NODEJS6 --language nodejs
1515
assert_success
1616

1717
# Ensure starting status is "INITIALIZED". Wait 20 seconds for status "READY"
18-
run_with_retry "dispatch get base-image base-nodejs6 --json | jq -r .status" "INITIALIZED" 1 0
19-
run_with_retry "dispatch get base-image base-nodejs6 --json | jq -r .status" "READY" 4 5
18+
run_with_retry "dispatch get base-image base-nodejs --json | jq -r .status" "INITIALIZED" 1 0
19+
run_with_retry "dispatch get base-image base-nodejs --json | jq -r .status" "READY" 4 5
2020

2121
# Create base image "base-python3"
2222
run dispatch create base-image base-python3 $DOCKER_REGISTRY/$BASE_IMAGE_PYTHON3 --language python3
@@ -41,23 +41,23 @@ load variables
4141
run_with_retry "dispatch get base-image base-java --json | jq -r .status" "READY" 10 5
4242

4343
# Create fifth image with non-existing image. Check that get operation returns five images. Wait for "ERROR" status for missing image.
44-
run dispatch create base-image missing-image missing/image:latest --language nodejs6
44+
run dispatch create base-image missing-image missing/image:latest --language nodejs
4545
assert_success
4646
run bash -c "dispatch get base-image --json | jq '. | length'"
4747
assert_equal 5 $output
4848
run_with_retry "dispatch get base-image missing-image --json | jq -r .status" "ERROR" 4 5
4949
}
5050

5151
@test "Image creation" {
52-
run dispatch create image nodejs6 base-nodejs6
52+
run dispatch create image nodejs base-nodejs
5353
assert_success
5454
run dispatch create image python3 base-python3
5555
assert_success
5656
run dispatch create image powershell base-powershell
5757
assert_success
5858
run dispatch create image java base-java
5959
assert_success
60-
run_with_retry "dispatch get image nodejs6 --json | jq -r .status" "READY" 8 5
60+
run_with_retry "dispatch get image nodejs --json | jq -r .status" "READY" 8 5
6161
run_with_retry "dispatch get image python3 --json | jq -r .status" "READY" 8 5
6262
run_with_retry "dispatch get image powershell --json | jq -r .status" "READY" 8 5
6363
run_with_retry "dispatch get image java --json | jq -r .status" "READY" 8 5
@@ -90,9 +90,9 @@ load variables
9090
assert_success
9191

9292
run_with_retry "dispatch get image python3 --json | jq -r .status" "READY" 6 30
93-
run_with_retry "dispatch get base-image nodejs6-base --json | jq -r .status" "READY" 6 30
93+
run_with_retry "dispatch get base-image nodejs-base --json | jq -r .status" "READY" 6 30
9494

95-
run_with_retry "dispatch get base-image nodejs6-base --json | jq -r .language" "python3" 1 0
95+
run_with_retry "dispatch get base-image nodejs-base --json | jq -r .language" "python3" 1 0
9696
assert_success
9797

9898
run_with_retry "dispatch get image python3 --json | jq -r .tags[0].key" "update" 1 0

0 commit comments

Comments
 (0)