Skip to content

Commit 3c6e8f7

Browse files
authored
update go sample to declarative function and add multiple functions sample (#78)
* update hello-world-go to declarative function, add a multiple functions sample Signed-off-by: Lize Cai <lize.cai@sap.com> * update go ff to v0.2.4 Signed-off-by: Lize Cai <lize.cai@sap.com> * update go-builder to v2.3.0, update go-ff to v0.3.0 in http sample Signed-off-by: Lize Cai <lize.cai@sap.com> * update ff-go to v0.3.0 in knative go function samples Signed-off-by: Lize Cai <lize.cai@sap.com> * add suffix for builder-go -> builder-go:v2.3.0-1.16 Signed-off-by: Lize Cai <lize.cai@sap.com>
1 parent 292ab4b commit 3c6e8f7

8 files changed

Lines changed: 224 additions & 1 deletion

File tree

functions-framework/golang/Knative/http/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module main.go
33
go 1.16
44

55
require (
6-
github.com/OpenFunction/functions-framework-go v0.2.2
6+
github.com/OpenFunction/functions-framework-go v0.3.0
77
github.com/fatih/structs v1.1.0
88
k8s.io/klog/v2 v2.30.0
99
)

functions/knative/hello-world-go/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66

77
You can refer to the [Installation Guide](https://github.com/OpenFunction/OpenFunction#install-openfunction) to setup OpenFunction.
88

9+
## Run it locally
10+
11+
Build the function locally
12+
13+
```sh
14+
pack build func-helloworld-go --builder openfunction/builder-go:v2.3.0-1.16 --env FUNC_NAME="HelloWorld" --env FUNC_CLEAR_SOURCE=true
15+
```
16+
17+
Run the function
18+
19+
```sh
20+
docker run --rm --env="FUNC_CONTEXT={\"name\":\"HelloWorld\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}" --env="CONTEXT_MODE=self-host" --name func-helloworld-go -p 8080:8080 func-helloworld-go
21+
```
22+
23+
Send a request
24+
25+
```sh
26+
curl http://localhost:8080/OpenFunction
27+
# Hello, OpenFunction!
28+
```
29+
930
## Deployment
1031

1132
1. Create secret
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module example.com/hello
22

33
go 1.16
4+
5+
require github.com/OpenFunction/functions-framework-go v0.3.0

functions/knative/hello-world-go/hello.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ package hello
33
import (
44
"fmt"
55
"net/http"
6+
7+
"github.com/OpenFunction/functions-framework-go/functions"
68
)
79

10+
func init() {
11+
functions.HTTP("HelloWorld", HelloWorld)
12+
}
13+
814
func HelloWorld(w http.ResponseWriter, r *http.Request) {
915
fmt.Fprintf(w, "Hello, %s!\n", r.URL.Path[1:])
1016
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Sample Function Go
2+
3+
## Prerequisites
4+
5+
### OpenFunction
6+
7+
You can refer to the [Installation Guide](https://github.com/OpenFunction/OpenFunction#install-openfunction) to setup OpenFunction.
8+
9+
## Run it locally
10+
11+
Build the function locally
12+
13+
```sh
14+
pack build multiple-functions-go --builder openfunction/builder-go:v2.3.0-1.16 --env FUNC_NAME="MultipleFunctions" --env FUNC_CLEAR_SOURCE=true
15+
```
16+
17+
Run the function
18+
19+
```sh
20+
docker run --rm --env="FUNC_CONTEXT={\"name\":\"MultipleFunctions\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}" --env="CONTEXT_MODE=self-host" --name multiple-functions-go -p 8080:8080 multiple-functions-go
21+
```
22+
23+
Send a request
24+
25+
```sh
26+
curl http://localhost:8080/bar
27+
# hello, bar!
28+
29+
curl http://localhost:8080/foo
30+
# {"hello":"foo!"}%
31+
```
32+
33+
34+
## Deployment
35+
36+
1. Create secret
37+
38+
Generate a secret to access your container registry, such as one on [Docker Hub](https://hub.docker.com/) or [Quay.io](https://quay.io/).
39+
You can create this secret by editing the ``REGISTRY_SERVER``, ``REGISTRY_USER`` and ``REGISTRY_PASSWORD`` fields in following command, and then run it.
40+
41+
```bash
42+
REGISTRY_SERVER=https://index.docker.io/v1/ REGISTRY_USER=<your_registry_user> REGISTRY_PASSWORD=<your_registry_password>
43+
kubectl create secret docker-registry push-secret \
44+
--docker-server=$REGISTRY_SERVER \
45+
--docker-username=$REGISTRY_USER \
46+
--docker-password=$REGISTRY_PASSWORD
47+
```
48+
49+
2. Create function
50+
51+
For sample function below, modify the ``spec.image`` field in ``function-sample.yaml`` to your own container registry address:
52+
53+
```yaml
54+
apiVersion: core.openfunction.io/v1beta1
55+
kind: Function
56+
metadata:
57+
name: function-sample
58+
spec:
59+
image: "<your registry name>/sample-go-multi-func:latest"
60+
```
61+
62+
Use the following command to create this Function:
63+
64+
```shell
65+
kubectl apply -f function-sample.yaml
66+
```
67+
68+
3. Access function
69+
70+
You can observe the process of a function with the following command:
71+
72+
```shell
73+
kubectl get functions.core.openfunction.io
74+
75+
NAME BUILDSTATE SERVINGSTATE BUILDER SERVING URL AGE
76+
function-sample Succeeded Running builder-jgnzp serving-q6wdp http://openfunction.io/default/function-sample 22m
77+
```
78+
79+
The `URL` is the address provided by the OpenFunction Domain that can be accessed. To access the function via this URL address, you need to make sure that DNS can resolve this address.
80+
81+
> You can use the following command to create a pod in the cluster and access the function from the pod:
82+
>
83+
> ```shell
84+
> kubectl run curl --image=radial/busyboxplus:curl -i --tty
85+
> ```
86+
87+
Access the function via `URL`:
88+
89+
```shell
90+
[ root@curl:/ ]$ curl http://openfunction.io.svc.cluster.local/default/function-sample/foo
91+
{"hello":"foo!"}%
92+
93+
[ root@curl:/ ]$ curl http://openfunction.io.svc.cluster.local/default/function-sample/bar
94+
hello, bar!
95+
```
96+
97+
There is also an alternative way to trigger the function via the access address provided by the Knative Services:
98+
99+
```shell
100+
kubectl get ksvc
101+
102+
NAME URL LATESTCREATED LATESTREADY READY REASON
103+
serving-q6wdp-ksvc-wk6mv http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io serving-q6wdp-ksvc-wk6mv-v100 serving-q6wdp-ksvc-wk6mv-v100 True
104+
```
105+
106+
Or get the service address directly with the following command:
107+
108+
> where` <external-ip> `indicates the external address of your gateway service.
109+
>
110+
> You can do a simple configuration to use the node ip as the `<external-ip>` as follows (Assuming you are using Kourier as network layer of Knative). Where `1.2.3.4` can be replaced by your node ip.
111+
>
112+
> ```shell
113+
> kubectl patch svc -n kourier-system kourier \
114+
> -p '{"spec": {"type": "LoadBalancer", "externalIPs": ["1.2.3.4"]}}'
115+
>
116+
> kubectl patch configmap/config-domain -n knative-serving \
117+
> --type merge --patch '{"data":{"1.2.3.4.sslip.io":""}}'
118+
> ```
119+
120+
```shell
121+
kubectl get ksvc serving-q6wdp-ksvc-wk6mv -o jsonpath={.status.url}
122+
123+
http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io
124+
```
125+
126+
Access the above service address via commands such as ``curl``:
127+
128+
```shell
129+
curl http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io/foo
130+
131+
{"hello":"foo!"}%
132+
133+
curl http://serving-q6wdp-ksvc-wk6mv.default.<external-ip>.sslip.io/bar
134+
135+
hello, bar!
136+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: core.openfunction.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: function-sample
5+
spec:
6+
version: "v2.0.0"
7+
image: "openfunctiondev/sample-go-multi-func:latest"
8+
imageCredentials:
9+
name: push-secret
10+
port: 8080 # default to 8080
11+
build:
12+
builder: openfunction/builder-go:latest
13+
env:
14+
FUNC_NAME: "MultipleFunctions"
15+
FUNC_CLEAR_SOURCE: "true"
16+
srcRepo:
17+
url: "https://github.com/OpenFunction/samples.git"
18+
sourceSubPath: "functions/knative/multiple-functions-go"
19+
revision: "main"
20+
serving:
21+
template:
22+
containers:
23+
- name: function
24+
imagePullPolicy: Always
25+
runtime: "knative"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module example.com/hello
2+
3+
go 1.16
4+
5+
require github.com/OpenFunction/functions-framework-go v0.3.0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package hello
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/OpenFunction/functions-framework-go/functions"
9+
)
10+
11+
func init() {
12+
functions.HTTP("Foo", Foo, functions.WithFunctionPath("/foo"))
13+
functions.HTTP("Bar", Bar, functions.WithFunctionPath("/bar"))
14+
15+
}
16+
17+
func Foo(w http.ResponseWriter, r *http.Request) {
18+
response := map[string]string{
19+
"hello": "foo!",
20+
}
21+
responseBytes, _ := json.Marshal(response)
22+
w.Header().Set("Content-type", "application/json")
23+
w.Write(responseBytes)
24+
}
25+
26+
func Bar(w http.ResponseWriter, r *http.Request) {
27+
fmt.Fprintln(w, "hello, bar!\n")
28+
}

0 commit comments

Comments
 (0)