|
| 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 | + ``` |
0 commit comments